Question

In: Computer Science

1- Create a class called Point that has two instance variables, defined as private, as follows:...

1- Create a class called Point that has two instance variables, defined as private, as follows: An x coordinate and a y coordinate of type integer.

a) Write two constructors for the Point class as follows: A default constructor that sets the class instance variables to zero and a constructor that receives two integer values and sets the instance variables to them.

b) Write the set and get methods for the Point class to set and return the values of its instance variables.

c) Write a toString() method for the Point class that prints the class name (Point) and the value of its instance variables.


2- Create three classes called Circle, Rectangle and Square with the following properties:

- The Circle class has two instance variables – a position of type Point (the class that you just created above) and a radius of type double. The instance variables position and radius specify the position of the center and radius of the circle, respectively.

- The Rectangle class has three instance variables – a position of type Point  (the class that you just created above), a length and a width of type double. The instance variables position, length and width specify the position of the top left corner, length and width of the rectangle, respectively.

- The Square class has two instance variables – a position of type Point (the class that you just created above) and a length  of type double. The instance variables position and length specify the position of the top left corner and length of the square, respectively.

For each of the Circle, Rectangle andSquare classes do the following:

a) Define the instance variables as private.

b) Write two constructors for each class as follows: A default constructor that sets the instance variables to zero and a constructor that receives values for the instance variables and sets them.

c) Write the set and get methods for each class to set and return the values of their instance variables. For example, one of the get methods would return a Point.

d) Write a toString() method for each class that prints the class name (Circle, Rectangle or Square) and the value of its instance variables.

e) Write two methods called getPerimeter() and getArea() for each class, which calculate and return the perimeter and area of the class, respectively. For example, the getArea() method of the Square class returns the area of the Square object.


3- Test the above classes by writing a class called GeometricTest.java that does the following:

- Creates instances of the Circle, Rectangle and Square classes as follows:

- Circle: positioned at x = 7, y = 3 and the radius = 4.5.

- Rectangle: positioned at x = 3, y = -1 and the length = 4.0 and width = 6.0.

- Square: positioned at x = 5, y = 8 and the length = 2.0.

- Prints each of the above objects.

- Changes the length of the Square object to 5.0.

- Prints the perimeter and area of each of each of the above objects.

- Compares the x coordinates of the Square and Rectangle classes and prints a message specifying which one of them has a larger x coordinate.

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Point
{
   private int x;
   private int y;
  
   public Point()
   {
       this.x = 0;
       this.y = 0;
   }
   public Point(int x,int y)
   {
       this.x = x;
       this.y = y;
   }
  
   public int getX()
   {
       return this.x;
   }
   public int getY()
   {
       return this.y;
   }
   public void setX(int x)
   {
       this.x = x;
   }
   public void setY(int y)
   {
       this.y = y;
   }
   public String toString()
   {
       return "Point("+getX() +","+getY()+")";
   }
  
};
class Rectangle extends Point
{
   private Point p;
   private double length;
   private double width;
  
   public Rectangle()
   {
   p.setX(0);
   p.setY(0);
   length = 0;
   width =0;
   }
   public Rectangle(Point p,double length,double width)
   {
       this.p = p;
       this.length = length;
       this.width = width;
   }
   public double getWidth()                   
   {
       return width;
      
   }
   public double getLength()                
   {
     
       return length;
   }
   public Point getPoint()               
   {  
       return this.p;
   }
   public double getArea()                       //compute the area of the rectangle.
   {
       double area;
       area = getWidth() * getLength();
       return area;
   }
   public double getPerimeter()                //compute the perimeter of the rectangle
   {
       double perimeter;
       perimeter = 2*(getWidth()+getLength());
       return perimeter;
   }
   public String toString()
   {
       return "Rectangle : Top left Point :"+ getPoint() +" width :"+getWidth() + " height :"+getLength();
   }
   }

   class Circle extends Point
   {
   private Point p;
   private double radius;
  
  
   public Circle()
   {
   p.setX(0);
   p.setY(0);
   radius =0;
   }
   public Circle(Point p,double radius)
   {
       this.p = p;
       this.radius = radius;
   }
   public double getRadius()                   
   {
       return radius;
      
   }
  
   public Point getPoint()               
   {  
       return this.p;
   }
   public double getArea()                       //compute the area of the circle.
   {
       double area;
       area = 3.14 * getRadius() * getRadius();
       return area;
   }
   public double getPerimeter()                //compute the perimeter of the circle
   {
       double perimeter;
       perimeter = 2* 3.14 *getRadius();
       return perimeter;
   }
   public String toString()
   {
       return "Circle : center Point:"+ getPoint() +" radius "+getRadius();
   }
   }
  
   class Square extends Point
   {
   private Point p;
   private double length;
  
  
   public Square()
   {
   p.setX(0);
   p.setY(0);
   length = 0;
  
   }
   public Square(Point p,double length)
   {
       this.p = p;
       this.length = length;
   }
  
   public double getLength()                
   {
     
       return length;
   }
   public Point getPoint()               
   {  
       return this.p;
   }
   public void setLength(double length)
   {
       this.length = length;
   }
   public double getArea()                       //compute the area of the square.
   {
       double area;
       area = getLength() * getLength();
       return area;
   }
   public double getPerimeter()                //compute the perimeter of the square
   {
       double perimeter;
       perimeter = 4*(getLength());
       return perimeter;
   }
   public String toString()
   {
       return "Square : Top left Point :"+ getPoint() +" length :"+getLength() ;
   }
   }


class GeometricTest
{
   public static void main (String[] args)
   {
       Point pc = new Point(7,3);
       Circle c = new Circle(pc,4.5);

       System.out.println(c.toString());
        System.out.println("Area of circle = "+c.getArea());
        System.out.println("Perimeter of circle = "+c.getPerimeter());
      
      
       Point pr = new Point(3,-1);
       Rectangle r = new Rectangle(pr,4.0,6.0);

       System.out.println(r.toString());
        System.out.println("Area of rectangle = "+r.getArea());
        System.out.println("Perimeter of rectangle = "+r.getPerimeter());
      
      
        Point ps = new Point(5,8);
       Square s = new Square(ps,2.0);

        s.setLength(5.0);
       System.out.println(s.toString());
        System.out.println("Area of square = "+s.getArea());
        System.out.println("Perimeter of square = "+s.getPerimeter());
      
  
        Point p1 = r.getPoint();
        Point p2 = s.getPoint();
      
        if(p1.getX() >p2.getX())
        System.out.println("x-coordinate of rectangle is greater than x-coordinate of square");
        else
        System.out.println("x-coordinate of square is greater than x-coordinate of rectangle");
   }
  
  
}

output:

Circle : center Point:Point(7,3) radius 4.5
Area of circle = 63.585
Perimeter of circle = 28.26
Rectangle : Top left Point :Point(3,-1) width :6.0 height :4.0
Area of rectangle = 24.0
Perimeter of rectangle = 20.0
Square : Top left Point :Point(5,8) length :5.0
Area of square = 25.0
Perimeter of square = 20.0
x-coordinate of square is greater than x-coordinate of rectangle

Related Solutions

Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
Programming Problem 2 - Cycle[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.[B] Edit your class Cycle by...
Create a class Person with two instance variables of type String called firstName and LastName, an...
Create a class Person with two instance variables of type String called firstName and LastName, an instance variable of type int called age, an instance variables of type int called height (measured in inches), and an instance variable of type double called weight. Define an appropriate constructor that takes initial values for all instance variables and calls the corresponding set methods. Define get and set methods for all instance variables. All set (mutator) methods should validate that reasonable data is...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”);...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
1) Create a class called Employee that includes three instance variables — a first name (type...
1) Create a class called Employee that includes three instance variables — a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. 2) Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating...
1) Create a class called Employee that includes three instance variables — a first name (type...
1) Create a class called Employee that includes three instance variables — a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. 2)Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating five...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max // // 2.) A constructor which takes initial values for // min and max // // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT