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...
in c#: Create a class named Square that contains fields for area and the length of...
in c#: Create a class named Square that contains fields for area and the length of a side and whose constructor requires a parameter for the length of one side of a Square. The constructor assigns its parameter to the length of the Square’s side field and calls a private method that computes the area field. Also include read-only properties to get a Square’s side and area. Create a class named DemoSquares that instantiates an array of ten Square objects...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class RealestateFinder.java that reads in real estate data from a CSV file (RealestateList.csv). Then provide the user with the following options: Sort per Price, Sort per Location, and Exit. Use ArrayList and Arrays.sort to perform the sorts and display the data to the user.
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....
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and height with setter and getter methods. A constructor that sets the values of base and height. A method named toString() that prints the values of base and height. A method named area() that calculates and prints the area of a triangle. Draw the UML diagram for the class. Implement the class. Q2: Write a Java program that creates a two-dimensional array of type integer...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT