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

JAVA HOMEWORK Question 3: Write a Child Class The class below describes a Shirt

ID: 3673532 • Letter: J

Question

JAVA HOMEWORK

Question 3: Write a Child Class

The class below describes a Shirt object. Write a class that inherits from Shirt called Sweatshirt that is also described by whether or not it is hooded. Write instance data variables, one or more constructors, getters and setters, and a toString method.

Question 4: The equals Method

Write an equals method for the Shirt class. Two shirts are logically equivalent if they have the same size, color, and price.


public class Shirt {

private Size size;
private String color;
private double price;

enum Size { SMALL, MEDIUM, LARGE }

public Shirt(Size size, String color, double price) {

this.size = size;
this.color = color;
this.price = price;

}

public Size getSize() {

return size;

}

public String getColor() {

return color;

}
public double getPrice() {

return price;

}
public void setSize(Size size) {

this.size = size;

}
public void setColor(String color) {

this.color = color;

}
public void setPrice(double price) {

if(price > 0) {

this.price = price;

}

}

@Override
public String toString() {

return "Size = " + size + " " +
"Color = " + color + " " +
"Price = " + price + " ";

}

}

Question 6: enum

Note that enums were covered last week, but a few people mentioned wanting some more practice on these, so I am including another question here this week.

Write an enum to represent a school semester- fall, spring, and summer. The enum is also described by the number of weeks (fall and spring have 18, summer has 6). Override the toString to print output like "Fall Semester (18 weeks)".

Create three instances of the enum: sanMateoFall, sanMateoSpring, sanMateoSummer. Sum up the weeks covered in these three semesters and print that value to the console.

Explanation / Answer

Sweatshirt.java

public class Sweatshirt extends Shirt {

   private boolean isHooded;

   public Sweatshirt(Size size, String color, double price, boolean isHooded) {
       super(size, color, price);
       this.isHooded = isHooded;
   }

   public boolean isHooded() {
       return isHooded;
   }

   public void setIsHooded(boolean isHooded) {
       this.isHooded = isHooded;
   }

   @Override
   public String toString() {
       return "Size = " + size + " " +
"Color = " + color + " " +
"Price = " + price + " " +
"Is Hooded = " + isHooded + " ";
   }
}

Shirt.java

public class Shirt {
private Size size;
private String color;
private double price;

enum Size { SMALL, MEDIUM, LARGE }

public Shirt(Size size, String color, double price) {
this.size = size;
this.color = color;
this.price = price;
}

public Size getSize() {
return size;
}

public String getColor() {
return color;
}

public double getPrice() {
return price;
}

public void setSize(Size size) {
this.size = size;
}

public void setColor(String color) {
this.color = color;
}

public void setPrice(double price) {
if(price > 0) {
this.price = price;
}
}

@Override
public String toString() {
return "Size = " + size + " " +
"Color = " + color + " " +
"Price = " + price + " ";
}

public boolean equals(Shirt object) {
return object.getSize().equals(this.size)
&& object.getColor().equals(this.color)
&& (object.getPrice() == this.price);
}
}

"Semester Enum"

public enum Semester {
       FALL(18),
       SPRING(18),
       SUMMER(6);

       private int numberOfWeeks;

       Semester(int numberOfWeeks) {
           this.numberOfWeeks = numberOfWeeks;
       }

       @Override
       public String toString() {
           switch(this) {
               case FALL:
                   return "Fall Semester ("+ this.numberOfWeeks + " weeks)";
               case SPRING:
                   return "Spring Semester (" + this.numberOfWeeks + " weeks)";
               case SUMMER:
                   return "Summer Semester (" + this.numberOfWeeks + "weeks)";
               default:
                   return "N/A";
           }
       }
   }