- Basic elements of class definitions:
- fields
- constructors
- parameters
- methods
- accessor (getter)
- mutator (setter)

Alberto Ferrari
Ingegneria dell'Informazione, UniPR
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 ...
*/
public TicketMachine(int cost)
{
price = cost;
balance = 0;
total = 0;
}
...
}
public class TicketMachine
{
Inner part of the class omitted.
}
public class ClassName {
Fields
Constructors
Methods
}
<visibility modifier> <type> <field name>;
private int price;
public class TicketMachine
{
private int price;
private int balance;
private int total;
... Constructor and methods omitted ...
}
// single-line comment
/* multi-line
comment */
public class TicketMachine
{
Fields omitted.
/**
* Create a machine that issues tickets of the given price.
* ...
*/
public TicketMachine(int cost)
{
price = cost;
balance = 0;
total = 0;
}
Methods omitted.
}

public TicketMachine(int cost)
/**
* Return the price of a ticket.
*/
public int getPrice()
{
return price;
}
/**
* Increase score by the given number of points.
*/
public void increase(int points)
{
...
}
/**
* Reduce price by the given amount.
*/
public void discount(int amount)
{
...
}
System.out.println(something-we-want-to-print);
if(perform some test that gives a true or false result) {
Do the statements here if the test gave a true result
}
else {
Do the statements here if the test gave a false result
}
if(amount > 0) {
balance = balance + amount;
} else {
System.out.println("Use a positive amount rather than: " + amount);
}
public int refundBalance() {
int amountToRefund;
amountToRefund = balance;
balance = 0;
return amountToRefund;
}
public TicketMachine(int cost) {
int price = cost;
balance = 0;
total = 0;
}
public void setRefNumber(String ref)
Alberto Ferrari
Ingegneria dell'Informazione, UniPR
www.ce.unipr.it/~aferrari/