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...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius 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 Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT