Please help me writing the code from this pseudocode. #Class Material # Private
ID: 3734318 • Letter: P
Question
Please help me writing the code from this pseudocode.
#Class Material
# Private String _type
# Public Module Material(String type)
# _type = type
# End Module
# Public Module display()
# Display _type
# End Module
# End Class
# Class Furniture
# Private Material _material
# Private String _type
# Public Module set_material(Material material)
# Set _material = material
# End Module
# Public Module display()
# Display _type
# Call _material.display()
# End Module
# End Class
# Class Chair Extends Furniture
# Public Module Chair()
# Set _type = "Chair"
# End Module
# End Class
# Module main()
# Declare Furniture furniture = New Chair()
# Call furniture.set_material(New Material("Wood"))
# Call furniture.display()
# End Module
#What will be the output when the main module is called? Separate each output with a single space.
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the pseudocode. [added one more setter method in Furniture to set the furniture type, or else it cannot be set from Chair class as the furniture type is private ]. Comments are also included in every statements to let you understand things easily. If you have any doubts, feel free to ask, Thanks
//Material class
public class Material {
//defining String variable for material type
private String type;
/**
* constructor to initialize material type
* @param type- type of material
*/
public Material(String type) {
this.type = type;
}
/**
* method to display the type
*/
public void display(){
System.out.println("Material type: "+type);
}
}
// Furniture class
public class Furniture {
//Material object
private Material material;
//Furniture type
private String type;
/**
* method to set material
* @param material - material to be set
*/
public void setMaterial(Material material) {
this.material = material;
}
/**
* method to set the furniture type
* @param type - type of furniture
*/
public void setType(String type) {
this.type = type;
}
/**
* method to display furniture details
*/
public void display(){
System.out.println("Furniture type: "+type);
//calling display method of material
material.display();
}
}
// Chair class
public class Chair extends Furniture{
/**
* Default constructor
*/
public Chair() {
//setting furniture type as chair
setType("Chair");
}
}
//Main class (which contains the main method)
public class Main {
public static void main(String[] args) {
/**
* Defining a furniture
*/
Furniture furniture=new Chair();
/**
* Setting the material
*/
furniture.setMaterial(new Material("Wood"));
/**
* Displaying the furniture
*/
furniture.display();
}
}
/*OUTPUT*/
Furniture type: Chair
Material type: Wood
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.