Question

In: Computer Science

JAVA: Create a circle that contains the following... Sample code to work from provided below. FIELDS:...

JAVA: Create a circle that contains the following... Sample code to work from provided below.

FIELDS:

  • a private double that holds the radius.

CONSTRUCTORS

  • a no argument constructor that sets the radius to zero
  • a constructor that takes a single argument of type double which is the radius.

METHODS

  • a method called "getRadius" which returns the current radius
  • a method called "setRadius" which takes a single parameter of type double containing the new radius
  • a method called "getArea" which returns the area of the circle (area = pi times radius squared)
  • a method called "getPerimeter" which returns the perimeter of the circle (perimeter = 2*pi*r)
  • a method called "toString" which returns the information about the radius as shown below
  • a method called "equals" which takes a Circle as an argument and returns a boolean indicating if the two Circles are equivalent (in this case that just means they have the same radius.

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));
*/
}
}

Solutions

Expert Solution

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


Related Solutions

Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of size 4 in a driver class using the following statement: Circle circleArr[] = new Circle[4]; Populate the array with four different radiuses and then, using a for loop from 0 to one less then the length of the array, print the area and the diameter of each of the four circles Circle Class: import java.text.DecimalFormat; public class Circle {    DecimalFormat dec = new...
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class RealestateFinder.java that reads in real estate data from a CSV file (RealestateList.csv). Then provide the user with the following options: Sort per Price, Sort per Location, and Exit. Use ArrayList and Arrays.sort to perform the sorts and display the data to the user.
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods:...
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods: ArrayPair - arr1 [] : int - arr2 [] : int Fields - length1 : int - length2 : int + ArrayPair (int arraySize) Methods + add (int arrNumber, int value) : void + remove (int arrNumber) : void + equal () : boolean + greater () : boolean + print () : void Thus, there are two arrays of integers in the class...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
only JAVA code /** Create a method as instructed below and then call it appropriately. */...
only JAVA code /** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class MoreBankCharges { //Constant declarations for base fee and per check fees //Class scope so constants are accessible by all methods static final double BASE_FEE = 10.0; static final double LESS_THAN_20_FEE = 0.10; static final double TWENTY_TO_THIRTYNINE_FEE = 0.08; static final double FORTY_TO_FIFTYNINE_FEE = 0.06; static final double SIXTY_OR_MORE_FEE = 0.04; public static void main(String[] args) { //Variable declarations int numChecks;...
Java Data Structures (Stack and Recursion) Using the CODE provided BELOW (WITHOUT IMPORTING any classes from...
Java Data Structures (Stack and Recursion) Using the CODE provided BELOW (WITHOUT IMPORTING any classes from Java Library) modify the classes and add the following methods to the code provided below. 1. Add a recursive method hmTimes() to the CODE BELOW that states how many times a particular value appears on the stack. 2. Add a recursive method insertE() to the CODE BELOW that allows insert a value at the end of the stack. 3. Add a recursive method popLast()...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT