C211 I211 Problem Solving and Programming II

C211 I211 Homework 3

Due Date: Monday, September 11, 2023.

Make sure to complete Lab 3 before doing the homework.

In this homework, you will continue working on the project start in the lab.

Ex. 1. Tournament Class

Add a class called Tournament to the same project. In this class, add an attribute of type array of FootballTeam objects called teams.

Also in this class, import java.util.Random and declare a static Random object, and initialize it with a new Random(). Also import the modules java.io.File, java.io.FileWriter, java.util.Arrays, and java.util.Scanner.

a. Constructors Write a default constructor where you initialize the teams array as null, and one with a parameter for the number of teams. In the second constructor, initialize the teams with a new array of the size given by the parameter, then add a loop that goes through the array and assigns a new FootballTeam object to each element.

b. File Input and Output

Write an method called read taking one parameter which is a file name (type String). In this method, declare a File object and use it to create a Scanner object, like in the model we've seen in the lecture. Use it to read all the team objects from a file. For this, use a for loop with i going from 0 to the length of the teams array -1 (you can use the specialized for loop if you want) where you check first if the scanner still has a next, and if it has, then call the FootballTeam function read from the object teams[i] with the scanner object as parameter. Use the error-checking syntax shown in the lecture and used in the lab.

Note: The function nextInt from the Scanner class reads only that integer from the input, but not the end of line character that comes after it. That end of line will be returned by the scanner in the next call to nextLine, so the next team will appear with an empty name. To avoid this, you can make one call to the function nextLine after calling the function read.

Write a method called write that declares a FileWriter object, with the appropriate error checking, and then goes through the array teams and calls the method write from the class FootballTeam on each of the elements of the array teams.

c. Output

Write an output method called output that goes through the entire roster of teams and outputs the information in each of them.

Create a file call "teams.txt" where you write down 10 team names with location and number of wins. Place this file in the project folder at the top.

In the main method in the class Main, add a declaration of an object of type Tournament and create it with a size of 10. Then call the function read on this object with the name "teams.txt" as parameter. After that, call the function output also from this object to see if the teams were read correctly.

e. Tournament Season and Sort

In the class Tournament, write a public void method called season with no parameter. In this method, go through the array teams, and for each of them, generate a random number between 1 and 10, and then call the method addWins on the object teams[i] to add it to their score.

After the for loop, sort the array of teams by doing

Arrays.sort(teams);

In the main, after outputting the teams, add a call to the method season, and then to the method write with the file name "newTeams.txt" as a parameter, both of them from the object of type Tournament. Take a look at this file to verify that the output into it is correct.

Homework Submission

Upload the files FootballTeam.java, Main.java, Tournament.java, teams.txt and newTeams.txt to Canvas, Assignments, Homework 3.