Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Item heaviestItem(){
- //If no items are in the ArrayList<Item>, then return null.
- if(this.items.isEmpty()){
- return null;
- }
- //Create new object with first item in Arraylist<Item>
- Item heaviestItem = new Item(this.items.get(0).getName(), this.items.get(0).getWeight());
- //Go through all the items in the Arraylist<Item> and find the heaviest one.
- for (Item item:this.items){
- if(heaviestItem.getWeight()<item.getWeight()){
- heaviestItem = new Item(item.getName(), item.getWeight());
- }
- }
- return heaviestItem;
- }
Advertisement
Add Comment
Please, Sign In to add comment