20.1k views
0 votes
Write code in java that takes input from the user for the radius (double) of a circle, and create a circle with that radius. The program should then print a sentence with the circumference and area of the circle. You should use the appropriate Circle methods to obtain the circumference and area of the circle rather than calculating these values yourself.

Sample run:

Enter the radius of the circle:
> 3
A circle with a radius 3.0 has a circumference of 18.84955592153876 and an area of 28.274333882308138

User Deniskrr
by
7.0k points

1 Answer

4 votes

Answer:

Step-by-step explanation:

import java.util.Scanner;

public class Circumference {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter the radius of the circle: ");

double userInput = input.nextDouble();

//create a new instance of the Circumference Class

Circumference c = new Circumference();

System.out.println(“A circle with a radius of “ + userInput + “ has a circumference of ” + c.getCircumference(userInput) + "and an area of " + c.getArea(userInput);

}

public double getCircumference(double radius){

return 2.0 * Math.PI * radius; //formula for calculating circumference

}

public double getArea(double radius){

return radius * radius * Math.PI; //formula for finding area

}

}

/* Formatting may be a lil weird in the main method(), if your getting errors just comment the print statement and re-type it yourself :)*/

User BanditKing
by
7.6k points