// (A) Blanket.java: package java_project; public class Blanket { public String
ID: 3762275 • Letter: #
Question
// (A) 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;
}
}
// (B) 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;
}
}
// (C) 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");
}
}
Hello,
Can you please review for me the solution for this problem and provide a correct answer?
The Java file is regarding blankets (a), electric blankets (b) and a demo file (c). Book: Java Programming, Joyce Farrell, 8th Edition, Ch.11, Exercise 6 a,b,c, pg. 586-587.
There is no solution for it listed in the textbook solutions and the other solutions I retrieved on Chegg are not complete or incorrect. Thank you for your help!
Explanation / Answer
This Code is Working Now
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 s,String c,String m,double p)
{
size=s;
color=c;
material=m;
price=p;
}
//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;
}
}
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 ){
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;
}
}
package java_project;
public class DemoBlanket {
public static void main(String[] args) {
Blanket b1=new Blanket("twin","red","cotton",10);
System.out.println("-----------------Test Case Starts From Here-----------");
b1.setMaterial("wool");
System.out.println(""+b1.getMaterial());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.