Question

In: Computer Science

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 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.

Solutions

Expert Solution

As given in the question I have provided the below code in Java which works fine and thus I have attached an output screenshot too below.

I have created an extra method display() in the SoccerPlayer class so as to display the information of that player. Rest all the methods and attributes are created according to the question only.

Code ScreenShots:

Code:

class SoccerPlayer {
    private String firstName;
    private String lastName;
    private int games;
    private int goals;

    public SoccerPlayer()
    {
        firstName=null;
        lastName=null;
        games=0;
        goals=0;
    }

    public SoccerPlayer(String fname,String lname,int games,int goals)
    {
        firstName=fname;
        lastName=lname;
        this.games=games;
        this.goals=goals;
    }

    public void setFirstName(String fname)
    {
        firstName=fname;
    }

    public void setLastName(String lname)
    {
        lastName=lname;
    }

    public void setGames(int games)
    {
        this.games=games;
    }

    public void setGoals(int goals)
    {
        this.goals=goals;
    }

    public double averageGoals()
    {
        double avg;

        if(games==0)
        {
            avg=0;
            System.out.println("Error: No games played till by the Player");
            return avg;
        }

        avg=(double)this.goals/this.games;
        return avg;
    }

    public void display()
    {
        System.out.println("Stats : \n\t Name: "+ firstName+" "+lastName+"\t Games played: "+games+"\t Goals Scored: "+goals+"\t Average Goals per Game: "+averageGoals());
    }
}

public class SoccerPlayerTest
{
    public static void main(String[] args) {

        SoccerPlayer player1 = new SoccerPlayer();
        SoccerPlayer player2 = new SoccerPlayer("lio","messi",143,115);

        player1.setFirstName("chris");
        player1.setLastName("ronaldo");
        player1.setGames(170);
        player1.setGoals(130);

        player1.display();
        player2.display();
    }
}

Output:

If there is any doubt left regarding the code do left a comment in the comments section.


Related Solutions

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...
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...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
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...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name (string) Company id (string) Billing address (string) Billing city (string) Billing state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyClient that inherits from the Client class.   HourClient must use the inherited parent class variables and add in hourlyRate and hoursBilled. Your Hourly Client class should contain a constructor that calls the constructor from the Client class to initialize...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT