tags:

views:

107

answers:

3

Mutator returning a value

Add a new method emptyMachine() to the TicketMachine class that is designed to simulate emptying the machine of money It should both return the value in total and reset the value of total to zero. Paste the whole method into the space below

public int emptyMachine()
    {
        System.out.println("# " + total );
        total = 0;

    }

i get this error:

TicketMachine.java:44: missing return statement
    }
    ^
1 error
The output should have been:
    emptyMachine() returns correct value
    emptyMachine() empties machine

This is what was actually produced:
    Exception in thread "main" java.lang.NoClassDefFoundError: TicketMachine
A: 

Your method signature declares that it returns an int, but you don't return anything at all.

public int emptyMachine()
{
    System.out.println("# " + total );
    total = 0;
    return total;
}

I have to assume that the variable 'total' is defined outside of the method because you do not show us that code.

Ed Swangren
public int emptyMachine() { System.out.println("# " + total ); total = 0; return total; }you mean that?
Tical
Mark: 0 out of 1Comments: * Test 1 (0.0 out of 1) The compilation was successful The output should have been: emptyMachine() returns correct value emptyMachine() empties machine This is what was actually produced: # 300 # 0 emptyMachine() empties machine
Tical
@Tical: I just added that as an instructional type of thing, but yes, the OP's assignment was to return 'total'.
Ed Swangren
+1  A: 

As the compiler says, you are missing a return statement in your method - which must return an int value

How to use the Java return statement

jitter
+2  A: 

Instructions: ""It should both return the value in total and reset the value of total to zero.""

That's easy. In this case the "and" can only be usefully read as "as well as".

public int emptyMachine()
{
    int prevTotal = total;
    total = 0;
    return prevTotal;
}

Win \o/ !

pst