Question
In this exercise, practice Inheritance. Remember that in Java, inheritance is having one class that is a specialized version of another, more generic, class. It
In this exercise, practice Inheritance. Remember that in Java, inheritance is having one class that is a specialized version of another, more generic, class. It identifies a "kind-of" relationship, or "is-a". Sweatshirts are Clothing, right? So let's write some code to demonstrate this using inheritance in Java. First, fix any issues noted in the feedback for exercises 3 & 4 (you can find the instructions in the "Exercises" Content section). Write a Sweatshirt class with attributes for: the size (example values could be: S, M, L, XL, 8X, etc.) whether or not the Sweatshirt has a hood Don't forget that if the constructor in your Clothing class has parameters, that your Sweatshirt class constructor must pass values to it. Next, you'll want to override the toString from the Clothing class to return Sweatshirt specific information as well when the object is printed. Here's an example: XL hooded stargate hoodie, blue $16.88 XXXL Clayton State sweatshirt, orange $ 9.99 The first sweatshirt had a hood, the second one did not. You can use a simple if statement in your toString to determine how you'd like that to be displayed. In your driver, create a 2 Sweatshirt objects, one with a hood and one without, then print them.
public class Clothing { // Instance variables private String color; private String dispalyName; private double price;
/** * @param color * @param dispalyName * @param price */ public Clothing(String color, String dispalyName, double price) { this.color = color; this.dispalyName = dispalyName; this.price = price; }
public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Clothing other = (Clothing) obj; if (color == null) { if (other.color != null) return false; } else if (!color.equals(other.color)) return false; if (dispalyName == null) { if (other.dispalyName != null) return false; } else if (!dispalyName.equals(other.dispalyName)) return false; if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) return false; return true; }
public String toString() { return dispalyName + ": " + color + ", " + price; } /** * main method for testing * @param args */ public static void main(String[] args) { // Create an object of Clothing Clothing clothing1 = new Clothing("orange", "T-Shirt", 19.87); // Call toString method System.out.println(clothing1.toString()); // Create an object of Clothing Clothing clothing2 = new Clothing("orange", "T-Shirt", 19.87); // Call equals method System.out.println(clothing1.equals(clothing2)); // Create an object of Clothing Clothing clothing3 = new Clothing("red", "T-Shirt", 19.87); // Call equals method System.out.println(clothing1.equals(clothing3)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started