Create a class named Blanket with fields for a blanket’s size, color, material,
ID: 3760357 • 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!
Explanation / Answer
package mani;
public class testAccount {
public static void main(String[] args) {
Blanket b=new Blanket();
b.setSize("king");
b.setMaterial("cashmere");
System.out.println(b.toString());
ElectricBlanket e=new ElectricBlanket();
e.setShutoff(true);
e.setColor("green");
e.setNum(4);
System.out.println(e.toString());
e.setNum(8);
System.out.println(e.toString());
}
}
package mani;
public class Blanket {
String size;
String color;
String material;
double price;
public Blanket(){
size="Twin";
color="white";
material="cotton";
price=30.00;
}
public void setSize(String s){
if(s.equalsIgnoreCase("double")){
size=s;
price=price+10;
}else if(s.equalsIgnoreCase("queen")){
size=s;
price=price+25;
}else if(s.equalsIgnoreCase("king")){
size=s;
price=price+40;
}else{
size=s;
}
}
public void setColor(String c){
color=c;
}
public void setMaterial(String m){
if(m.equalsIgnoreCase("wool")){
material=m;
price=price+20;
}else if(m.equalsIgnoreCase("cashmere")){
material=m;
price=price+45;
}else{
material=m;
}
}
public String toString(){
return "Its a "+this.size+" sized blanket which is made up of "+this.material+" and of color "+this.color+" It is priced at "+this.price;
}
}
package mani;
public class ElectricBlanket extends Blanket {
int numHeat;
boolean shutoff;
public ElectricBlanket(){
super();
numHeat=1;
shutoff=true;
}
public void setShutoff(boolean t){
if(t){
price=price+5.75;
}
}
public void setNum(int n){
if(n>0&&n<6){
numHeat=n;
}else{
numHeat=1;
}
}
public boolean getShutt(){
return shutoff;
}
public int getNum(){
return numHeat;
}
public String toString(){
return super.toString()+"Electric blanket has Shuttoff feature "+getShutt()+" Number of heat levels "+getNum();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.