Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create a project containing classes Point (you can use one of those discussed in

ID: 3642103 • Letter: C

Question

create a project containing classes Point (you can use one of those discussed in class), Line, and Rectangle. The class Line should extend the class Point, adding the member variable length (assume that a line is horizontal and defined by a point representing its left end and the length length); it should contain appropriate methods setting and getting the length. The class Rectangle should extend Line and have the member variable height – with corresponding methods for setting and getting the height; it should also contain methods for computing the area and the perimeter of a rectangle. All the classes should have appropriate methods toString. Create also a test class (containing main) that would create two objects of Points, Lines, and Rectangles, would compute and print out areas and perimeters of the rectangles, and would print complete information about each object (using appropriate methods toString).
this must be program in java.

Explanation / Answer

public class Point {

       private int x;

       private int y;

       public Point(int x, int y) {

              this.x = x;

              this.y = y;

       }

       public int getX() {

              return x;

       }

       public void setX(int x) {

              this.x = x;

       }

       public int getY() {

              return y;

       }

       public void setY(int y) {

              this.y = y;

       }

       public String toString() {

              return "Point at (" + x + ", " + y + ")";

       }

}

public class Line extends Point {

       private int length;

       public Line(int x, int y, int length) {

              super(x,y);

              this.length = length;

       }

       public int getLength() {

              return length;

       }

       public void setLength(int length) {

              this.length = length;

       }

       public String toString() {

              return "Line at (" + getX() + ", " + getY() + ") with length " + length;

       }

}

public class Rectangle extends Line {

       private int height;

       public Rectangle(int x, int y, int length, int height) {

              super(x,y,length);

              this.height = height;

       }

       public int getHeight() {

              return height;

       }

       public void setHeight(int height) {

              this.height = height;

       }

       public int getArea() {

              return height * getLength();

       }

       public int getPerimeter() {

              return 2 * (height + getLength());

       }

       public String toString() {

              return "Rectangle at (" + getX() + ", " + getY() + ") with length " + getLength()

                           + " and height " + height;

       }

}

public class GeometryTest {

       public static void main(String[] args) {

              Point>new Point(1,2);

              Point two = new Point(2,3);

              Line three = new Line(2,4,5);

              Line four = new Line(3,6,2);

              Rectangle five = new Rectangle(1,5,3,6);

              Rectangle six = new Rectangle(5,2,2,10);

              System.out.println("Area of first rectangle: " + five.getArea());

              System.out.println("Area of second rectangle: " + six.getArea());

              System.out.println("Perimeter of first rectangle: " + five.getPerimeter());

              System.out.println("Perimeter of second rectangle: " + six.getPerimeter());

              System.out.println(one.toString());

              System.out.println(two.toString());

              System.out.println(three.toString());

              System.out.println(four.toString());

              System.out.println(five.toString());

              System.out.println(six.toString());

       }

}