Purpose: Learn more about inherited, protected visibility, enums and the \"super
ID: 3812509 • Letter: P
Question
Purpose: Learn more about inherited, protected visibility, enums and the "super" keyword Directions: Use Ellipse Create an enum called clothing type Give it three values different types of clothing Create a super class called Clothing Inside this class create a private field of type Clothing Type Also create a private field called is Wearing that will hold a values indicating if we are currently wearing the clothing or not Create an expert constructor that takes a Clothing Type parameter and sets the appropriate fields Create a protected method called wear that sets the is wearing field to true Create a protected method called takeoff that sets the is wearing Held to false Create 3 subclasses, each representing a different type of clothing For each subclass there should be a default constructor. This constructor won't compile unless we explicitly call the constructor of the inherited class and pass It an appropriate value. We should override the method wear Make sure to call the inherited class's version of the wear method print out what the user is wearing (like "You just wore a shirt" or something) make sure to change the visibility of the method to public This is Important if we are to call the method from another class We should override the method takeoff same as method wear call the inherited class's method print out a custom message change visibility of the method to public Create a class called Main Put the main method inside of it Create an object whose compile time type is Clothing but whose runtime type is determined by the user Ask the user to select an item of clothing and present them with a menu that contains all the clothing items you created classes and enum values for above o Based on what the user gives you, initialize the object to be of the appropriate runtime type o call the wear method on the object (ONLY do this ONCE. This method should not be inside of your if or switch statement that analyzes user input) Now call the takeoff methodExplanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
*
* @author Sam
*/
public class Main { // main class conaining driver
public static void main(String[] args) throws IOException {
Clothing clothing = null;
System.out.println("1. Shirt"); //show options
System.out.println("2. Polo");
System.out.println("3. Jacket");
System.out.println("Enter options: ");
int ch = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine()); //get option
if (ch == 1)
clothing = new ClothingShirt(); //create shirt object
else if (ch == 2)
clothing = new ClothingPolo(); //create polo object
else if (ch == 3)
clothing = new ClothingJacket(); //create jacket object
clothing.wear(); //as intsructed, wear and takeoff
clothing.takeoff();
}
}
enum ClothingType { /* I cannot get any clothing type right now */
Shirt,
Polo,
Jacket
}
class Clothing {
private ClothingType clothingtype;
boolean isWearing;
public Clothing(ClothingType clothingtype) { //no default constructor
this.clothingtype = clothingtype;
}
protected void wear() { //wear of super calss
isWearing = true;
}
protected void takeoff() {
isWearing = false;
}
}
class ClothingShirt extends Clothing { //class for shirt
public ClothingShirt() {
super(ClothingType.Shirt);
}
@Override
public void wear() { //look!! public visibility
super.wear();
System.out.println("You just wore a shirt");
}
@Override
public void takeoff() {
super.takeoff();
System.out.println("You just took off a shirt");
}
}
class ClothingPolo extends Clothing { //class for polo
public ClothingPolo() {
super(ClothingType.Polo);
}
@Override
public void wear() {
super.wear();
System.out.println("You just wore a polo");
}
@Override
public void takeoff() {
super.takeoff();
System.out.println("You just took off a polo");
}
}
class ClothingJacket extends Clothing { //class for jacket
public ClothingJacket() {
super(ClothingType.Jacket);
}
@Override
public void wear() { //access specifier changed to public
super.wear();
System.out.println("You just wore a jacket");
}
@Override
public void takeoff() { //for takeoff
super.takeoff();
System.out.println("You just took off a jacket");
}
}
I coded the program for you. I hope you like the answer. If you face any difficulty, please let me know int the comment section below
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.