Create the class Suitcase. Suitcase has things and a maximum weight limit, which defines the greatest total allowed weight of the things contained within the Suitcase object. Add the following methods to your class: A constructor, which is given a maximum weight limit public void addThing(Thing thing), which adds the thing in the parameter to your suitcase. The method does not return any value. public String toString(), which returns a string in the form "x things (y kg)" The things are saved into an ArrayList object: ArrayList things = new ArrayList();              The class Suitcase has to make sure the thing's weight does not cause the total weight to exceed the maximum weight limit. The method addThing should not add a new thing if the total weight happens to exceed the maximum weight limit. Below, you find an example of how the class can be used: public class Main {     public static void main(String[] args) {         Thing book = new Thing("Happiness in three steps", 2);         Thing mobile = new Thing("Nokia 3210", 1);         Thing brick = new Thing("Brick", 4);           Suitcase suitcase = new Suitcase(5);         System.out.println(suitcase);           suitcase.addThing(book);         System.out.println(suitcase);           suitcase.addThing(mobile);         System.out.println(suitcase);           suitcase.addThing(brick);         System.out.println(suitcase);     } }              The program output should look like the following: 0 things (0 kg) 1 things (2 kg) 2 things (3 kg) 2 things (3 kg)   This is class Thing public class Thing { privateStringname; privateintweight;   publicThing(Stringname,intweight){ this.name=name; this.weight=weight; }   publicStringgetName(){ // returns the thing's name returnthis.name; }   publicintgetWeight(){ // returns the thing's weight returnthis.weight; }   publicStringtoString(){ returnthis.name+" ("+this.weight+" kg)"; } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Create the class Suitcase. Suitcase has things and a maximum weight limit, which defines the greatest total allowed weight of the things contained within the Suitcase object.

Add the following methods to your class:

  • A constructor, which is given a maximum weight limit
  • public void addThing(Thing thing), which adds the thing in the parameter to your suitcase. The method does not return any value.
  • public String toString(), which returns a string in the form "x things (y kg)"

The things are saved into an ArrayList object:

ArrayList<Thing> things = new ArrayList<Thing>();

            

The class Suitcase has to make sure the thing's weight does not cause the total weight to exceed the maximum weight limit. The method addThing should not add a new thing if the total weight happens to exceed the maximum weight limit.

Below, you find an example of how the class can be used:

public class Main {

    public static void main(String[] args) {

        Thing book = new Thing("Happiness in three steps", 2);

        Thing mobile = new Thing("Nokia 3210", 1);

        Thing brick = new Thing("Brick", 4);

 

        Suitcase suitcase = new Suitcase(5);

        System.out.println(suitcase);

 

        suitcase.addThing(book);

        System.out.println(suitcase);

 

        suitcase.addThing(mobile);

        System.out.println(suitcase);

 

        suitcase.addThing(brick);

        System.out.println(suitcase);

    }

}

            

The program output should look like the following:

0 things (0 kg)

1 things (2 kg)

2 things (3 kg)

2 things (3 kg)

 

This is class Thing

public class Thing {

privateStringname;

privateintweight;

 

publicThing(Stringname,intweight){

this.name=name;

this.weight=weight;

}

 

publicStringgetName(){

// returns the thing's name

returnthis.name;

}

 

publicintgetWeight(){

// returns the thing's weight

returnthis.weight;

}

 

publicStringtoString(){

returnthis.name+" ("+this.weight+" kg)";

}

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education