+1 (315) 557-6473 

Football Tournament Homework Help Using Java

Our adept Java programmers are at your service whenever you need them. They are ready to help students who need football tournament homework help pass their homework with flying colors. All you have to do is send us a “do my football tournament homework for me” message and our super friendly customer support team will link you to a professional Java homework expert. Get in touch with us if you need football tournament homework help using Java. We will be happy to assist you.

Tournament Class

Add a class called Tournament to the same project. In this class, add an attribute of a typical 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.

Constructors

Write a default constructor where you initialize the team's 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.

 File Input and Output

Write a 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 team's 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 function read from the object teams[i] with the scanner object as a parameter. Use the error-checking syntax shown in the lecture and used in the lab.
Write a method called to 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 football team on each of the elements of the array teams.

Output Method

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 called "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 a parameter. After that, call the function output also from this object to see if the teams were read correctly.

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.
Football Team Code Solution
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lab3;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class FootballTeam implements Comparable {
    String name, location;
    int numberWins;
    public FootballTeam() {
        name = "";
        location = "";
        numberWins = 0;
    }
    public FootballTeam(String theName, String loc, int wins) {
        name = theName;
        location = loc;
        numberWins = wins;
    }
    public int compareTo(FootballTeam other) {
        return (numberWins - other.numberWins);
    }
    public void addWins(int wins) {
        numberWins += wins;
    }
    public void read(String filename) {
        File f = new File(filename);
        try (Scanner scan = new Scanner(f)) {
            name = scan.nextLine();
            location = scan.nextLine();
            numberWins = Integer.parseInt(scan.nextLine());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void read(Scanner scan) {
        if (scan.hasNext()) {
            name = scan.nextLine();
            location = scan.nextLine();
            numberWins = Integer.parseInt(scan.nextLine());
        }
    }
    public void write(String filename) {
        try (FileWriter f = new FileWriter(filename)) {
            f.write(name + "\n" + location + "\n" + numberWins + "\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void write(FileWriter f) {
        try {
            f.write(name + "\n" + location + "\n" + numberWins + "\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Main.Java code Solution
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lab3;
public class Main {
    public static void main(String[] args) {
        FootballTeam team2, team1;
        team2 = new FootballTeam("IUSB Titans", "South Bend", 5);
        team1 = new FootballTeam();
        team1.read("team1.txt");
        team2.write("team2.txt");
        Tournament tournament = new Tournament(10);
        tournament.read("teams.txt");
        tournament.output();
        tournament.season();
        tournament.write("newTeams.txt");
    }
}
Tournament Java Code Solution
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lab3;
public class Main {
    public static void main(String[] args) {
        FootballTeam team2, team1;
        team2 = new FootballTeam("IUSB Titans", "South Bend", 5);
        team1 = new FootballTeam();
        team1.read("team1.txt");
        team2.write("team2.txt");
        Tournament tournament = new Tournament(10);
        tournament.read("teams.txt");
        tournament.output();
        tournament.season();
        tournament.write("newTeams.txt");
    }
}

Related Blogs