Question

In: Computer Science

Create a base class that describes a baseball player. A baseball player’s attributes are: player name...

Create a base class that describes a baseball player. A baseball player’s attributes are:

  • player name
  • player number

A pitcher has all the attributes of a baseball player but also has two other attributes:

  • number of innings pitched
  • number of earned runs.

All pitchers have an earned run average which is calculated as : (Earned runs / innings pitched ) x 9

A batter has all of the attributes of a baseball player plus two other attributes:

  • number of at bats
  • number of hits.

The batting average is then calculated as: Hits / at bats

Step 1- Create Player class, test all methods and make sure all is working

Step2- A Pitcher IS A Player. Create Pitcher class, test all methods and make sure all is working

Step 3- A Batter IS A Player. Create Batter class, test all methods and make sure all is working

The output should look like :

MENU
1 – Pitcher / 2 – Batter / 3 - Quit
Select player type: 2

Enter batter name: Hunter Pence
Enter player number: 08
Enter at bats: 38
Enter hits: 11

Batter Hunter Pence, Batting Average 0.289  //use toString

There should be a total of 4 java files.

Solutions

Expert Solution

Answer: Hey dear student finds the solution of your query, if you have any doubt feel free to ask. Thanks!!

Copy to code: this code has four classes, Super class and two driver classes ,one test class. All are working fine.

import java.util.*;
import java.util.Scanner;
import java.text.DecimalFormat;
//class Base player
class player
{
   String player_name;//attributes
   int player_num;
  
   public player(String na,int num)//constructor
   {
       this.player_name = na;
       this.player_num = num;
   }
   public String toString()//toString method
   {
       return(player_name+" ");
}
}
class pitcher extends player//class pitcher that extends Base/super class player
{
   int innings;//attributes of pitcher class
   int runs;
   public pitcher(String name,int num,int inn,int run)
   {
       super(name,num);//call super class constructor
       this.innings = inn;
       this.runs = run;
   }
   public double calcAvg()//avg()
   {
       double avg;
       avg = (runs/innings)*9;//calculate average
       return avg;//return average
   }
   public String toString()//toString()
   {
       return("Pitcher "+super.toString());
}
}
class batter extends player//class batter that extends super class player
{
private static DecimalFormat df2 = new DecimalFormat("#.###");//to print decimal places
   int number_bats;//attrubutes
   int number_hits;
   public batter(String na,int num,int bats,int hits)
   {
       super(na,num);//call constructor of super class
       this.number_bats = bats;
       this.number_hits = hits;
   }
   public double Avg()//average method to calculate average
   {
       double avg;
       avg = (double)number_hits/(double)number_bats;//calculate average
       return avg;
   }
   public String toString()//toString()
   {
       return("Batter "+super.toString()+"Batting average "+df2.format(Avg()));
}
}

class Main
{
public static void quit()//quit()
{
System.exit(0);
}
   public static void main(String []args)
   {
  
       int player,num_player,inns,num_runs,hits,bats;
       double average = 0.0;
       String name;
       Scanner sc = new Scanner(System.in);//create object of Scanner
       Scanner in = new Scanner(System.in);
       System.out.println("MENU"+"\n1- Pitcher"+"\n2- Batter"+"\n3- Quit");
       System.out.println("Select player type: ");
       player = sc.nextInt();//ask for choice from MENU
       if(player==1) //if select 1
       { //prompt for necessary info from user
           System.out.println("Enter pitcher name: ");
           name = in.nextLine();
           System.out.println("Enter player number: ");
           num_player = sc.nextInt();
           System.out.println("Enter number of innings: ");
           inns = in.nextInt();
           System.out.println("Enter number of runs: ");
           num_runs = in.nextInt();
           pitcher pt = new pitcher(name,num_player,inns,num_runs);//create object of class putcher
           average = pt.calcAvg();//call average method
           System.out.print(pt.toString()+" ");
       System.out.print("Run Average "+average);
       }
       else
       if(player == 2)//if select 2
       { //prompt for necessary info from user
       System.out.println("Enter batter name: ");
           name = in.nextLine();
           System.out.println("Enter player number: ");
           num_player = sc.nextInt();
           System.out.println("Enter at bats: ");
           bats = in.nextInt();
           System.out.println("Enter hits: ");
           hits = in.nextInt();
           batter bt = new batter(name,num_player,bats,hits);//create object of batter class
           System.out.print(bt.toString()+" ");//toString()
       }
       else
       if(player == 3)//if select 3 exit from program
       {
       quit();
       }
      
   }
}
          

Snapshots of the code:


Related Solutions

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 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...
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 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...
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...
Create a class Client. Your Client class should include the following attributes: Company Name (string) Company...
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 the common...
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
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...
The on-base percentage (OBP) indicates how successful a baseball player is as a batter. The dataset...
The on-base percentage (OBP) indicates how successful a baseball player is as a batter. The dataset OBP in UsringR contains the OBP for the year 2000. Test whether these data come from normal distribution. Use R to solve and show R code
Programming Language: C++ Create a base class called Shape which has 2 attributes: X and Y...
Programming Language: C++ Create a base class called Shape which has 2 attributes: X and Y (positions on a Cartesian coordinate system). Since a shape is amorphous, set the class up so that an object of type Shape can not be instantiated. Create three derived classes of your choice whose base class is Shape. These derived classes should have accessors/mutators for their class specific attributes, as well as methods to compute the area and the perimeter of the shape. In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT