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

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...
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
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
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
The following is a chart of 25 baseball players' salaries and statistics from 2016. Player Name...
The following is a chart of 25 baseball players' salaries and statistics from 2016. Player Name RBI's HR's AVG Salary (in millions) Joe Mauer 49 11 0.261 23.000 Robinson Cano 103 39 0.298 24.050 Leonys Martin 47 15 0.245 4.150 Brandon Crawford 84 12 0.275 6.000 Colby Rasmus 54 15 0.206 15.800 Carlos Gonzalez 100 25 0.298 17.454 Matt Kemp 108 35 0.268 21.500 Prince Fielder 44 8 0.212 18.000 Mark Teixeira 44 15 0.204 23.125 Yoenis Cespedes 86 31...
The following is a chart of 25 baseball players' salaries and statistics from 2019. Player Name...
The following is a chart of 25 baseball players' salaries and statistics from 2019. Player Name RBI's HR's AVG Salary (in millions) Adam Jones 67 16 0.260 4.500 Marwin Gonzalez 55 15 0.264 12.000 Wil Myers 53 18 0.239 5.500 Christian Vazquez 72 23 0.276 2.850 Adam Eaton 49 15 0.279 8.400 Joey Votto 47 15 0.261 25.000 Stephen Piscotty 44 13 0.249 7.333 DJ LeMahieu 102 26 0.327 12.000 Max Kepler 90 36 0.252 6.000 Christian Yelich 97 44...
Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
How to write a Java program that has a base class with methods and attributes, subclasses...
How to write a Java program that has a base class with methods and attributes, subclasses with methods and attributes. Please use any example you'd like.
1) Create a table “college” that has as attributes name, city name, state/province/region name, country name,...
1) Create a table “college” that has as attributes name, city name, state/province/region name, country name, and year in which the college was established as well as an ID as primary key. Insert records for 5 colleges of your choice, including one or more you have attended. 2) Create a table “student” that has as attributes first name, last names, and college ID for students, and insert the names of yourself and friends who attended one or more colleges together...
In python- Create a class defined for Regression. Class attributes are data points for x, y,...
In python- Create a class defined for Regression. Class attributes are data points for x, y, the slope and the intercept for the regression line. Define an instance method to find the regression line parameters (slope and intercept). Plot all data points on the graph. Plot the regression line on the same plot.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT