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


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:



publicclass Main {


publicstatic voidmain(String[] args) {


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


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


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



        Suitcase suitcase =newSuitcase(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)";


}


}

Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here