Several objects working together
Programs are usually made of several classes, each with a job of its own, and their objects use each other. Here's a small football tournament:
- A
Teamkeeps its own name and points. - A
Matchknows its two teams and the goals they scored, and the rule for giving points: 3 to the winner, and 1 to each team for a draw. mainonly sets things up and prints.
public class Main {
public static void main(String[] args) {
Team owls = new Team("Owls");
Team foxes = new Team("Foxes");
Team bears = new Team("Bears");
Match first = new Match(owls, foxes, 2, 1);
Match second = new Match(foxes, bears, 0, 0);
Match third = new Match(bears, owls, 3, 1);
first.givePoints();
second.givePoints();
third.givePoints();
System.out.println(first);
System.out.println(second);
System.out.println(third);
System.out.println(owls);
System.out.println(foxes);
System.out.println(bears);
}
}public class Match {
private Team home;
private Team away;
private int homeGoals;
private int awayGoals;
public Match(Team home, Team away, int homeGoals, int awayGoals) {
this.home = home;
this.away = away;
this.homeGoals = homeGoals;
this.awayGoals = awayGoals;
}
public void givePoints() {
if (this.homeGoals > this.awayGoals) {
this.home.addPoints(3);
} else if (this.awayGoals > this.homeGoals) {
this.away.addPoints(3);
} else {
this.home.addPoints(1);
this.away.addPoints(1);
}
}
public String toString() {
return this.home.getName() + " " + this.homeGoals + "-" + this.awayGoals + " " + this.away.getName();
}
}public class Team {
private String name;
private int points;
public Team(String name) {
this.name = name;
this.points = 0;
}
public void addPoints(int points) {
this.points = this.points + points;
}
public String getName() {
return this.name;
}
public String toString() {
return this.name + ", points: " + this.points;
}
}Owls 2-1 Foxes
Foxes 0-0 Bears
Bears 3-1 Owls
Owls, points: 3
Foxes, points: 1
Bears, points: 4- A Match has two Team objects as instance variables (step 2). They aren't copies:
firstandthirdboth refer to the one Owls object thatowlsrefers to, so the points from both matches add up in the same team. givePointschanges the teams through their own method,addPoints(step 1). It can't touch a team'spointsdirectly, and it doesn't need to.- The match's
toStringasks the teams for their names withgetName(), because the result line should show only the names, not the points.
Each class does one job. Team doesn't know the rules of football, and Match doesn't keep a table. A tournament that gave 2 points for a win would change only givePoints.
Planning a program of several classes
When you plan a program like this, go through its data and its rules. For each piece of data, ask which object it belongs to: the points belong to a team, the goals to a match. For each rule, ask which class knows enough to apply it: giving points needs both teams and both scores, and only a Match has all four. The class that has the data is usually the one that gets the method.