Question

Design classes that store the information about various pieces of furniture. The system will support at...

Design classes that store the information about various pieces of furniture. The system will support at least these pieces: table, chair and lamp. For each piece of furniture, the system will store some attributes. For a table, it will be at least width, length, height, photo and price. For a chair, it will be at least price, photo, height and diagonal. For a lamp, it will be at least the number of bulbs, photo and price. Each piece of furniture will provide a method that prints all the information about that piece. For example, a chair will be printed as follows:

Chair, id: 1, photo: chair1.png, price: 11.99, diagonal: 6, height: 12

A table will be printed as follows: Table, id: 2, photo: table1.png, price: 12.48, width: 10, length: 12, height: 8

And a lamp as follows: Lamp, id: 3, photo: lamp1.png, price: 8.75, bulbs: 2

Besides the individual pieces, the system will support sets of pieces. Each set will have a name, any number of pieces of furniture, and a price and will provide a method that prints all the information about that set. For example, a kitchen set will be printed as follows:

Kitchen (set) Table, id: 4, photo: table2.png, width: 11, length: 16, height: 25 Chair, id: 5, photo: chair2.png, diagonal: 5, height: 10 Chair, id: 5, photo: chair2.png, diagonal: 5, height: 10 price: 25.99

Each class will provide suitable constructors and methods. Each piece of furniture will have a unique ID that will be assigned automatically.

java programming

Homework Answers

Answer #1

Short Summary:

  • Implemented the given problem using OOP
  • Provided you the UML diagram which explains class structure
  • The common characterstics should be placed in an abstract class, then child classes should inherit the parent class and implement the abstract methods.
  • set class has list of furnitures and print method
  • Shown you sample test driver and sample test run.

UML Diagram:

Source Code:

furniture.java File:

public abstract class furniture {
   private int ID;
   private static int numberOfObject;
   private String photo;
   private double price;

   /**
   *
   */
   public furniture() {
       numberOfObject++;
       this.ID = numberOfObject;
   }

   /**
   * @return the photo
   */
   protected String getPhoto() {
       return photo;
   }

   /**
   * @param photo the photo to set
   */
   protected void setPhoto(String photo) {
       this.photo = photo;
   }

   /**
   * @return the price
   */
   protected double getPrice() {
       return price;
   }

   /**
   * @param price the price to set
   */
   protected void setPrice(double price) {
       this.price = price;
   }

   /**
   * @return the iD
   */
   protected int getID() {
       return ID;
   }
  
   // abstract method to be overriden in child class
   public abstract void printInfo();
  
}

table.java File:

public class table extends furniture{
   private int width;
   private int length;
   private int height;
   /**
   * @param width
   * @param length
   * @param height
   */
   public table(int width, int length, int height, String photo, double price) {
       super();
       this.width = width;
       this.length = length;
       this.height = height;
       super.setPhoto(photo);
       super.setPrice(price);
   }
  
   // Print table info
   @Override
   public void printInfo() {
       System.out.println("Table, id: " +super.getID() + ", photo: " + super.getPhoto() + ", price: " + super.getPrice()
           + ", width: " + this.width + ", length: " + this.length + ", height: " + this.height);
      
   }
}

chair.java File:

public class chair extends furniture {
   private int height;
   private int diagonal;
  
   /**
   * @param height
   * @param diagonal
   */
   public chair(int height, int diagonal, String photo, double price) {
       super();
       this.height = height;
       this.diagonal = diagonal;
       super.setPhoto(photo);
       super.setPrice(price);
   }

   @Override
   public void printInfo() {
       System.out.println("Chair, id: " + super.getID() + ", photo: " + super.getPhoto() + ", price: " +
               super.getPrice() + ", diagonal: " + this.diagonal + ", height: " + this.height);
      
   }
  
  
}

lamp.java File:
public class lamp extends furniture {
   private int numberOfBulbs;

   /**
   * @param numberOfBulbs
   */
   public lamp(int numberOfBulbs, String photo, double price) {
       super();
       this.numberOfBulbs = numberOfBulbs;
       super.setPhoto(photo);
       super.setPrice(price);
   }

   @Override
   public void printInfo() {
       System.out.println("Lamp, id: " + super.getID() + ", photo: " + super.getPhoto() + ", price: " + super.getPrice() +
               ", bulbs: " + this.numberOfBulbs);
   }
}

set.java File:

import java.util.ArrayList;

public class set {
   private ArrayList<furniture> furnitures;

   /**
   *
   */
   public set() {
       super();
       furnitures = new ArrayList<furniture>();
   }
  
   public void addFurniture(furniture f) {
       furnitures.add(f);
   }
  
   public void printSet() {
       for(furniture f : furnitures) {
           f.printInfo();
       }
   }
}

furnitureTest.java File:
public class furnitureTest {

   public static void main(String[] args) {
       chair c = new chair(12, 6, "chair1.png", 11.99);
       table t = new table(10, 12, 8, "table1.png", 12.48);
       lamp l = new lamp(2, "lamp1.png", 8.75);
      
       set kitchenSet = new set();
       kitchenSet.addFurniture(c);
       kitchenSet.addFurniture(t);
       kitchenSet.addFurniture(l);
       kitchenSet.printSet();

   }

}

Sample Run:

**************************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

**************************************************************************************

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions