Gustaffson

Code that does pass. Java MOOC Part 6 - 8

Sep 15th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.47 KB | Source Code | 0 0
  1. public Item heaviestItem(){
  2.     //If no items are in the ArrayList<Item>, then return null.
  3.     if(this.items.isEmpty()){
  4.         return null;
  5.     }
  6.  
  7.     //Create new object with first item in Arraylist<Item>
  8.     Item heaviestItem = this.items.get(0);
  9.  
  10.     //Go through all the items in the Arraylist<Item> and find the heaviest one.
  11.     for (Item item:this.items){
  12.            
  13.         if(heaviestItem.getWeight()<item.getWeight()){
  14.             heaviestItem = item;
  15.         }
  16.     }
  17.     return heaviestItem;
  18. }
Tags: Java MOOC
Advertisement
Add Comment
Please, Sign In to add comment