top of page

Java Assignment Help, Java Coding Help | Practice Set Paper

The questions for this part are based on generating and displaying some scores for games of Australian Rules Football. Games of football are scored based on “goals” (worth 6 points) and “behinds” (worth 1 point). The points are summed to determine the winner.

Example:


Create a new NetBeans 8.2 Java project called Part2 and add the following starter code in a class called FootballScores:


The following questions for this part either ask you to define a helper class, read or write football scores in a text file, or consider how the solution could be extended for binary files.


Question 2

Add a class called FootballGame to your Part2 project and implement it based on the following class diagram:
















The key methods are described as follows:

  • summarize(): Calculates the winner of the game. Prints a message reporting whether the home team won, lost, or tied the game. Example output formats:

o Round 1: Home team won 26 points to 20.

o Round 2: Home team lost 8 points to 33.

o Round 3: Match was tied 42 points each.

  • toString(): Gives a comma separated values (CSV) representation of the football game details:

o roundNumber,homeGoals,homeBehinds,awayGoals,awayBehinds



Question 3

Complete the body of the writeTextScores() method from the FootballScores class provided in the starter code. This method will randomly generate the scores of NUM_ROUNDS football games. Each goal and behind score is a randomly generated number that must be at least 0 and less than MAX_SHOTS. The scores are written to the file represented by the TEXT_FILEconstant. Each line of the file will be in the format from the toString() method in the FootballGameclass.


Question 4

Complete the body of the printTextScores() method from the FootballScores class provided in the starter code. This method reads the scores from the TEXT_FILE file for one game at a time. For each game, the data is converted back to a FootballGame object and the result of the game is printed to the console using the summarise() method. (Hint: You can use the split() method of the String class to convert CSV data to tokens.)


Question 5

Consider hypothetically the work required to convert your solutions in questions 3-4 to use a binary file rather than a text file. Add a multilinejava comment at the end of your FootballScores class with written answers to the following questions:


a) What change would you need to make to the FootballGame class?

b) Which Java 8 API class would you use to write football scores to a binaryfile?

c) Which Java 8 API class would you use to read football scores from a binaryfile?

d) Briefly describe a technique to make sure you do not exceed the end of the file when reading a binary file?



PART 3 – Questions about Inheritance and Polymorphism

The questions for this part are based on an organisation that manages vehicleregistrations. Vehicle registrations are made up of the owner name (“name”), vehicle registration number (“regNum”), location (one of inner metropolitan, outer metropolitan, or regional) and the engine capacity of motorcycles if relevant (“engCapacity”, measured in “cc”, which is cubic centimeters).


Here are the registration fees that are charged for an annual registration:

The questions are also based on the following class diagram:

















Create a new NetBeans 8.2 Java project called Part3 and complete the following questions.


Question 6

Implement the Vehicle class in your Part3 project as per the problem description and UML class diagram above. Some points to highlight are:

  • The Vehicle class is an abstract class.

  • The Location enumeration is defined inside the Vehicle class, as indicated by .

  • The “location” member is additional to the “Location” enumeration.

  • The calcRegFee() method (for calculating registration fees) is an abstractmethod.

  • The toString() method provides a short summary of the member fields.

  • You need to interpret the remainder yourself from the problem statement and UML

Question 7

Implement the Car class in your Part3 project as per the problem description and UML class diagram above. Some points to highlight are:


  • The Car class extends the Vehicle class.

  • The calcRegFee() method overrides the version in the parent class.

  • Members inherited (but not overridden) are not repeated in the UML diagram.

  • You need to interpret the remainder yourself from the problem statement and UML.

Question 8

Implement the Motorcycleclass in your Part3 projectas per the problem description and UML class diagram above. Some points to highlight are:

  • The Motorcycle class also extends the Vehicle class.

  • The calcRegFee() method also overrides the version in the parent class.

  • The toString() method is overridden to additionally report the enginecapacity.

  • Members inherited (but not overridden) are not repeated in the UML diagram.

  • You need to interpret the remainder yourself from the problem statement and UML.

Question 9

Implement the RegistrationTester class in your Part3 projectto test the other classes.It will perform the following 4 tasks in the main() method:

  • Instantiate an array of type Vehicle and length 4.

  • Initialise the elements of the array with the following vehicles:

  • Write a loop that prints the details of each Car in the array and the registration fee.

  • Write a loop that prints the details of each Motorcycle in the array and the registration fee.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

class FootballGame {
    protected int roundNumber;
    protected int homeGoals;
    protected int homeBehinds;
    protected int awayGoals;
    protected int awayBehinds;

    FootballGame(int roundNumber, int homeGoals, int homeBehinds, int awayGoals, int awayBehinds) {
        this.roundNumber = roundNumber;
        this.homeGoals = homeGoals;
        this.homeBehinds = homeBehinds;
        this.awayGoals = awayGoals;
        this.awayBehinds = awayBehinds;

    }

    public void summarize() {
        int homeScore = 6 * this.homeGoals + this.homeBehinds;
        int awayScore = 6 * this.awayGoals + this.awayBehinds;
        if (homeScore > awayScore)
            System.out.println(
                    "Round " + this.roundNumber + ": Home team won " + homeScore + " points to " + awayScore + ".");
        else if (homeScore < awayScore)
            System.out.println(
                    "Round " + this.roundNumber + ": Home team lost " + homeScore + " points to " + awayScore + ".");
        else
            System.out.println("Round " + this.roundNumber + ": Match was tied " + homeScore + " points each.");
    }

    public String toString() {
        String ret = "";
        ret += this.roundNumber + "," + this.homeGoals + "," + this.homeBehinds + "," + this.awayGoals + ","
                + this.awayBehinds;
        return ret;
    }
}

public class FootballScores {
    private final static int NUM_ROUNDS = 5;
    private final static int MAX_SHOTS = 10;
    private final static String TEXT_FILE = "text-scores.txt";

    public static void main(String[] args) throws FileNotFoundException {
        writeTextScores();
        printTextScores();
    }

    public static void writeTextScores() throws FileNotFoundException {
        // Implementation omitted
        Random random = new Random();
        FileOutputStream fos = new FileOutputStream(TEXT_FILE);
        PrintWriter out = new PrintWriter(fos);

        // output.close();
        for (int i = 1; i <= NUM_ROUNDS; i++) {
            int roundNum = i;
            int hG = random.nextInt(MAX_SHOTS);
            int hB = random.nextInt(MAX_SHOTS);
            int aG = random.nextInt(MAX_SHOTS);
            int aB = random.nextInt(MAX_SHOTS);

            FootballGame FootballGameObj = new FootballGame(roundNum, hG, hB, aG, aB);
            String main_str = FootballGameObj.toString();
            out.println(main_str);
            // output.write(main_str);
        }
        out.close();
    }

    public static void printTextScores() throws FileNotFoundException {
        FileInputStream fis = new FileInputStream(TEXT_FILE);
        Scanner in = new Scanner(fis);
        System.out.println("Printing the data here");
        while (in.hasNext()) {
            String input_str = in.nextLine();
            String[] res = input_str.split("[,]");
            int[] temparr = { 0, 0, 0, 0, 0 };
            for (int i = 0; i < res.length; i++)
                temparr[i] = Integer.parseInt(res[i]);
            FootballGame FootballGameObj = new FootballGame(temparr[0], temparr[1], temparr[2], temparr[3], temparr[4]);
            FootballGameObj.summarize();
        }
        in.close();
        // Implementation omitted
    }
}

// Q5(a):
// in toString method instead of returning the current string we will
// convert it to byte array using getBytes() method of string class
// Q5(b): We can use the java.io.FileOutputStream API for writing scores

// Q5(c): We can use the java.io.FileInputStream API for reading scores
// Q5(d):
// int intval;
// while((intval = fileInputStream.read()) != -1) {
// ch = (char) intval;
// System.out.print(ch);
// }
// So when we reach the end of the file, the api returns -1 and we stop reading
abstract class Vehicle {
    protected String owner;
    protected String regNum;
    protected Location location;

    enum Location {
        INNER_METRO, OUTER_METRO, REGIONAL;
    }

    Vehicle() {

    }

    Vehicle(String owner, String regNum, Location location) {
        this.owner = owner;
        this.regNum = regNum;
        this.location = location;
    }

    public String getOwner() {
        return this.owner;
    }

    public String getRegNum() {
        return this.regNum;
    }

    public Location getLocation() {
        return this.location;
    }

    abstract double calcRegFee();

    public String toString() {
        String ret = "";
        ret += "Owner: " + this.owner + "," + "Location: " + this.location + "," + "RegNumber: " + this.regNum;
        return ret;
    }

}

class Car extends Vehicle {
    Car(String owner, String regNum, Location location) {
        this.owner = owner;
        this.regNum = regNum;
        this.location = location;
    }

    public double calcRegFee() {
        if (this.location == Location.INNER_METRO)
            return 800;
        else if (this.location == Location.OUTER_METRO)
            return 750;
        else
            return 690;
    }
}

class Motorcycle extends Vehicle {
    int engCapacity;

    Motorcycle(String owner, String regNum, Location location, int engCapacity) {
        this.owner = owner;
        this.regNum = regNum;
        this.location = location;
        this.engCapacity = engCapacity;
    }

    public int getEngCapacity() {
        return this.engCapacity;
    }

    public double calcRegFee() {
        if (this.engCapacity >= 100) {
            if (this.location == Location.INNER_METRO)
                return 500;
            else if (this.location == Location.OUTER_METRO)
                return 465;
            else
                return 420;
        } else {
            if (this.location == Location.INNER_METRO)
                return 250;
            else if (this.location == Location.OUTER_METRO)
                return 230;
            else
                return 205;
        }
    }

    public String toString() {
        String ret = "";
        ret += "Owner: " + this.owner + "," + "Location: " + this.location + "," + "RegNumber: " + this.regNum + ","
                + "EngCapacity: " + this.engCapacity;
        return ret;
    }
}

public class RegistrationTester {
    public static void main(String args[]) {
        Vehicle[] arr = new Vehicle[4];
        int type[] = { 0, 1, 0, 1 };
        String Name[] = { "John Smith", "Sandy Philips",
                "Bob Jones", "Jane Doe"
        };
        String RegNum[] = { "ABC222", "DEF333", "GHI444", "JKL555" };
        Vehicle.Location LocationArr[] = { Vehicle.Location.INNER_METRO, Vehicle.Location.OUTER_METRO,
                Vehicle.Location.REGIONAL, Vehicle.Location.INNER_METRO };
        int EngineCap[] = { 0, 80, 0, 250 };
        for (int i = 0; i < 4; i++) {
            if (type[i] == 0) {
                Car c1 = new Car(Name[i], RegNum[i], LocationArr[i]);
                arr[i] = c1;
            } else {
                Motorcycle m1 = new Motorcycle(Name[i], RegNum[i], LocationArr[i], EngineCap[i]);
                arr[i] = m1;
            }
        }
        System.out.println("Printing Data of Cars:\n");
        for (int i = 0; i < 4; i++) {
            if (type[i] == 0) {
                System.out.println(arr[i].toString());
                System.out.println("And RegFee: " + arr[i].calcRegFee());
                System.out.println("");
            }
        }
        System.out.println("\nPrinting Data of Motorcycles:\n");
        for (int i = 0; i < 4; i++) {
            if (type[i] == 1) {
                System.out.println(arr[i].toString());
                System.out.println("And RegFee: " + arr[i].calcRegFee());
                System.out.println("");
            }
        }
    }
}



Hire Expert to get help in java Project, Java Assignment and Java Homework. We are group of top rated java expert and Java professionals.


Contact Us to get instant help:


realcode4you@gmail.com

Comentarios


bottom of page