In: Computer Science
JAVA Program
Create a class called SoccerPlayer
Create 4 private attributes: First Name, Last Name, Games, and Goals
Have two constructors
Constructor 1 – default constructor; all values to "NONE" or zero
Constructor 2 – accepts input of first name, last name, games and goals.
Create get and set methods for each of the four attributes
Create a method the returns a double that calculates the average goals per game
This method checks for zero games played:
If there are zero played, display an error and set average to 0;
If greater than zero, do the math and set average to result of calculation
Create a test program that allows you to set the first name, last name, number of games and number of goals. Call it SoccerPlayerTest.
Create two instances of players.
The first should use the default constructor and the set methods to fill the attributes
The second should use the other constructor to set the attributes
Display the info about the players including the average goals per game.
Use the same SoccerPlayer class you created in CE-SoccerPlayer
Create a test program that allows you to enter in the first name, last name, number of games and number of goals. Call it SoccerPlayerTest.
Create an array of players. There should be three players.
Create a loop and ask for the information about each player.
Create a loop and display the info about the players including the average goals per game
Solution:
Program implementation:
import java.util.*;
class SoccerPlayer{
// creates four private instance variables.
private String FirstName;
private String LastName;
private int Games;
private int Goals;
// Non-Parameterised constructor.
public SoccerPlayer(){
this.FirstName="NONE";
this.LastName="NONE";
this.Games=0;
this.Goals=0;
}
// Parameterised constructor,
public SoccerPlayer(String FirstName,String LastName,int Games,int
Goals){
this.FirstName=FirstName;
this.LastName=LastName;
this.Games=Games;
this.Goals=Goals;
}
// avg_goals() method returns the avgerage goals scored.
public double avg_goals(){
double avg=0.0;
if(this.Games==0){
System.out.println("ERROR!!!");
avg=0;
}
else if(this.Games>0){
avg=(double)this.Goals/this.Games;
}
return avg;
}
// get() method for all the four instance variables.
public String get_FirstName(){
return this.FirstName;
}
public String get_LastName(){
return this.LastName;
}
public int get_Games(){
return this.Games;
}
public int get_Goals(){
return this.Goals;
}
// set() method for all the four instance variables.
public void set_FirstName(String FirstName){
this.FirstName=FirstName;
}
public void set_LastName(String LastName){
this.LastName=LastName;
}
public void set_Games(int Games){
this.Games=Games;
}
public void set_Goals(int Goals){
this.Goals=Goals;
}
}
// Test class.
public class SoccerPlayerTest{
// main method.
public static void main(String []args){
SoccerPlayer p1=new SoccerPlayer();// creates an object of
SoccerPlayer class.
SoccerPlayer p2[]=new SoccerPlayer[3];// creates array of three
objects of SoccerPlayer class.
Scanner sc=new Scanner(System.in);// creates an object of scaneer
class.
String FirstName;
String LastName;
int Games;
int Goals;
for(int i=0;i<3;i++){
// takes input from user and using Parameterised constructor
populates the objects.
FirstName=sc.next();
LastName=sc.next();
Games=sc.nextInt();
Goals=sc.nextInt();
p2[i]=new SoccerPlayer(FirstName,LastName,Games,Goals);
}
//Display details of all the players along with their
average.
System.out.println("Player Details");
for(int i=0;i<3;i++){
System.out.println("FirstName: "+p2[i].get_FirstName());
System.out.println("LastName: "+p2[i].get_LastName());
System.out.println("Games: "+p2[i].get_Games());
System.out.println("Goals: "+p2[i].get_Goals());
System.out.println("Average: "+p2[i].avg_goals());
}
}
}
Program screenshot:
Program input and output screenshot: