Java Reference
In-Depth Information
Code 2.1
continued
The
TicketMachine
class
/**
* 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;
}
}
2.3
The class header
The text of a class can be broken down into two main parts: a small outer wrapping that
simply names the class and a much larger inner part that does all the work. In this case, the
outer wrapping appears as follows:
public class TicketMachine
{
Inner part of the class omitted.
}
The outer wrappings of different classes all look pretty much the same. The outer wrapping
contains the class header, whose main purpose is to provide a name for the class. By a widely
Exercise 2.6
Write out what you think the outer wrappers of the
Student
and
LabClass
classes might look like; do not worry about the inner part.
Exercise 2.7
Does it matter whether we write
public class TicketMachine
or
class public TicketMachine
Search WWH ::
Custom Search