Create a class named Blanket with fields for a blanket’s size, color, material,
ID: 3760474 • Letter: C
Question
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.
I am having difficulty with this assignment. This is a beginner Java class so please keep simple and explain each step. Thanks!
Also I have posted this question and this part inside the blanket class was not coded (Whenever the size or material is invalid, reset the blanket to the default values) and the rest of the code that was written was too advanced for this beginner class
Explanation / Answer
Hi,
Below is the solution to your problem.
Blanket.java:
package java_project;
public class Blanket {
public String size,color,material;
public double price;
//Default constructor with default values assigned to each varaible
public Blanket(String size,String color,String material,double price)
{
size="twin";
color="white";
material="cotton";
price=30.00;
}
//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=10.00;
}
else if(size=="queen"){
price=25.00;
}
else if(size=="king"){
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:
package java_project;
public class ElectricBlanket extends Blanket {
public int no_of_heat_settings;
public String automatic_shutoff;
//Default constructor to set default values for the varaibles
public ElectricBlanket(int no_of_heat_settings,String automatic_shutoff ){
super();
no_of_heat_settings=1;
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 no_of_heat_settings+" "+automatic_shutoff;
}
}
DemoBlanket.java:
package java_project;
public class DemoBlankets {
public static void main(String[] args) {
Blanket b1=new Blanket();
System.out.println("Blanket Demo class");
b1.Blanket("twin","red","cotton",10);
b1.setMaterial("wool");
}
}
I have just implemented the DemoBlanket class based on my knowledge as you have not mentioned what you wnat in it.You can add any modifictations that you wish into it.
Hope that helps...HAPPY ANSWERING!!!!!!!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.