Inheritance and Polymorphism Create a superclass clothing with two variables siz
ID: 2247634 • Letter: I
Question
Inheritance and Polymorphism
Create a superclass clothing with two variables size and color.
Create three subclasses: shirt, pants, socks.
For shirt on the size allow only S,M,L and XL. pants will have no size restrictions, socks also no restriction.
For socks, think about how you use a sock class, like for laundry folder (HINT)
Recall subclasses extend the superclass. Include constructors, getters, setters, and toString().
Write tester to show all three subclasses in action!
Here is an example tester file:
OUTPUT
Create a superclass clothing with two variables size and colo Create three subclasses: shirt, pants, socks For shirt on the size allow only S,M,L and XL. pants will have no size restrictions, socks also no restriction For socks, think about how you use a sock class, like for laundry folder (HINT) Recall subclasses extend the superclass. Include constructors, getters, setters, and toString0 Write tester to show all three subclasses in action! Here is an example tester file public class clothingTesterRuse ( public static void main(String args) { // Test super class only clothing suit = new clothing() clothing jersey = new clothing("medium", "blue") System.out.println("clothing 1:" + suit) System.out.println("clothing 2:" + jersey) System.out.println0 /l Test shirt class clothing blueShirt = new shirt("medium", "blue''); // sets size to null since not S. M. L or XL shirt pinkShirt = new shirt("Short Sleeves''); // shirt class has one field called style pinkShirt.setColor("Pink") pinkShirt.setSize("M") System.out.println("blueShirt" + blueShirt) System.out.println("pinkShirt " + pinkShirt) System.out.println0 // Test pants class pants blackPantsnew pants) blackPants.setColor( Black") blackPants.setSize(M") System.out.println("blackPants " blackPants) System.out.println0 /l Test sock class clothing purpleSocks = new socks("9-10", "Purple"); // socks has field pair, boolean default true in this constructor socks greenSock = new socks("7-8", "Green" ,false) System.out.println(" purpleSocks " purpleSocks) System.out.println("greenSocks" +greenSock)Explanation / Answer
import java.io.*;
class clothing{
private String size;
private String color;
public clothing(){
size = null;
color = null;
}
public clothing(String a , String b){
size = a;
color = b;
}
public void setSize(String a){
size = a;
}
public void setColor(String a){
color = a;
}
public String getSize(){
return size;
}
public String getColor(){
return color;
}
public String toString(){
return "[size=" + getSize()+ "," + "color=" + getColor() + "]";
}
}
class shirt extends clothing{
private String style;
public shirt(String a , String b){
super(a,b);
if (a.equals("S") || a.equals("M") || a.equals("L") || a.equals("XL"))
super.setSize(null);
style = null;
}
public void setSize(String a){
if (a.equals("S") || a.equals("M") || a.equals("L") || a.equals("XL"))
super.setSize(a);
else
super.setSize(null);
}
public shirt(String a){
super(null,null);
style = a;
}
public void setStyle(String a){
style = a;
}
public String getStyle(){
return style;
}
public String toString(){
return "[style=" + getStyle()+ "," +"size=" + getSize()+ "," + "color=" + getColor() + "]";
}
}
class pants extends clothing{
public pants(){
super();
}
}
class socks extends clothing{
private boolean pair;
public socks(String a , String b){
super(a,b);
pair = true;
}
public socks(String a , String b, boolean pr){
super(a,b);
pair = pr;
}
public void setPair(boolean a){
pair = a;
}
public boolean getPair(){
return pair;
}
public String toString(){
return "[pair=" + getPair()+ "," +"size=" + getSize()+ "," + "color=" + getColor() + "]";
}
}
public class DemoClothing {
public static void main(String[] args){
// Test super class only
clothing suit = new clothing();
clothing jersey = new clothing("medium", "blue");
System.out.println("clothing 1:" + suit);
System.out.println("clothing 2:" + jersey);
System.out.println();
// Test shirt class
clothing blueShirt = new shirt("medium", "blue"); // sets size to null since not S, M, L or XL
shirt pinkShirt = new shirt("Short Sleeves"); // shirt class has one field called style
pinkShirt.setColor("Pink");
pinkShirt.setSize("M");
System.out.println("blueShirt " + blueShirt);
System.out.println("pinkShirt " + pinkShirt);
System.out.println();
// Test pants class
pants blackPants = new pants();
blackPants.setColor("Black");
blackPants.setSize("M");
System.out.println("blackPants " + blackPants);
System.out.println();
// Test sock class
clothing purpleSocks = new socks("9-10", "Purple"); // socks has field pair, boolean default true in this constructor
socks greenSock = new socks("7-8", "Green" ,false);
System.out.println("purpleSocks " + purpleSocks);
System.out.println("greenSocks " + greenSock);
}
}