Question

In: Computer Science

Copy class CirclesViewer from Codecheck and complete it. You need to import the graphics package. After...

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;

}
}

Solutions

Expert Solution

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

Related Solutions

This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import java.util.Scanner; /** * * */ public class SoftOpening {    public static void main(String[] args) {       Scanner input = new Scanner(System.in);    ArrayList foodList = generateMenu();    User user = generateUser(input); user.introduce();    userBuyFood(foodList, user, input); user.introduce(); } public static ArrayList generateMenu(){    ArrayList foodList = new ArrayList<>();    Food pizza1 =new Food(1,"pizza","Seafood",11,12); Food pizza2 =new Food(2,"pizza","Beef",9,10); Food Friedrice =new Food(3,"fried rice","Seafood",5,12);...
To complete this exercise you need a software package that allows you to generate data from...
To complete this exercise you need a software package that allows you to generate data from the uniform and normal distributions. (i) Start by generating 500 observations xi - the explanatory variable - from the uniform distribution with range [0,10]. (Most statistical packages have a command for the Uniform[0,1] distribution; just multiply those observations by 10.) What are the sample mean and sample standard deviation of the xi? (ii) Randomly generate 500 errors, ui, from the Normal[0,36] distribution. (If you...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import...
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import java.util.List; /** * An abstraction over the idea of a search. * * @author liberato * * @param <T> : generic type. */ public abstract class Searcher<T> { protected final SearchProblem<T> searchProblem; protected final List<T> visited; protected List<T> solution; /**    * Instantiates a searcher.    *    * @param searchProblem    *            the search problem for which this searcher will find and   ...
PYTHON Suppose you need to import a single class named GeneTools from a module named bioinformatics,...
PYTHON Suppose you need to import a single class named GeneTools from a module named bioinformatics, in Python. Which of the following represents the correct use of an import statement? import GeneTools from bioinformatics from bioinformatics import GeneTools from bioinformatics import * import GeneTools
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // As a hint, you should not have to use the name of each // month more than once. // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month...
import java.util.Scanner; public class Months { // You will need to write a method that makes...
import java.util.Scanner; public class Months { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month = input.nextInt(); String output = monthAsString(month); System.out.println(output); } }
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
Sunset Graphics often buys inventory after receiving a sales order from the customer. Suppose you are...
Sunset Graphics often buys inventory after receiving a sales order from the customer. Suppose you are asked to prepare one UML class diagram that combines both the sales and collection process and the purchases and payments process. What would be shared among those processes? What would be unique to each process? Why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT