In: Computer Science
Copy class CirclesViewer from Codecheck and complete it. You need to import the graphics package.
After completed, the program will ask the user to enter an integer between 1 and 10 then draw the specified number of circles. The radius starts at 10 and increments by 10 for each next one. They all touch the line x = 5 at the left and the line y = 10 at the top.
Another way to look at this is that the circles are inside a right angle consisting of the lines x = 5 and y = 10. The circles are tangent to the sides of the right angle.
Here is an image of 10 circles with the tangent lines drawn in red.
Use the message “Enter an integer between 1 and 10: ” to get the input. The input could be invalid, and you need to use a loop to read input until it is valid.
You must not use try-catch statement or call any parse method.
If the input is not an integer, display message “Not an integer!”, read the input, then prompt for input again.
If the input is an integer but out of the range, display message “Not in the range!”, then prompt for input again.
No magic numbers, except the numbers in the input prompt.
For the draft, just get the input and do not draw any circles.
codecheck:
mport java.util.Scanner;
/**
* A Java application to draw circles.
*
* @author
* @version
*/
import java.util.Scanner;
public class CirclesViewer
{
public static final int MAX_NUM_OF_CIRCLES = 10;
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
boolean validInput = false;
int numOfCircles = 0;
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: since you did not mention any details about how to draw (what library to be used (using a JFrame or a DrawingPanel or StdDraw etc)) and since the question says "just get the input and do not draw any circles", I'm just leaving that part blank for now. Let me know more details and I'll update the code myself.
//CirclesViewer.java
import java.util.Scanner;
/**
* A Java application to draw circles.
*
* @author
* @version
*/
import java.util.Scanner;
public class CirclesViewer {
public static final int MAX_NUM_OF_CIRCLES = 10;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean validInput = false;
int numOfCircles = 0;
// looping as long as validInput is false
while (!validInput) {
// asking for input
System.out.print("Enter an integer between 1 and 10: ");
// checking if there is an integer on the input buffer
if (!in.hasNextInt()) {
// nope!, clearing contents upto next newline
in.nextLine();
// and then printing error
System.out.println("Not an integer!");
} else {
// yes, reading integer
numOfCircles = in.nextInt();
in.nextLine(); // ignoring rest of the line
// validating numOfCircles
if (numOfCircles <= 0 || numOfCircles > MAX_NUM_OF_CIRCLES) {
// out of range
System.out.println("Not in the range!");
} else {
// valid.
validInput = true;
}
}
}
// at this point, we have a valid input.
// the code to draw circles should be here. but since you did not
// mention any details about how to draw (what library to be used) and
// the question says "just get the input and do not draw any circles",
// I'm just leaving that part blank for now. Let me know more details
// and I'll update the code myself.
}
}
/*OUTPUT*/
Enter an integer between 1 and 10: gsb shys
Not an integer!
Enter an integer between 1 and 10: x
Not an integer!
Enter an integer between 1 and 10: 13
Not in the range!
Enter an integer between 1 and 10: -2
Not in the range!
Enter an integer between 1 and 10: 5