Football Stats in Java Homework Sample

Write a program for Football (or Soccer as it is known in America) statistics. You need a Player class that represents the base instance, and Goalkeeper and Attacker subclasses. The Player class has 2 attributes which are name and age. Attackers have goals scored and goals missed, and Goalkeepers have goals saved and goals conceded. There are 2 virtual methods in Player, which are TotalPoints which returns the number of points for that player, and printInfo which displays the statistics for a player. For an attacker the TotalPoints are 3 points for each goal scored and -1 for each goal missed, and for a goalkeeper 1 point for each goal saved and -1 point for each goal conceded. For Java assignment help like this involving object orientated inheritance contact us for details.

Solution:

main.java

public class main {

public static void main(String[] args){
// create instances of Attacker and Goalkeeper
Attacker a = new Attacker(“Lionel Messi”, 29, 3, 0);
Goalkeeper g = new Goalkeeper(“Petr Cech”, 34, 5, 1);

// printInfo of both the players
a.printInfo();
System.out.println();
g.printInfo();

// check who is leading
// output the leading player with points
if(a.isLeading(g)){
System.out.println(a.getName() + ” leads ” + g.getName() + ” by ” + (a.getTotalPoints() – g.getTotalPoints()) + ” points.”);
} else {
System.out.println(g.getName() + ” leads ” + a.getName() + ” by ” + (g.getTotalPoints() – a.getTotalPoints()) + ” points.”);
}

// program finished
System.out.println(“\nProgram Completed”);
}
}

Attacker.java

public class Attacker extends Player{

private int scored;
private int missed;

// default constructor for Attacker
public Attacker(String name, int age, int scored, int missed){
// call Player constructor
super(name, age);

this.scored = scored;
this.missed = missed;

// set total points for Player
setTotalPoints(scored*3 – missed);
}

@Override
public void printInfo() {
// call Player printinfo to display basic details
super.printInfo();

// print other details
System.out.println(“Type: Attacker”);
System.out.println(“Goals Scored: ” + scored);
System.out.println(“Goals Missed: ” + missed);
System.out.println(“Total Points: ” + getTotalPoints());
}
}

Goalkeeper.java

public class Goalkeeper extends Player{

private int saved;
private int conceded;

// default constructor for Goalkeeper
public Goalkeeper(String name, int age, int saved, int conceded){
// call Player constructor
super(name, age);

this.saved = saved;
this.conceded = conceded;

// set total points for Player
setTotalPoints(saved – conceded);
}

@Override
public void printInfo() {
// call Player printinfo to display basic details
super.printInfo();

// print other details
System.out.println(“Type: GoalKeeper”);
System.out.println(“Goals Saved: ” + saved);
System.out.println(“Goals conceded: ” + conceded);
System.out.println(“Total Points: ” + getTotalPoints());
}

}

Player.java

public class Player {
private String name;
private int age;
private int totalPoints;

// default constructor for player
public Player(String name, int age){
this.name = name;
this.age = age;
this.totalPoints = totalPoints;
}

// get methods to access private variables
public int getAge() {
return age;
}

public int getTotalPoints() {
return totalPoints;
}

public String getName() {
return name;
}

// set methods for private variables
public void setAge(int age) {
this.age = age;
}

public void setName(String name) {
this.name = name;
}

public void setTotalPoints(int totalPoints) {
this.totalPoints = totalPoints;
}

// print basic details of player
public void printInfo(){
System.out.println(“Name: ” + name);
System.out.println(“Age: ” + age + ” years”);
}

// compare two players
public boolean isLeading(Player player){
return totalPoints >= player.getTotalPoints();
}
}