Question

In: Computer Science

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 you create these objects using the constructor. Print out these circle object using toString method.

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Below java classes are created.

circle.java :

//Java class
public class circle {
   // fields
   private int x;
   private int y;
   private double radius;
   //constructor
   public circle(int x,int y,double radius)
   {
       this.x=x;
       this.y=y;
       this.radius=radius;
      
   }
  
   // method to return radius
   public double getRadius() {
       return this.radius;// return radius
   }

   // method to return x -coordinate
   public int getX() {
       return this.x;// return x
   }

   // method to return y -coordinate
   public int getY() {
       return this.y;// return y
   }
   //method to calculate area of circle
   public double area ()
   {
       //return area of circle
       return Math.PI*getRadius()*getRadius();
   }
   //method to return perimeter
   public double perimeter()
   {
       //return perimeter
       return 2*Math.PI*getRadius();
   }
   //toString() method
   public String toString()
   {
       //return area of circle and perimeter
       return "Area : "+String.format("%.2f", this.area())+", Perimeter :"+String.format("%.2f",this.perimeter());
   }

}

****************************

CircleClient.java :

//Java class
public class CircleClient {

   //entry point of program , main method
   public static void main(String[] args) {
       //creating object of circle class
       circle c1=new circle (5,6,7);
       circle c2=new circle (7,8,9);
       System.out.println(c1.toString());//print c1 details
       System.out.println(c2.toString());//print c2 details   

   }

}

======================================================

Output : Compile and Run CircleClient.java to get the screen as shown below

Screen 1 :CircleClient.java

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields Item Name Item Serial No Item Unit Price Item Stock Level Item Reorder Level It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level. b) Extend the Retail Item class of part a) above...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields  Item Name  Item Serial No  Item Unit Price  Item Stock Level  Item Reorder Level  It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level.    b) Extend...
Write a Circle Class that has the following fields: • radius: a double • PI: a...
Write a Circle Class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 • Constructor. Accepts the radius of the circle as an argument • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI *...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
Create a class, called Song. Song will have the following fields:  artist (a string) ...
Create a class, called Song. Song will have the following fields:  artist (a string)  title (a string)  duration (an integer, recorded in seconds)  collectionName (a string) All fields, except for collectionName, will be unique for each song. The collectionName will have the same value for all songs. In addition to these four fields, you will also create a set of get/set methods and a constructor. The get/set methods must be present for artist, title, and duration....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT