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, 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...
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;...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
JAVA Write code which takes three decimal inputs from the user, creates a circle with a...
JAVA Write code which takes three decimal inputs from the user, creates a circle with a radius equal to the first input and a rectangle with length and width equal to the second and third input respectively, then prints both of these shapes. Sample run: Type a radius: 3.7 Type a length: 4.9 Type a width: 8.6 circle with radius 3.7 rectangle with length 4.9, width 8.6
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT