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 lengthof type double. The instance variables position andlength 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
spec.
1- Create a class called Point that has two instance variables, defi.pdf
1. 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 lengthof type double. The instance variables position andlength 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.
2. 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.
Solution
import java.util.*;
import java.lang.*;
import java.io.*;
class Point
3. {
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()+")";
}
4. };
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.
{
5. 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;
6. }
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;
7. }
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() ;
8. }
}
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");