Question

In: Computer Science

Create a class named Blanket with fields for a blanket’s size, color, material, and price. Include...

  1. 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.

  1. 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 fewer 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.

Solutions

Expert Solution

class Blanket
{
   private String size,color,material;
   private double price;
  
   public Blanket()
   {
       size = "Twin";
       color = "white";
       material = "cotton";
       price = 30.0;
   }
  
   public void setSize(String size)
   {
       this.size = size;
       if(size == "Twin")
       this.price = this.price + 10;
       else if(size == "Queen")
       this.price = this.price +25;
       else if(size == "King")
       this.price = this.price +40;
   }
   public void setColor(String color)
   {
       this.color = color;
   }
   public void setMaterial(String material)
   {
       this.material = material;
       if(material == "wool")
       this.price = this.price + 20;
       else if(material == "cashmere")
       this.price = this.price +45;
       else if(size == "King")
       this.price = this.price +40;
   }
   public double getPrice()
   {
       return price;
   }

public String toString()
{
   return "Blanket size : "+size +" color : "+color +" material : "+material + " Price : "+getPrice();
  
}
}
class ElectricBlanket extends Blanket
{
   private int numHeatSettings;
   private boolean automaticShutOff;
  
   public ElectricBlanket()
   {
       super();
       numHeatSettings = 1;
       automaticShutOff = false;
      
   }
   public void setNumHeatSettings(int numHeatSettings)
   {
       if(numHeatSettings < 5)
       this.numHeatSettings = 1;
       else
      
       this.numHeatSettings = numHeatSettings;
   }
   public int getNumHeatSettings()
   {
       return numHeatSettings;
   }
   public void setAutomaticShutOff(boolean automaticShutOff)
   {
      
      
       this.automaticShutOff = automaticShutOff;
   }
   public boolean getAutomaticShutOff()
   {
       return automaticShutOff;
   }
  



public String toString()
{
    return super.toString() +" Number of Heat Settings : "+numHeatSettings+" Has automatic shut off : "+ automaticShutOff;
}

public double getPrice()
{
    if(automaticShutOff == true)
    return (super.getPrice() + 5.75);
    else
    return super.getPrice();
}
}

class TestBlanket
{
   public static void main (String[] args)
   {
       ElectricBlanket eb = new ElectricBlanket();
       eb.setSize("King");
       eb.setColor("green");
       eb.setMaterial("Kashmere");
       eb.setAutomaticShutOff(true);
       eb.setNumHeatSettings(6);
      
       System.out.println(eb);
       System.out.println("Price : "+eb.getPrice());
      
      
   }
}

Output:

Blanket  size : King color : green material : Kashmere   Price : 115.75  Number of Heat Settings : 6  Has automatic shut off : true
Price : 115.75

Do ask if any doubt.Please upvote.


Related Solutions

Create an Automobile class for a dealership. Include fields for an ID number, make, model, color,...
Create an Automobile class for a dealership. Include fields for an ID number, make, model, color, year, vin number, miles per gallon, and speed. Include get and set methods for each field. Do not allow the ID to be negative or more than 9999; if it is, set the ID to 0. Do not allow the year to be earlier than 2000 or later than 2017; if it is, set the year to 0. Do not allow the miles per...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
This is 1 java question with its parts. Thanks! Create a class named Lease with fields...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG),...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG), the course number (for example, 101), the credits (for example, 3), and the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data. Create a subclass named LabCourse that adds $50 to the course fee....
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create “New Class…” named Book to store the details of a book Declare 3 fields for...
Create “New Class…” named Book to store the details of a book Declare 3 fields for the details of the book: field named author of type String field named title of type String field named callNumber of type String Add overloaded constructors with the following headers and make sure each constructor has only 1 statement in the body that makes an internal method call to setBookDetails: public Book(String author, String title, String callNumber) public Book(String author, String title) HINT: Initialize...
Question 1 - Create a class named Student that has fields for an ID number, number...
Question 1 - Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT