In: Computer Science
Create a base class that describes a baseball player. A baseball player’s attributes are:
A pitcher has all the attributes of a baseball player but also has two other attributes:
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:
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.
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: