C211 I211 Problem Solving and Programming II

C211 I211 Lab 3

Due Date: Monday, Sptember 11, 2023.

In this lab, we're going to start a project storing information about football teams in a file. This project will be continued in the homework.

Ex. 1. a. FootballTeam Class

Open either Eclipse or NetBeans. Create a project called lab3 and a class called FootballTeam inside it. Do not add the main method to it - we'll add a separate test class in this project. Import the modules java.io.File and java.io.FileWriter.

Attributes. Add a couple of instance attribute to this class called name and location of type String. Also add an integer instance attribute called numberWins.

Constructors. Write a default constructor for this class and another one that takes values for the team name, location, and number of wins.

Comparison. We want to be able to compare an array of FootballTeam objects using a library class. For that, first we need to add an interface to the class declaration:

public class FootballTeam implements Comparable<FootballTeam> {

Now, let's write a comparison method that tells us which team has more wins. This will allows later to use the method Arrays.sort() with arrays made of objects of this class. The signature of the function should be

@Override
public int compareTo(FootballTeam other)

This method should return a positive number if the current team has a higher number of wins than the other one, 0 if they are equal, and a negative number if the other team has more wins than the current team. You can just subtract the number of wins of the other from the current number of wins and return it.

Add Wins. Write another public method to this class called addWins taking one integer parameter, that adds the value of this parameter to the number of wins. This will be tested in the homework.

b. File Operations

Read. Write a public method called read that takes one String parameter representing a file name, and reads the team name, location, and number of wins from the file. Write a similar method that takes a parameter of type Scanner and inputs the team's name, location, and number of wins from it. In both cases, we'll assume that each value is stored on a full line, since the name and location can be made up of multiple words, such as "IUSB Titans" or "South Bend".

Write. Add a couple of methods called write that take a file name as parameter and that take a FileWriter as parameter. In both cases, output the name, location, and number of wins to the file, each on one line.

c. Test Class

Add a test class to the project containing the main function called Main.

In the main method, declare a couple of objects of type FootballTeam and use the constructor with given name and location for one of them, and the default constructor for the other one. Create a text file in the project folder called team1.txt and store the name of your favorite team in it, together with the location and a number of wins. Read the second object from the file. Call the function write on the first object to write it to the file team2.txt.

Test this part of the program to make sure that it works.

Lab Submission

Wait until you finish the homework to upload all the files .java that you worked on this week.