Question

In: Computer Science

JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games,...

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

Solutions

Expert Solution

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:


Related Solutions

Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java 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...
Specifications Create an abstract Employee class that provides private attributes for the first name, last name,...
Specifications Create an abstract Employee class that provides private attributes for the first name, last name, email address, and social security number. This class should provide functions that set and return the employee’s first name, last name, email address, and social security number. This class has a function: get_net_income which returns 0. Create a Manager class that inherits the Employee class. This class should add private attributes for years of experience and the annual salary. This class should also provide...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyEmployee that inherits from the Employee class.   HourEmployee must use the inherited parent class variables and add in HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the...
Java Create an abstract Product Class Add the following private attributes: name sku (int) price (double)...
Java Create an abstract Product Class Add the following private attributes: name sku (int) price (double) Create getter/setter methods for all attributes Create a constructor that takes in all attributes and sets them Remove the default constructor Override the toString() method and return a String that represents the building object that is formatted nicely and contains all information (attributes) Create a FoodProduct Class that extends Product Add the following private attributes: expDate (java.util.Date) for expiration date refrigerationTemp (int) if 70...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT