In: Computer Science
Soccer League
Java homework assignment.
Below is the assignment brief.
It requires to have the necessary java classes created: 'Team', 'Game', 'Schedule', and 'Main'.
All attributes are private, and getters/setters are created.
The sample output should be as stated below.
Overview
It has been a brutally cold and snowy winter. None of your friends have wanted to play soccer. But now that spring has arrived, another season of the league can begin. Your challenge is to write a program that models a soccer league and keeps track of the season’s statistics. There are 4 teams in the league. Matchups are determined at random. 2 games are played every Tuesday, which allows every team to participate weekly. There is no set number of games per season. The season continues until winter arrives. The league is very temperature sensitive. Defenses are sluggish on hot days. Hotter days allow for the possibility of more goals during a game. If the temperature is freezing, no games are played that week. If there are 3 consecutive weeks of freezing temperatures, then winter has arrived, and the season is over.
Task
Write a program that models a soccer league and keeps track of the season’s statistics. Carefully consider what data should be stored in an array and what data should be stored in an Array List. Design classes with fields and methods based on the description of the league. You will also need a test class that contains the main method. All fields must be private. Provide any necessary getters and setters.
Team
Each team has a name. The program should also keep track of each team’s win-total, loss-total, tie total, total goals scored, and total goals allowed. Create an array of teams that the scheduler will manage. Print each team’s statistics when the season ends.
Game
In a game, it is important to note each team’s name, each team’s score, and the temperature that day. Number each game with an integer ID number. This number increases as each game are played. Keep track of every game played this season. This class stores an Array List of all games as a field. Your program should determine scores at random. The maximum number of goals any one team can score should increase proportionally with the temperature. But make sure these numbers are somewhat reasonable. When the season ends, print the statistics of each game. Print the hottest temperature and average temperature for the season.
Schedule
Accept user input through a JOptionPane or Scanner. While the application is running, ask the user to input a temperature. The program should not crash because of user input. If it is warm enough to play, schedule 2 games. Opponents are chosen at random. Make sure teams are not scheduled to play against themselves. If there are 3 consecutive weeks of freezing temperatures, the season is over.
Sample Output:
run:
Too cold to play.
Too cold to play.
Too cold to play.
Season is over
*********RESULTS*********
Team 1
Wins: 1, Losses: 1, Ties:0
Points Scored: 9, Points Allowed: 9
Team 2 Wins: 1, Losses: 1, Ties:0
Points Scored: 8, Points Allowed: 8
Team 3 Wins: 0, Losses: 1, Ties:1
Points Scored: 6, Points Allowed: 9
Team 4 Wins: 1, Losses: 0, Ties:1
Points Scored: 8, Points Allowed: 5
Game #1
Temperature: 90
Away Team: Team 2, 4
Home Team: Team 4, 7
Game #2
Temperature: 90
Away Team: Team 1, 8
Home Team: Team 3, 5
Game #3 Temperature: 35
Away Team: Team 1, 1
Home Team: Team 2, 4
Game #4
Temperature: 35
Away Team: Team 3, 1
Home Team: Team 4, 1
Hottest Temp: 90
Average Temp:62.5
public class Cat {
public String name;
public int age;
public int weight;
public Cat(String name, int age, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
public Cat() {
}
public void sayMeow() {
System.out.println("Meow!");
}
}
But it does. Imagine you're sitting at work and write this
Cat
class to represent cats. And then you go home.
While you're gone, another programmer arrives at work. He creates
his own Main
class, where he begins to use the
Cat
class you wrote.
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.name = "";
cat.age = -1000;
cat.weight = 0;
}
}
It doesn't matter why he did it and how it happened (maybe the
guy's tired or didn't get enough sleep). Something else matters:
our current Cat
class allows fields to be assigned
absolutely insane values. As a result, the program has objects with
an invalid state (such as this cat that is -1000 years old). So
what error did we make when declaring our class? We exposed our
class's data. The name, age and weight
fields are public. They can be accessed anywhere in the program:
simply create a Cat
object and any programmer has
direct access to its data through the dot (.
)
operator
Cat cat = new Cat();
cat.name = "";
Here we are directly accessing the name field and
setting its value. We need to somehow protect our data from
improper external interference. What do we need to do that? First,
all instance variables (fields) must be marked with the private
modifier. Private is the strictest access modifier in Java. Once
you do this, the fields of the Cat
class will not be
accessible outside the class.
public class Cat {
private String name;
private int age;
private int weight;
public Cat(String name, int age, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
public Cat() {
}
public void sayMeow() {
System.out.println("Meow!");
}
}
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.name = "";//error! The Cat class's name field is private!
}
}
The compiler sees this and immediately generates an error. Now the fields are sort of protected. But it turns out that we've shut down access perhaps too tightly: you can't get an existing cat's weight in the program, even if you need to. This is also not an option. As it is, our class is essentially unusable. Ideally, we need to allow some sort of limited access:
Cat
objectsThat's a decent list of requirements! In reality, all this is easily achieved with special methods called getters and setters