Purpose: Create a java program from detailed directions. a. Create a class named
ID: 3761227 • Letter: P
Question
Purpose: Create a java program from detailed directions.
a. Create a class named Blanket with fields for a blanket’s size, color, material, and price. Include a constructor that sets default values for the fields as Twin, white, cotton, and $30.00. Include a set method for each of the first three fields. The method that sets size adds $10 to the base price for a double blanket, $25 for a queen blanket, and $40 for a king. The method that sets the material adds $20 to the price for wool and $45 to the price for cashmere. In other words, the price for a king-sized cashmere blanket is $115. Whenever the size or material is invalid, reset the blanket to the default values. Include a toString() method that returns a description of the blanket. Save the file as Blanket.java. b. Create a child class named ElectricBlanket that extends Blanket and includes two additional fields: one for the number of heat settings and one for whether the electric blanket has an automatic shutoff feature. Default values are one heat setting and no automatic shutoff. Include get and set methods for the fields. Do not allow the number of settings to be less than one or more than five; if it is, use the default setting of 1. Add a $5.75 premium to the price if the blanket has the automatic shutoff feature. Also include a toString() method that calls the parent class toString() method and combines the returned value with data about the new fields to return a complete description of features. Save the file as ElectricBlanket.java. c. Create an application that declares a blanket of each type and demonstrates how the methods work. Save the file as DemoBlankets.java.
Ability to: Follow established programming conventions for writing java programs Understand inheritance To create a parent class To create a child class To create a method that calls the parent method and combines both To create both parent and child objects To display both types of objects created.
Explanation / Answer
Program code:
Blanket.java
public class Blanket {
public String size,color,material;
public double price;
public Blanket()
{
this.size="twin";
this.color="white";
this.material="cotton";
this.price=30.00;
}
//Default constructor with default values assigned to each varaible
public Blanket(String size,String color,String material,double price)
{
this.size=size;
this.color=color;
this.material=material;
this.price=price;
}
//This method is used to set value for size and based on the side provided it adds value for the price
public void setSize ( String s ) {
size =s ;
if(size=="double"){
price=price+10.00;
}
else if(size=="queen"){
price=price+25.00;
}
else if(size=="king"){
price=price+40.00;
}
else{
new Blanket(size,color,material,price);
}
}
//Method to set the color for the blanket
public void setColor(String c){
color=c;
}
//This method is used to set material for the blanket and based on the material provided it adds value for the price
public void setMaterial ( String m ) {
material =m ;
if(material=="wool"){
price=price+20.00;
}
else if(material=="cashmere"){
price=price+45.00;
}
else{
new Blanket(size,color,material,price);
}
}
//This is setter method used to get the size of the blanket.
public String getSize() {
return size;
}
//This is a setter method used to get the color of the blanket.
public String getcolor(){
return color;
}
//This is a setter method used to get the color of the blanket.
public String getMaterial (){
return material;
}
//this is s toString method that is used to return the description of the blanket
public String toString(){//overriding the toString() method
return size+" "+color+" "+material+" "+price;
}
}
ElectricBlanket.java:
public class ElectricBlanket extends Blanket
{
public static int no_of_heat_settings;
public static String automatic_shutoff;
//Default constructor to set default values for the varaibles
public ElectricBlanket()
{
super();
no_of_heat_settings = 0;
automatic_shutoff = "No";
}
public ElectricBlanket(String size,String color,String material,double price)
{
super(size,color,material,price);
no_of_heat_settings = 0;
automatic_shutoff = "No";
}
//This method is used to set value for no_of_heat_settings and based on the no_of_heat_settings provided it adds value for the price
public void setHeat ( int h ) {
no_of_heat_settings=h;
/*if(no_of_heat_settings>=5){
new ElectricBlanket (no_of_heat_settings,automatic_shutoff);
}*/
price=price+ 5.75;
}
//This method is used to set values for automatic_shutoff which is either yes or no
public void setAutomatic(String a){
System.out.println("The value should be either yes or no");
automatic_shutoff =a;
}
//This is a getter method to get the values for the variable no_of_heat_settings
public int getHeat(){
return no_of_heat_settings;
}
//This is a getter method to get the values for the variable automatic_shutoff
public String getAutomatic(){
return automatic_shutoff;
}
public String toString(){//overriding the toString() method of the superclass
return size+" "+color+" "+material+" "+price+" "+no_of_heat_settings+" "+automatic_shutoff;
}
}
ElectricBlanketTest.java
public class ElectricBlanketTest {
public static void main(String[] args) {
// Blanket b1=new Blanket(color, material, size, price);
System.out.println("Blanket Demo class");
Blanket b1=new Blanket("twin","red","cotton",10);
System.out.println(b1.toString());
b1.setMaterial("wool");
System.out.println(b1.toString());
ElectricBlanket e1= new ElectricBlanket("twin","red","cotton",10);
e1.setAutomatic("yes");
e1.setHeat(3);
e1.setMaterial("cashmere");
System.out.println("Electric blanket");
System.out.println(e1.toString());
}
}
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.