tags:

views:

92

answers:

5

Hi, was wondering where im going wrong, any ideas?

Modifying variable value from method

Implement a method empty() in the TicketMachine class that simulates the effect of removing all money from the machine It should have a void return type and the body should simply set the total variable/field to zero.

Does this method need any parameters? No

Is this method a mutator or accessor? mutator

Paste the whole method into the space below

public void empty(int return) 
{ 
balance = 0; 
}

Mark: 0 out of 3

Comments:

* Test 1 (0.0 out of 3)



TicketMachine.java:26: <identifier> expected
  public void empty(int return)
                        ^
  TicketMachine.java:60: ')' expected
  }
  ^
  2 errors

The output should have been:

      No it doesn't need parameters 
       and it is a mutator
      TicketMachine emptied successfully

This is what was actually produced:

      Exception in thread "main" java.lang.NoClassDefFoundError: TicketMachine
+1  A: 

You called your method parameter return:

public void empty(int return)

which is a reserved keyword in Java hence the error above. Rename the parameter...

Jon
OP mentions that the method shouldn't have parameters. They are putting things between the () and apparently doesn't understand that those are parameters in Java.
Drew
Sorry, not sure what you mean. What or who is OP?
Jon
Original Poster => OP
A: 

I have no idea what you're actually asking. But one of the problems might be that return in Java is a reserved word. I can't imagine this ever compiled as you have it. Change it to something else (return0 maybe?) and see if that fixes your problem.

Tenner
TicketMachine.java.28: cannot find symbol\symbol : variable balancelocation: class TicketMachinebalance = 0;^i get that error
Kumar
A: 

i try'd changing the "return" to balance, but that still gave me errors as well, can anyone help?

Kumar
1) Don't answer to your own question unless you actually have an answer - for additional info or inquiries, either edit the question itself, or add a comment to it. 2) Can't be helped unless you post the entire code for your class, and not just the method.
Pavel Minaev
+1  A: 

The question seems to indicate that there should be no parameters.

public void empty() {
balance = 0;
}

Antz
ive try'd that before, in return i gotTicketMachine.java:28: cannot find symbolsymbol : variable balancelocation: class TicketMachinebalance = 0;^
Kumar
Is there anymore code? The sample that you have supplied does not have enough detail to be able to fully answer your question. I assumes that there was a variable in the TicketMachine class because it was referred to in your sample code.
Antz
ive added the full programs code, hopefully it could make it easier
Kumar
The variable definitions have been commented out. You need to move add a `/` afer the final * in the top comment . As it exists this code will not even compile.
Antz
i did that because iwas playing around with the code, but evene without it being hidden this error still pops up and im still confused
Kumar
I have just copied the code, edited the comment added a main method and was able to run the empty method as written withour error. Can you edit your full code post to show the changes that you made.
Antz
A: 

Additional info, here is the full program's code.

/**
 * TicketMachine models a naive ticket machine that issues
 * flat-fare tickets.
 * The price of a ticket is specified via the constructor.
 * It is a naive machine in the sense that it trusts its users
 * to insert enough money before trying to print a ticket.
 * It also assumes that users enter sensible amounts.
 *



    public class TicketMachine 
{
    // The price of a ticket from this machine.
    private int price;
    // The amount of money entered by a customer so far.
    private int balance;
    // The total amount of money collected by this machine.
    private int total;

//     private int name;



    /**
     * Create a machine that issues tickets of the given price.
     * Note that the price must be greater than zero, and there
     * are no checks to ensure this.
     */
    public TicketMachine()
    {
        price = 1000;
        balance = 0;
        total = 0;
//         name = "";
    }


public void empty()
{
balance = 0;
}
//     public void setPrice (int ticketCost)
//     {
//         price = ticketCost;
//     }
    public void setPrice(int newPrice)
    {
        price = newPrice;
    }
    public void setName(int Name, int newName)
{
        Name = newName;
}
//     public TicketMachine(int price)
//     {

//      System.out.println("Price cannot be negative: Setting it to 10");
//     }



    public void priceCompare(int Budget)
    {
     Budget = price;

     if (Budget < price) 
     System.out.println("Too expensive");
     else 
     System.out.println("Just right");
     if
     (Budget > price) 


     System.out.println("Too expensive");
     else 
     System.out.println("Just right");
    }

    /**
     * Return the price of a ticket.
     */
    public int getPrice()
    {
        return price;
    }

    /**
     * Return the amount of money already inserted for the
     * next ticket.
     */
    public int getBalance()
    {
        return balance;
    }

    /**
     * Receive an amount of money in cents from a customer.
     */
    public void insertMoney(int amount)
    {
        balance = balance + amount;
    }

//     public void setName(int newName)
//     {
//         name = newName;
//     }

    /**
     * Print a ticket.
     * Update the total collected and
     * reduce the balance to zero.
     */
      public void prompt()
      {
        System.out.println("please insert correct amount of money");
      }

      public void showPrice()
      {
        System.out.println("the price of a ticket is 5 cents");
      }



      public int getAge(int age) 
      {
   return age;
      }


       public byte getName(byte name)
      {

   return name;
      }




   public void printTicket()
    {
        // Simulate the printing of a ticket.
        System.out.println("##################");
        System.out.println("# The BlueJ Line");
        System.out.println("# Ticket");
        System.out.println("# "  + price + " cents.");
        System.out.println("##################");
        System.out.println();

        // Update the total collected with the balance.
        total = total + balance;
        // Clear the balance.
        balance = 0;
    }
}
Kumar
Advice for beginners: always indent your code consistently. Also, you didn't close your first comment, so a part of what seems to be intended as code is commented out.
Svante
did it because i was playing around with the code
Kumar