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

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

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; } }

\begin{tabular}{|l|} \hline \multicolumn{1}{|c|}{ Triangle } \\ \hline -side1: double \\ -side2: double \\ -side3: double \\ \hline +Triangle() \\ +Traingle(s1: double, s2: double, s3: double, String color) \\ +getSide10): double \\ +getSide2(): double \\ +getSide3(): double \\ +setSide1(s1: double) \\ +setSide2(s2: double) \\ +setSide3(s3: double) \\ +isValid(s1:double, s2:double, s3:double): boolean \\ +getPerimeter(): double \\ +equals(triangle: Triangle): boolean \\ +toString():String \\ \hline \end{tabular} 1. Download the Main, Shape, Rectangle and Circle class under the latest module. Create the Triangle class below. Rectangle, Circle, and Triangle are all subclasses of Shape. a. Your default constructor should set the color to pink and striped to false. Each side should be set to 0 . b. For the isValid method, a triangle is valid if the sum of its two sides is greater than the third side. - A side should not be set if the Triangle would not be valid with the change. - A new Triangle object should not be created if the Triangle is not valid. c. The equation for finding the perimeter of a Triangle is: perimeter=side1+side2+side3 d. For the equals method, two Triangles are equal if all sides are equal and if all of the conditions in the Shape class's equals method are met. (HINT: use super) e. The toString should print out the perimeter and color of the Triangle object. f. Run your program. I added a test program in the main header. g. There is a Triangle object, called t1 : Triangle t1= new Triangle0; Which method calls will work and which will not? Try running each. - t1.toString0; - t1.getWidth(; - t1.getSide10; - t1.getNumOfSides(; Create an array of 5 Triangles each with values of your choosing. Display the array. The code for selection sort for sorting a double array can be found under the lab folder. Please take this code and alter the method so it can sort a character array (remember, characters has an ascii value that is just an int essentially). Create another method called sortTriangles. This method should take in an array of Triangles and sort Triangles by the value of side1 in ascending order (least to greatest). Use the same selection sort code again and alter it to work for this scenario. Use the method on your array of Triangles. Display the array

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!