Create a class named Circle with field names radius, diameter, and area. Include
ID: 3673552 • Letter: C
Question
Create a class named Circle with field names radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius, but calculates the other two values (as you know, the diameter of a circle is twice the radius, the area of a circle is pi multiplied by the square of the radius). Save class as Circle.java. Create a class named TestCircle whose main method() declares several Circle objects. Using setRadius() method, assign one circle a small radius value (code a user input making use of Scanner of JOptionPane), and assign another a larger radius value (to be an input by a user as well). Do not assign a value to the radius of the third circle; instead retain the value assigned at object construction. Display all the values for three Circle objects. Save the class as TestCircle.java. Note: Create an UML class diagram prior to coding the application. Compile, run, and check the results. Submit the following: The source .java files An MS .doc file with explanations/comments of your solution and the results of a test run, including a screen shots pasted to the MS .doc file The UML class diagram of the application included as a part of the MS .doc above
Explanation / Answer
/*HEADER FILES*/
import java.util.Scanner;
import java.lang.Math;
import javax.swing.JOptionPane;
/*INTILIZATION*/
public class Circle
{
// Declare Variables
double Radius;
double Diameter;
double Area;
Scanner scan = new Scanner(System.in);
/*CONSTRUCTOR*/
Public Circle()
{
Radius = 1;
Diameter = (Radius * 2);
Area = Math.PI * (Radius * Radius);
}
/*FUNCTION*/
public double getRadius()
{
return this.Radius;
}
public void setRadius(double Radius)
{
/*CALCULATION*/
this.Radius = Radius;
/*FORMULAE*/
Diameter = (Radius * 2);
Area = Math.PI * (Radius * Radius);
}
/*END MAIN*/
}
/*HEADER FILES*/
import java.util.Scanner;
import javax.swing.JOptionPane;
/*DECLARATION*/
public class TestCircle
{
double Radius;
Scanner in = new Scanner(System.in);
/*MAIN METHOD*/
public static void main(String[] args)
{
/*Create Circle Objects*/
Circle Circle1 = new Circle();
Circle Circle2 = new Circle();
Circle Circle3 = new Circle();
/*COMPUTATION*/
Double r1=Double.parseDouble(JOptionPane.showInputDialog("Enter the radius of 1st circle: "));
Circle1.setRadius(r1);
System.out.println("Radius of 1st circle: "+Circle1.getRadius()); r1=Double.parseDouble(JOptionPane.showInputDialog("Enter the radius of 1st circle: "));
Circle2.setRadius(r1);
/*DISPLAY THE RADIUS*/
System.out.println("Radius of 2nd circle: "+Circle2.getRadius());
System.out.println("Radius of 3rd circle: Radius: "+Circle3.getRadius());
}
/*END MAIN*/
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.