Skip to content

4.12.4 - Clothing Store

Java (Latte)

Starter and Solution Code

Clothing.java

ClothingTester.java

Jeans.java

Sweatshirt.java

TShirt.java

Did you attempt the exercise before looking at the solution?

We strongly suggest that you try the exercise yourself and use the starter code before looking at the solution code.

Clothing.java
public class Clothing
{
    // Your code here

    // Instance variables for size and color
    private String clothingSize;
    private String clothingColor;

    // Getter method for size
    public String getSize()
    {
        return clothingSize;    
    }

    // Getter method for color
    public String getColor()
    {
        return clothingColor;    
    }

    // Constructor
    public Clothing(String size, String color)
    {
        clothingSize = size;
        clothingColor = color;
    }
}
ClothingTester.java
1
2
3
4
5
6
7
public class ClothingTester extends ConsoleProgram
{
    public void run()
    {
        // Start here!
    }
}
Jeans.java
1
2
3
4
5
6
7
public class Jeans extends Clothing
{
    public Jeans(String size)
    {
        super(size, "blue");
    }
}
Reminder about the this. keyword

The prefix this. can be used to indicate that the instance variable is being used, not the formal parameter.

Sweatshirt.java
public class Sweatshirt extends Clothing
{
    private boolean clothingHasHood;

    public boolean hasHood()
    {
        return this.clothingHasHood;    
    }

    public Sweatshirt(String size, String color, boolean hasHood)
    {
        super(size, color);
        this.clothingHasHood = hasHood;
    }
}
TShirt.java
public class TShirt extends Clothing
{   
    private String clothingFabric;

    public String getFabric()
    {
        return this.clothingFabric;
    }

    public TShirt(String size, String color, String fabric)
    {
        super(size, color);
        this.clothingFabric = fabric;
    }   
}

Prompt (CodeHS)

In this problem, you’ll design a few classes that represent different pieces of clothing in a clothing store.

You’ll write the classes for TShirt, Jeans, Sweatshirt and Clothing.

The Clothing class should have two instance variables: one for the size of the clothing (a String), and another for the clothing’s color (also a string).

Clothing should have two accessor (getter methods) as well:

public String getSize()
public String getColor()

The Sweatshirt class should have a private instance variable (or field) to store whether or not it has a hood, and a corresponding getter method

public boolean hasHood()

The TShirt class should have a private field to store the fabric and a corresponding getter for that called

public String getFabric()
All Jeans should have the color blue.

The constructors should be of this format:

public Clothing(String size, String color)
public TShirt(String size, String color, String fabric)
public Sweatshirt(String size, String color, boolean hasHood)
public Jeans(String size)