Question

Write a class called Shelf that contains instance data that represents the length, breadth, and capacity...

Write a class called Shelf that contains instance data that represents the length, breadth, and capacity of the shelf. Also include a boolean variable called occupied as instance data that represents whether the shelf is occupied or not. Define the Shelf constructor to accept and initialize the height, width, and capacity of the shelf. Each newly created Shelf is vacant (the constructor should initialize occupied to false). Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the shelf. Create a driver class called ShelfCheck, whose main method instantiates and updates several Shelf objects. (Java)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Shelf.java


public class Shelf {
   private double length;
   private double breadth;
   private int capacity ;
   private boolean occupied ;
   public Shelf(double length, double breath ,int capacity){
       this.length = length;
       this.breadth = breath;
       this.capacity = capacity;
       occupied = false;
   }
   public double getLength() {
       return length;
   }
   public void setLength(double length) {
       this.length = length;
   }
   public double getBreadth() {
       return breadth;
   }
   public void setBreadth(double breadth) {
       this.breadth = breadth;
   }
   public int getCapacity() {
       return capacity;
   }
   public void setCapacity(int capacity) {
       this.capacity = capacity;
   }
   public boolean isOccupied() {
       return occupied;
   }
   public void setOccupied(boolean occupied) {
       this.occupied = occupied;
   }
   public String toString(){
       return "Length: "+getLength()+" Breadth: "+getBreadth()+" Capacity: "+getCapacity()+" Occupied: "+isOccupied();
   }
  
}

ShelfCheck.java


public class ShelfCheck {

  
   public static void main(String[] args) {
       Shelf s = new Shelf(10,20, 5);
       System.out.println(s.toString());
       s.setOccupied(true);
       System.out.println(s.toString());
       s.setBreadth(50);
       s.setLength(30);
       s.setCapacity(40);
       s.setOccupied(true);
       System.out.println(s.toString());

   }

}

Output:

Length: 10.0 Breadth: 20.0 Capacity: 5 Occupied: false
Length: 10.0 Breadth: 20.0 Capacity: 5 Occupied: true
Length: 30.0 Breadth: 50.0 Capacity: 40 Occupied: true

Add a comment
Know the answer?
Add Answer to:
Write a class called Shelf that contains instance data that represents the length, breadth, and capacity...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name

    Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name, flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight objects.

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • 8). Name five of the fundamental ter ns which encompass object-oriented programming 9). Write a class...

    8). Name five of the fundamental ter ns which encompass object-oriented programming 9). Write a class called NumberOfGoals that represents the total number of goals scored by a ootball team. The NumberOfGioals class should contain a single integer as data, representing the number of goals scored. Write a constructor to initialize the number of goals to Zero. 10). Write a set of instructions to prompt the user for an int value and input it using the Scanner class into the...

  • A java class named Book contains: instance data for the title, author, publisher and copyright year...

    A java class named Book contains: instance data for the title, author, publisher and copyright year a constructor to accept and initialize the instance data variables set and get methods a toString() method to return a formatted, multi-line description of the book Produce a child class that inherits from Book. The class is called Dictionary. Dictonary must include: instance data that describes the number of words in the dictionary a constructor that takes in all information needed to describe a...

  • Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains...

    Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains instance data that represents the dog's name and dog's age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in "person years" (seven times age of dog. Include a toString method that returns a one-time description of the dog (example below)....

  • Can you please help me with with problem. please follow all the specific insturctions, and try...

    Can you please help me with with problem. please follow all the specific insturctions, and try to make simple for a beginner in java. Write a class called Shape that contains instance data that represents the name and number of sides of the shape. Define a constructor to initialize these values. Include mutator(setter) methods – with the this reference – for the instance data, and a toString method that returns a the shape data. Create a static variable to keep...

  • Write an entire class called Movie that stores an instance variable named title for the title...

    Write an entire class called Movie that stores an instance variable named title for the title of the movie as a string. Write the corresponding getter and setter methods (getTitle, setTitle), and a constructor that takes one parameter (string for the title). Include both the class definition and method definitions! Be sure to use appropriate visibility modifiers. Assume all includes and using namespace std are already in the file. Do not try to make header files.

  • 1. Assume you have a Car class that declares two private instance variables, make and model....

    1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT