(Use JAVA) For this program, you need to create three classes, one of which is a main class that includes aggregation from the other two classes. Each of the classes must include at least two fields, multiple constructors, accessor and mutator methods, and toString. Methods in your main class must include: 1. .equals 2. .copy 3. One that passes an object other than the .copy method 4. One that returns an object 5. toString that calls the toStrings from the other classes
class ShapeDemo { public static void main(String[] args) { Triangle triangle1 = new Triangle(3, 4, 5); Triangle triangle2 = new Triangle(); System.out.println("Triangle1 " + triangle1); System.out.println("Triangle2 " + triangle2); triangle2.copy(triangle1); System.out.println("After copy of triangle 1 to triangle 2: " + triangle2); System.out.println(); Rectangle rectangle1 = new Rectangle(2, 3); Rectangle rectangle2 = new Rectangle(2, 3); System.out.println(rectangle1); System.out.println(rectangle2); System.out.println("Equality of rectangle: " + rectangle1.equals(rectangle2)); System.out.println("Rectangle with l = 4, b = 5" + Rectangle.rectangleWith(4, 5)); } } class Triangle { private int a; private int b; private int c; public Triangle(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public Triangle() { this.a = 0; this.b = 0; this.c = 0; } public Triangle(Triangle t) { this.a = t.a; this.b = t.b; this.c = t.c; } public void copy(Triangle t) { this.a = t.a; this.b = t.b; this.c = t.c; } public int getA() { return a; } public int getB() { return b; } public int getC() { return c; } public void setA(int a) { this.a = a; } public void setB(int b) { this.b = b; } public void setC(int c) { this.c = c; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Triangle triangle = (Triangle) o; return a == triangle.a && b == triangle.b && c == triangle.c; } @Override public String toString() { return "Triangle{" + "a=" + a + ", b=" + b + ", c=" + c + '}'; } } class Rectangle { private int length; private int breadth; public Rectangle(Rectangle r) { this.length = r.length; this.breadth = r.breadth; } public Rectangle(int length, int breadth) { this.length = length; this.breadth = breadth; } public Rectangle() { this.length = 0; this.breadth = 0; } public int getLength() { return length; } public int getBreadth() { return breadth; } public void setLength(int length) { this.length = length; } public void setBreadth(int breadth) { this.breadth = breadth; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Rectangle rectangle = (Rectangle) o; return length == rectangle.length && breadth == rectangle.breadth; } @Override public String toString() { return "Rectangle{" + "length=" + length + ", breadth=" + breadth + '}'; } public static Rectangle rectangleWith(int l, int b) { return new Rectangle(l, b); } } |
Get Answers For Free
Most questions answered within 1 hours.