In: Computer Science
JAVA: Create a circle that contains the following... Sample code to work from provided below.
FIELDS:
CONSTRUCTORS
METHODS
Sample Code to work from: (Driver class & Test class)
public class Circle {
private double radius;
/**
* No argument constructor. Creates a Circle with radius 0
* */
// Your code here
/**
* Constructor, creates a circle with the provided radius
* @param radius - the desired radius of our circle
* */
// Your code here
/**
* Set new radius for this circle
* @param radius - the new radius for our circle
* */
// Your code here
/**
* Get the current radius of our circle
* @return the radius of this circle as a double
*/
// Your code here
/**
* Get the area of our circle as calculated by PI*r*r
* @return the area of the circle
*/
// Your code here
/**
* Get's the perimeter of the circle as 2 * PI * r
* @return the perimeter as a double
*/
// Your code here
/**
* Builds a string containing the information about the circle
* @return String containing the info about the circle
*/
/* Your output should look like this:
This is a circle
Radius: 10.00
Area: 314.16
Perimeter: 62.83
*/
//@Override
// Your code here
/*
* Tests a circle passed in as an argument for equality with
* current circle
* @param Circle c - The Circle to compare to the current
circle
*/
//public boolean equals(Circle c){
// Your code here
//}
}
class CircleTest{
public static void main(String[] args){
/* Uncomment after you complete the no argument constructor
System.out.println("Creating circle1");
Circle circle1 = new Circle();
*/
/* Uncomment after you complete the single argument
constructor
System.out.println("Creating circle2");
Circle circle2 = new Circle(1.0);
System.out.println("Creating circle2");
Circle circle3 = new Circle(10.0);
*/
System.out.println("\n\nTesting methods\n");
/* Uncomment after you write your getters
System.out.println("Testing Getters");
System.out.println("circle1 radius: " + circle1.getRadius());
System.out.println("circle1 area: " + circle1.getArea());
System.out.println("circle1 perimeter: " +
circle1.getPerimeter());
*/
/*Uncomment after you get setters written
System.out.println("\n\nTesting Setters");
System.out.print("Set circle2 radius to 10: ");
circle2.setRadius(10.0);
if (circle2.getRadius() <= 10.0001 &&
circle2.getRadius() >= 9.9999){
System.out.println("Success");
} else {
System.out.println("Failed");
}
*/
/* Uncomment when you complete your toString() method
System.out.println("\n\nTesting toString() method:");
System.out.println(circle2);
*/
/* Uncomment after you write your .equals() method
System.out.println();
System.out.print("\n\nTesting equals() method:
circle1.equals(circle2) (should return false):");
System.out.println(circle1.equals(circle2));
System.out.print("Testing equals() method: circle2.equals(circle3)
(should return true):");
System.out.println(circle2.equals(circle3));
*/
}
}
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// Circle.java
public class Circle {
private double radius;
/**
* No argument constructor. Creates a Circle with
radius 0
* */
// Your code here
public Circle() {
this.radius=0;
}
/**
* Constructor, creates a circle with the provided
radius
*
* @param radius
* - the desired radius of our circle
**/
/**
* @param radius
*/
public Circle(double radius) {
this.radius = radius;
}
/**
* Set new radius for this circle
*
* @param radius
* - the new radius for our circle
**/
public void setRadius(double radius) {
this.radius = radius;
}
/**
* Get the current radius of our circle
*
* @return the radius of this circle as a double
*/
public double getRadius()
{
return radius;
}
/**
* Get the area of our circle as calculated by
PI*r*r
*
* @return the area of the circle
*/
public double getArea()
{
return Math.PI*radius*radius;
}
/**
* Get's the perimeter of the circle as 2 * PI *
r
*
* @return the perimeter as a double
*/
public double getPerimeter()
{
return 2*Math.PI*radius;
}
/**
* Builds a string containing the information about the
circle
*
* @return String containing the info about the
circle
*/
/*
* Your output should look like this: This is a circle
Radius: 10.00 Area:
* 314.16 Perimeter: 62.83
*/
// @Override
// Your code here
@Override
public String toString() {
String s=new String().format("This
is a circle Radius: %.2f Area: %.2f Perimeter: %.2f\n",radius,
getArea(),getPerimeter());
return s;
}
/*
* Tests a circle passed in as an argument for equality
with current circle
*
* @param Circle c - The Circle to compare to the
current circle
*/
public boolean equals(Circle c){
if(this.radius == c.getRadius())
return true;
else
return false;
}
}
==============================================
// CircleTest.java
public class CircleTest {
public static void main(String[] args) {
/*
* Uncomment after you complete the
no argument constructor
*/
System.out.println("Creating
circle1");
Circle circle1 = new
Circle();
/*
* Uncomment after you complete the
single argument constructor
*/
System.out.println("Creating
circle2");
Circle circle2 = new
Circle(1.0);
System.out.println("Creating
circle2");
Circle circle3 =new
Circle(10.0);
System.out.println("\n\nTesting methods\n");
/*
* Uncomment after you write your
getters
*/
System.out.println("Testing
Getters");
System.out.println("circle1 radius:
" + circle1.getRadius());
System.out.println("circle1 area: "
+ circle1.getArea());
System.out.println("circle1
perimeter: " + circle1.getPerimeter());
/*
* Uncomment after you get setters
written
*/
System.out.println("\n\nTesting
Setters");
System.out.print("Set circle2
radius to 10: ");
circle2.setRadius(10.0);
if (circle2.getRadius() <=
10.0001 && circle2.getRadius() >= 9.9999)
{
System.out.println("Success");
}
else
{
System.out.println("Failed");
}
/*
* Uncomment when you complete your
toString() method
*/
System.out.println("\n\nTesting
toString() method:");
System.out.println(circle2);
System.out.println();
System.out.print("\n\nTesting
equals() method: circle1.equals(circle2) (should return
false):");
System.out.println(circle1.equals(circle2));
System.out.print("Testing equals()
method: circle2.equals(circle3) (should return true):");
System.out.println(circle2.equals(circle3));
}
}
==============================================
Output:
=====================Could you plz rate me well.Thank You