A basic java programming questions. Here is the assignment. Create a class named
ID: 3569673 • Letter: A
Question
A basic java programming questions. Here is the assignment.
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() andgetRadius(). 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. UsingsetRadius() method, assign one circle a small radius value (code a user input making use of Scanner ofJOptionPane), 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.
I wrote most of the code, however I am now stuck. Here are the two classes I created.
import java.util.Scanner;
import java.lang.Math;
public class Circle{
//Declare Variables
double Radius;
double Diameter;
double Area;
Scanner scan = new Scanner(System.in);
//Here is the constructor. I have the Radius set for 1 and the mathematical equation
//for Diameter and Area
public Circle (double Radius, double Diameter, double Area){
Radius = 1;
Diameter = (Radius*2);
Area = Math.PI*(Radius * Radius);
}
public double getRadius(){
double Radius = scan.nextDouble();
return this.Radius;
}
public void setRadius(){
this.Radius = Radius;
Diameter = (Radius*2);
Area = Math.PI*(Radius * Radius);
}
}
import java.util.Scanner;
public class TestCircle{
double Radius;
Scanner in = new Scanner(System.in);
public static void main(String[] args){
//Create Circle Objects
Circle Circle1 = new CircleObject();
Circle Circle2 = new CircleObject();
Circle Circle3 = new CircleObject();
//Create output for user
Circle1.getRadius();
Circle2.getRadius();
Circle3.setRadius();
}
}
I still need to do the portion that displays the circle information. The biggest problem I'm having is that it won't make the objects. Here is the error I'm receiving.
Circle Circle1 = new CircleObject();
^
symbol: class CircleObject
location: class TestCircle
Thank you in advance for any help you can provide.
Explanation / Answer
Here you go :)
Answer as per the given original question.
Comment if you have any doubts
//Circle class
import java.util.Scanner;
import java.lang.Math;
import javax.swing.JOptionPane;
public class Circle {
// Declare Variables
double Radius;
double Diameter;
double Area;
Scanner scan = new Scanner(System.in);
// Here is the constructor. I have the Radius set for 1 and the mathematical
// equation
// for Diameter and Area
public Circle() {
Radius = 1;
Diameter = (Radius * 2);
Area = Math.PI * (Radius * Radius);
}
public double getRadius() {
return this.Radius;
}
public void setRadius(double Radius) {
this.Radius = Radius;
Diameter = (Radius * 2);
Area = Math.PI * (Radius * Radius);
}
}
//#######################################################################
//TestCircle class
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TestCircle {
double Radius;
Scanner in = new Scanner(System.in);
public static void main(String[] args) {
// Create Circle Objects
Circle Circle1 = new Circle();
Circle Circle2 = new Circle();
Circle Circle3 = new Circle();
// Create output for user
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);
System.out.println("Radius of 2nd circle: "+Circle2.getRadius());
System.out.println("Radius of 3rd circle: Radius: "+Circle3.getRadius());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.