Question: public class Main { public static void main(String[] args) { Triangle t1 = new Triangle(); System.out.println(t1); Triangle t2 = new Triangle(4, 5, 6, purple); System.out.println(t2);
![public class Main { public static void main(String[] args) { Triangle](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f4e6eae47c2_93066f4e6ea660cb.jpg)


public class Main {
public static void main(String[] args) {
Triangle t1 = new Triangle(); System.out.println(t1);
Triangle t2 = new Triangle(4, 5, 6, "purple"); System.out.println(t2);
Triangle t3 = new Triangle(4, 5, 6, "purple"); System.out.println(t3);
System.out.println("t1 equals t2: " + t1.equals(t2)); System.out.println("t2 equals t3: " + t2.equals(t3));
t3.setStriped(true); System.out.println(t3.getStriped()); } } //**//
public class Shape { // instance variables, attributes private static int numOfShapes = 0; private int numOfSides; private String color; private boolean striped;
// constructors public Shape() { this(4, "pink", false); } public Shape(int num) { numOfSides=num; numOfShapes++; } public Shape(int num, String color, boolean striped) { // numOfSides = num; this(num); setColor(color); this.striped = striped; numOfShapes++; }
public int getNumOfShapes() { return numOfShapes; }
// get/accessor methods public int getNumOfSides() { return numOfSides; } public String getColor() { return color; } public boolean getStriped() { return striped; }
// set methods public void setNumOfSides(int num) { numOfSides = num; } public void setColor(String color) { if(checkColor(color)) { this.color = color; } else { System.out.println("Color not valid"); System.exit(0); } } public void setStriped(boolean striped) { this.striped = striped; }
// check methods public boolean checkColor(String color) { //blue for(int i = 0; i // toString method public String toString() { return "Shape has " + numOfSides + " sides, is " + color + " and has stripes: " + striped; } } //**// public class Circle extends Shape { // instance variables private double radius; // constructor public Circle() { super(0, "pink", true); radius = 0; } public Circle(double r, String color, boolean striped) { super(0); radius = r; super.setColor(color); super.setStriped(striped); } public Circle(double r, int numOfSides, String color, boolean striped) { super(numOfSides, color, striped); radius = r; } // get and set methods public double getRadius() { return radius; } public void setRadius(double r) { radius = r; } // equals method public boolean equals(Circle o) { if(radius == o.radius && super.equals(o)) { return true; } return false; } // toString method public String toString() { String color = super.getColor(); // String result = super.toString(); return "Radius = " + radius + ", Color = " + color; } } //**// public class Rectangle extends Shape { // instance variables private double width; private double length; // constructors public Rectangle() { super(4, "pink", false); width = 5; length = 20; } public Rectangle(double width, double length) { super(4, "cyan", true); this.width = width; this.length = length; } // get methods public double getWidth() { return width; } public double getLength() { return length; } // set methods public void setWidth(double w) { width = w; } public void setLength(double l) { length = l; } // equals method public boolean equals(Rectangle o) { if(this.length == o.length && this.width == o.width && super.equals(o)) { return true; } return false; } // toString public String toString() { // String result = super.toString(); // return result + ", length = " + length + ", width = " + width; boolean striped = super.getStriped(); String color = super.getColor(); return "Rectangle is " + color + " and has a length of " + length + " and has a width of " + width; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
