The Student Room Group

Java - Naieve Ticket Machine Tutorial Help

Hi, im working through some java text books to try and learn, and i am at the end of the chapter trying to do some tasks


/**
* 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.
*
* @author David J. Barnes and Michael Kolling
* @version 2006.03.30
*/
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;

/**
* 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;
}

public void prompt ()

{
System.out.println("Please enter the correct amount of money");
}


public void showPrice ()

{
System.out.println ("The price of a ticket is " + price + "cents");
}

/**
* 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;
}

/**
* Print a ticket.
* Update the total collected and
* reduce the balance to zero.
*/
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;
}
}


the question says,

implement a method called empty that simulates the effect of removing all money from the machine, this method should have a void return type and it s body should set the total field to zero. Does this method need to take any parameters?

Now as far as i understant (which is probs completley wrong) do i just need the following method

public void empty ()

{
total = 0;
}


if i am wrong please dont give me the answer, hints would be nice though :wink:

thanks
Reply 1
Looks right to me.

I would suggest also adding a check in the printTicket() method that make sure the balance is high enough.
Reply 2
Baron, the last task says give the class two constructors, one should take a single parameter that specifices the price, and the other takes no parameter and set the price to be a defualt of your choosing?

i dont really understand this, is it possible to have two contructors in one class? wont they overide each other?
Reply 3
No they won't override each other. They will overload each other. You already have one constructor that defines a default price. Once you've made both constructors, you could choose which constructor you use when creating a TicketMachine object by either specifying a ticket price or by not.

TicketMachine defaultTicketMachine = new TicketMachine();
TicketMachine expensiveTicketMachine = new TicketMachine(10000);
Important things to note:
- you'll find constructor overloading used all over the place in the standard library. For example, if you're creating an Integer, you can do it with an actual number (new Integer(5)) or with a string representation of a number (new Integer("5")).
- it's not only constructors but also methods that can be overloaded. For example, you use System.out.println(String x), but println can also take any primitive type (int, boolean etc), or no arguments at all if you just want to print a blank line.
- you can't have two constructors/methods with the same "signature" (take all the same parameters), including if they differ only in return type, as then it wouldn't be possible to work out which one you meant when you called them. So:
- this is OK: "MyObj myMethod()" and "MyObj myMethod(String x)"
- this is OK: "String myMethod()" and "MyObj myMethod(String x)"
- this is not OK: "String myMethod(String x1)" and "String myMethod(String x2)"
- this is not OK: "String myMethod()" and "MyObj myMethod()"
Reply 5
jermaindefoe


implement a method called(1) empty that simulates the (2)effect of removing all money from the machine, this (3)method should have a void return type and it s (4)body should set the total field to zero. Does this method need to take any parameters?

Now as far as i understant (which is probs completley wrong) do i just need the following method

public void empty ()

{
total = 0;
}



from the question (and the ones that i'd bold), it seems you've answer all the questions requirements.

is it possible to have two contructors in one class? wont they overide each other?


All you have to remember is that, Overriding is usually (not usually, definitely :smile: ) is associated with inheritance.

Overloading is where you have the same identifier name for:
1-Methods
2-Constructors
in a class but different parameters'

i.e

public void ADD()
{

}



public void ADD(int i, int x)
{


}



urm..understand? :s-smilie:
Reply 6
owh while Override:


public class Student
{
public void setAge()
{

}
}


and


public class Undergraduate extends Student
{
public void setAge(int something)
{
//have some implementation here

}


}



so if you called Undergraduate object and invokes the method setAge, it said that you've override the Superclass method Student and use the one in Undergraduate
No, actually, by including "int something" for Undergrad but not Student, you overloaded instead of overriding :wink:
Reply 8
ThePants999
No, actually, by including "int something" for Undergrad but not Student, you overloaded instead of overriding :wink:


owh yeah lol, made a mistake there. The method inside the subclass should be the same as inside the Super-duper-Class hehehe :p: - only then it's called Overriding
Reply 9
i don't want the OP to be confused so:


public class Student
{
public void showSomething()
{



}

}




and



public class Undergraduate extends Student
{

public void showSomething()
{

//have some implementation here that's not in the super class

}


}



now i feel better :biggrin:

Latest

Trending

Trending