Question: import java.io.*; public class Main { public static void main(String[] args) { // object creation test Mammal m1 = new Mammal(false, terrestrial, 4, herbivore); System.out.println(m1);
![import java.io.*; public class Main { public static void main(String[] args)](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f457ead6f00_32266f457ea56241.jpg)
import java.io.*;
public class Main {
public static void main(String[] args) {
// object creation test
Mammal m1 = new Mammal(false, "terrestrial", 4, "herbivore"); System.out.println(m1);
Mammal m2 = new Mammal(false, "aquatic", 0, "carnivore"); System.out.println(m2);
Horse h1 = new Horse("brown"); Horse h2 = new Horse("brown"); Horse h3 = new Horse("white");
System.out.println(h1.equals(h2)); System.out.println(h1.equals(h3));
// Code for Part 2:
Animal a1 = new Animal("omnivore", 10); System.out.println(a1);
Animal a2 = new Animal("carnivore", 8); System.out.println(a2);
Animal a3 = new Animal("herbivore", 4); System.out.println(a3);
Animal a4 = new Animal("carnivore", 30); System.out.println(a4);
Animal a5 = new Animal("omnivore", 2); System.out.println(a5);
} }
//**//
public class Animal {
private String diet;
private int numOfLegs;
public Animal(){}
public Animal(String diet, int legs) { setDiet(diet); setNumOfLegs(legs); }
public String getDiet() { return diet; }
public int getNumOfLegs() { return numOfLegs; }
public void setDiet(String diet) { isValidDiet(diet); this.diet = diet; }
public void setNumOfLegs(int numOfLegs) { isValidNumOfLegs(numOfLegs); this.numOfLegs = numOfLegs; }
// check methods public boolean isValidDiet(String diet) { if(diet.equalsIgnoreCase("herbivore") || diet.equalsIgnoreCase("carnivore") || diet.equalsIgnoreCase("omnivore")) { return true; } System.out.println("Diet not valid: An animal may only be an herbivore, carnivore or omnivore."); System.exit(1); return false; }
public boolean isValidNumOfLegs(int legs) { if(legs >= 0) { return true; } System.out.println("Number of legs not valid: An animal can't have a negative number of legs!"); System.exit(1); return false; }
// equals method public boolean equals(Animal o) { return diet.equalsIgnoreCase(o.diet) && numOfLegs == o.numOfLegs; }
// toString method public String toString() { return "Animal has " + numOfLegs + " legs and is a(n) " + diet; } }
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(
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
