This is my first question. These 2 create method questions, were on a quiz I took. Create a method, to find a radius, double r. Return the array and its indices, from the 2d circles array. If you can't find double r, return {-1,-1}.**
public int[] void findCircleWithRadius(Circle[][] circles, double r) {
for(int i = 0; i<circles.length-1; i++) { //search the row
for(int j = 0; j<circles[0].length; j++) { //search each column
Circle temp = circles[i][j];
if(temp == r) r = temp;
else return "{-1,-1}";
}
}
return circles.indexOf(r);
}
Create a method to swap the circles, using the findCircleWithRadius method.
public static void swapCircles(Circles[][] circles, double r1, double r2) {
Circle radius1 = r1.findCircleWithRadius();
Circle radius2 = r2.findCircleWithRadius();
Circle temp2 = radius2;
radius2 = radius1;
radius1 = temp2;
}
A few things to fix in your first example:
Right now, j < circles[0].length
means that only one column is being searched: column 0
. You'll want j < circles[i].length
to search every column by row.
if (temp == r)
means you're comparing a Circle
and a double
. I'm not familiar with the circle class, but I believe you'll want to replace instead of Circle temp = circles[i][j]
with double temp = circles[i][j].getRadius();
.
You want to return as soon as you find the matching Circle
, so you have some things a little backwards. With my new revisions, if (temp == r)
will now activate the code if you have found the correct radius. That means below that if
statement, you'll want return {i, j};
. That will return the current circle's (which has the correct radius) indicies.
The last statement will be called if none of the radius tests return true, so where you have return circles.indexOf(r);
, you'll want return {-1, -1};
.
Since arrays are 0-based, and less than already means one minus the value, you do not need - 1
in i < circles.length - 1
On your second example:
Your findCircleWithRadius
method has two paramaters: a Circle[][]
and a double
. That means you'll need to give it those. The method you created is not called from a double, as well, so you can't say r1.findCircleWithRadius();
Additionally, you need to use the int[]
that the findCircleWithRadius
passes you to get those Circle
's.
Therefore, your first lines in swapCircles
should be:
int[] rad1 = this.findCircleWithRadius(circles, r1); // Get the coordinates of the first circle by passing the 2D array, and the radius you're looking for.
int[] rad2 = this.findCircleWithRadius(circles, r2); // Get the coordinates of the second circle by passing the 2D array, and the radius you're looking for.
Circle radius1 = circles[rad1[0]][rad1[1]]; // Circle 1 is equal to the Circle in the array that has coordinates of the first index in the coordinates, and the second index of the coordinates. (circles[x, y])
Circle radius2 = circles[rad2[0]][rad2[1]]; // Circle 2 is equal to the Circle in the array that has coordinates of the first index in the coordinates, and the second index of the coordinates. (circles[x, y])
In conclusion, the completed code with my revisions will be as follows:
public int[] void findCircleWithRadius(Circle[][] circles, double r) {
for(int i = 0; i < circles.length; i++) { //search the row
for(int j = 0; j < circles[i].length; j++) { //search each column
double temp = circles[i][j].getRadius();
if(temp == r)
return {i, j};
}
}
return {-1, -1};
}
public static void swapCircles(Circles[][] circles, double r1, double r2) {
int[] rad1 = this.findCircleWithRadius(circles, r1);
int[] rad2 = this.findCircleWithRadius(circles, r2);
Circle radius1 = circles[rad1[0]][rad1[1]];
Circle radius2 = circles[rad2[0]][rad2[1]];
Circle temp2 = radius2;
radius2 = radius1;
radius1 = temp2;
}
Otherwise, everything else is looking pretty good! I hope you did well on your quiz! Please let me know if you have any further questions about anything I have said so I can make sure you understand this fully.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With