-6

This is the question

Write a method atCapacity(int people, int capacity) that returns a boolean determining if a stadium is at maximum capacity. A stadium is at capacity if at least 80% of the seats are sold out.

Here's the code I tried

public class Scratchpad
{
    atCapacity(int people, int capacity)
    {
        boolean atCapacity = false;
        capacity = people * (80/100);
    
        if(people > capacity)
        {
            return false;
        }
        else if(people <= capacity)
        {
        return true;
        }
    }
}

The code checker says

"Grader.java: Line 4: invalid method declaration; return type required"

I don't understand how to fix the code. I'm not asking for answers I just need a nudge in the right direction. Thanks

2 Answers2

2

This is supposed to be a method declaration:

    atCapacity(int people, int capacity)

But the syntax for a method declaration requires a return type, as in

    SomeReturnType atCapacity(int people, int capacity)

If we look at the body of the method you are returning true and false so the correct type should be the type of true and false, which is <nudge>


I can see some other errors too (<nudges>):

  • The compiler will tell you that you need a return at the end of the method. (It is not going to deduce that people > capacity and people <= capacity are mutually exclusive.)

    There is more than one way of solving that conundrum.

  • At runtime, 80/100 is going to cause you grief; see Int division: Why is the result of 1/3 == 0?.

  • On the top of that, you have a variable called atCapacity which doesn't appear to be needed.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

you could write

public boolean atCapacity() { ... }
Richard Barker
  • 1,161
  • 2
  • 12
  • 30
0yang
  • 11
  • 2