Question

In: Computer Science

JAVA Write a Java console application that prompts the user to enter the radius of a...

JAVA

Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area.

The Console Output

Enter the radius of the circle: 1.2

The radius is 1.2
The diameter is 2.4
The circumference is 7.5398223686155035
The area is 4.523893421169302

Programmer Notes

  • Write and document your program per class coding conventions.
  • Add an instance variable double radius. Generate its get/set methods.
  • Manually type the methods getDiameter(), getCircumference(), and getArea(). To calculate the circumference and area, use 3.14159 or the Java defined constant Math.PI
  • In main method, create a new instance of Assign3: Assign3 circle = new Assign3();
  • Create an instance of Scanner. Call print() to prompt the user to enter the radius. Call input.nextDouble() to get the user input. Call circle.setRadius() to set the radius.
  • Then call println() 4 times, each with the value of getRadius, getDiameter, getCircumference, getArea, respectively.

Solutions

Expert Solution



public class Circle {
    private double radius;

    public Circle() {
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getDiameter() {
        return 2 * radius;
    }

    public double getCircumference() {
        return 2 * Math.PI * radius;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }
}



import java.util.Scanner;

public class CircleTest {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the radius of the circle: ");
        Circle circle = new Circle();
        circle.setRadius(in.nextDouble());
        System.out.println("\nThe radius is " + circle.getRadius());
        System.out.println("The diameter is " + circle.getDiameter());
        System.out.println("The circumference is " + circle.getCircumference());
        System.out.println("The area is " + circle.getArea());
        in.close();
    }
}


Related Solutions

[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
C# Create a console application that prompts the user to enter a regular expression, and then...
C# Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a regular expression...
C# & ASP.NET Create a console application that prompts the user to enter a regular expression,...
C# & ASP.NET Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a...
Write a C++ console program that prompts a user to enter information for the college courses...
Write a C++ console program that prompts a user to enter information for the college courses you have completed, planned, or are in progress, and outputs it to a nicely-formatted table. Name the CPP as you wish (only use characters, underscores and number in your file name. DO NOT use blank). Its output should look something like this: Course Year Units Grade ---------- ---- ----- ----- comsc-110 2015 4 A comsc-165 2016 4 ? comsc-200 2016 4 ? comsc-155h 2014...
Write a Java application that prompts the user for an age. If the age entered is...
Write a Java application that prompts the user for an age. If the age entered is greater or equal to 65, display the statement "Age is greater than or equal to 65"; otherwise display the message "Age is less than 65". If the age entered is less than 18; display the statement "This person is a minor"; otherwise display the message "This person can legally vote. Do not create a class for this application. The code can be created in...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT