#Returning different types

1 messages · Page 1 of 1 (latest)

tropic cape
#

I want to return null if list is empty, but I want to return an item that has heaviest weight. I don't know how to set up the method to accept both return options. Any ideas?

  public Item heaviestItem(){
        if (items.size() == 0) {
            heaviestItem = null;
        } else {
         for(Item item: items) {
             if (item.getWeight() > heaviestItem.getWeight()) {
                 heaviestItem = item;
             }
    }
        }
        
        return heaviestItem;
    }
raven nymphBOT
#

This post has been reserved for your question.

Hey @tropic cape! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

graceful bluff
tropic cape
#

Well, this is MOOC challenge and it gives me an error when I submit.

graceful bluff
#

yeah because heaviest item doesn't have a type. It should be Item heaviestItem

tropic cape
#

Sorry if this is stupid questions, but isn't that what I set as a type in my method?

graceful bluff
#

no in the method declaration you just specify the return type. The compiler has no way of knowing which variable inside that method should be of type item

tropic cape
#

I did set in my code heaviestItem as a type of Item

graceful bluff
#

where ?

tropic cape
#

Suitcase class:

public class Suitcase {

    private ArrayList<Item> items = new ArrayList<>();
    private int maxWeight;
    private int weight;
    private Item heaviestItem;

    public Suitcase(int maxWeight) {
        this.maxWeight = maxWeight;
        this.weight = 0;
    }

    public void addItem(Item item) {

        if ((this.weight + item.getWeight()) < this.maxWeight) {
            items.add(item);
            weight = weight + item.getWeight();
        }
    }

    public String toString() {
        if (items.size() == 0) {
            return "no items (" + this.weight + "kg)";
        } else if (items.size() == 1) {
            return items.size() + " item (" + this.weight + "kg)";
        } else {
            return items.size() + " items (" + this.weight + "kg)";
        }
    }
    
    public void printItems(){
    for(Item item: items) {
        System.out.println(item.toString());
    }
    }
    
    public int totalWeight(){
    return this.weight;
    }
    
    public Item heaviestItem(){
        if (items.size() == 0) {
            heaviestItem = null;
        } else {
         for(Item item: items) {
             if (item.getWeight() > heaviestItem.getWeight()) {
                 heaviestItem = item;
             }
    }
        }
        System.out.println(heaviestItem);
        return heaviestItem;
    }
}
#

I have Item and Main class as well

graceful bluff
#

oh yeah ok I lacked that information. Whats the error you are getting ?

tropic cape
#

This is the main class:

public class Main {

    public static void main(String[] args) {
        // You can use the main to test your classes!
        Item book = new Item("Lord of the rings", 2);
        Item phone = new Item("Nokia 3210", 1);
        Item brick = new Item("Brick", 4);

        Suitcase suitcase = new Suitcase(10);
        suitcase.addItem(book);
        suitcase.addItem(phone);
        suitcase.addItem(brick);

        Item heaviest = suitcase.heaviestItem();
        System.out.println("Heaviest item: " + heaviest);
    }

}
graceful bluff
#

in your if statement add item != null && ...

tropic cape
#

What if it is?

graceful bluff
#

then you cant compare the two

tropic cape
#

Task says to return null if there are no items in suitcase

graceful bluff
#

what is line 58 ?

tropic cape
#
 public Item heaviestItem(){
        if (items.size() == 0) {
            heaviestItem = null;
        } else {
         for(Item item: items) {
//Line 58 below
             if (item.getWeight() > heaviestItem.getWeight()) {
                 heaviestItem = item;
             }
    }
        }
        System.out.println(heaviestItem);
        return heaviestItem;
    }
graceful bluff
#

yeah just to be sure rewrite the code so that heaviestItem is always null at the start and add the null check in line 58

tropic cape
#

You are good! That logic makes lots of sense. Thanks a lot @graceful bluff

graceful bluff
#

you're welcome 🙂