Question

In: Computer Science

Soccer League Java homework assignment. Below is the assignment brief. It requires to have the necessary...

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

Solutions

Expert Solution

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:

  • Other programmers should be able to create Cat objects
  • They should be able to read data from existing objects (for example, get the name or age of an existing cat)
  • It should also be possible to assign field values. But in doing so, only valid values should be allowed. Our objects should be protected from invalid values (e.g. age = -1000, etc.).

That's a decent list of requirements! In reality, all this is easily achieved with special methods called getters and setters


Related Solutions

The homework assignment this week requires you to prepare various journal entries for a series of...
The homework assignment this week requires you to prepare various journal entries for a series of transactions involving sales transactions, estimations for bad debts expense, and estimated warranty obligations. Remember – the only way to lose points is to leave something blank. Take your best shot even if you are NOT really sure. The Professor sold rubber life rafts with a full cash refund if they sunk within a year after the purchase. During the first year 300 rafts were...
Follow the steps below to complete your SPSS homework assignment: At the bottom of this document...
Follow the steps below to complete your SPSS homework assignment: At the bottom of this document are 20 questionnaires that were completed by undergraduate students at University XYZ. Open a new SPSS file and save it. Leave the output window open. You will save the output in just a moment. Use the questionnaire questions to correctly define each variable in an SPSS file. Further directions can be found at the following YouTube link: https://www.youtube.com/watch?v=MoKDcPpRa_0 Generate a “file information” output of...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci numbers form a sequence where each number is the sum of the previous two numbers. Starting from 0 and 1, the first eight Fibonacci numbers are built in the following list using the equation Fn = Fn-1 + Fn-2: 0, 0 0, 1 1, 1 1, 2 2, 3 3, 5 5, 8 8, 13 The sequence would be the numbers in red (0,...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale (POS) system. The solution must be in JAVA language. You create the main program called POSmain.java and two classes: ShoppingCart and CashRegister. Your POSmain program should take three file names from command line arguments. The first file contains a list of products and their prices; the second and third files are lists of items in two shopping carts of two customers. The POSmain program...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies on localization to format currencies and dates. In NetBeans, copy the linked code to a file named "Startercode.java". Read through the code carefully and replace all occurrences of "___?___" with Java™ code. Note: Refer to "Working with Dates and Times" in Ch. 5, "Dates, Strings, and Localization," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide for help. Run and debug...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT