In the following picture, you can see the diagram of class circle. Design class
ID: 3808373 • Letter: I
Question
In the following picture, you can see the diagram of class circle. Design class Circle with the following information: Class circle contains: three instance variable radius: it is a private instance variable of the type double, with default value of 1.0 color it is a private instance variable of the type String, with default value of "green". label: it is a public instance variable of the type String, with default value of "group1". three overloaded constructors - a default constructor with no argument, and a constructor which takes a double argument for radius, and a constructor which takes a double argument for radius, a string argument for color, and a string argument for label. Five public methods; getRadius(), getArea(), getCircumference(), getLabel(), and setLabel(:string). Design a class TestCircle, and let users to create 3 instance of class Circle by asking the following questions from the user. What is the label of your circle? What is the color of your circle? What is the radius of your circle? Now, for each circle, print the following information in a file named label txt (label is the label of the circle) For example: Inside file circle01.txt we have:Explanation / Answer
Circle.java
public class Circle
{
//Declare needed variable
private final double PI_Val = 3.14159;
private double cRadius;
private String cColor;
private String cLabel;
//Constructor
public Circle()
{
//Initialize
cRadius = 0.0;
}
//Constructor
public Circle(double r)
{
//Initialize
cRadius = r;
}
//Constructor
public Circle(double r, String c, String l)
{
//Initialize
cRadius = r;
cColor= c;
cLabel =l;
}
//Method setcRadius()
public void setcRadius(double r)
{
//Initialize
cRadius = r;
}
//Method getcRadius()
public double getcRadius()
{
//Return
return cRadius;
}
//Method getcColor()
public String getcColor()
{
//Return
return cColor;
}
//Method getcLabel()
public String getcLabel()
{
//Return
return cLabel;
}
//Method setcLabel()
public void setcLabel()
{
//Initialize
cLabel = "a1";
}
//Method getcArea()
public double getcArea()
{
//Return
return PI_Val * cRadius * cRadius;
}
//Method getcCircumference()
public double getcCircumference()
{
//Return
return 2 * PI_Val * cRadius;
}
}
TestCircle.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.Scanner;
public class TestCircle
{
//Driver
public static void main(String[] args)
{
//Create instance
Scanner ourSc = new Scanner(System.in);
//Create instance
DecimalFormat two = new DecimalFormat("#0.00");
//Prompt for input
System.out.print("What is the label of the circle? ");
//Read input
String cLabel = ourSc.next();
//Prompt for input
System.out.print("What is the color of the circle? ");
//Read input
String cColor = ourSc.next();
//Prompt for input
System.out.print("What is the radius of the circle? ");
//Read input
double cRadius = ourSc.nextDouble();
//Close scanner
ourSc.close();
//Create instance
Circle circle = new Circle(cRadius);
//Output
System.out.println(" ");
System.out.println("The color of the circle is: " + cColor);
System.out.println("The radius of the circle is: " + cRadius);
System.out.println("The area of the circle is " + two.format(circle.getcArea()));
System.out.println("The circumference of the circle is " + two.format(circle.getcCircumference()));
//Create instance
BufferedWriter foutput = null;
//Try
try
{
//Output
String fpath="circle01.txt";
File file = new File(fpath);
//Check
if (!file.exists())
{
file.createNewFile();
}
//Create instance
FileWriter fw1 = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw1 = new BufferedWriter(fw1);
// Write in file
bw1.write(" The color of the circle is: " + cColor);
bw1.write(" The radius of the circle is: " + cRadius);
bw1.write(" The area of the circle is " + two.format(circle.getcArea()));
bw1.write(" The circumference of the circle is " + two.format(circle.getcCircumference()));
// Close connection
bw1.close();
}
//Catch
catch(Exception e)
{
//Display
System.out.println(e);
}
}
}
Output:
What is the label of the circle? c1
What is the color of the circle? red
What is the radius of the circle? 8
The color of the circle is: red
The radius of the circle is: 8.0
The area of the circle is 201.06
The circumference of the circle is 50.27
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.