Answer:
class BaseballPlayer
{
private int hits;
private int atBats;
private String name;
public BaseballPlayer(String n,int h,int a)
{
name=n;
hits=h;
atBats=a;
}
public void printBattingAverage()
{
double battingAverage = hits / (double)atBats;
System.out.println(battingAverage);
}
public String toString()
{
return name + ": "+hits+"/"+atBats;
}
}
public class Baseballtester{
public static void main(String[] args){
BaseballPlayer babeRuth = new BaseballPlayer("Babe Ruth", 2873, 8399);
System.out.println(babeRuth);
babeRuth.printBattlingAverage();
}
}
Explanation:
The BaseballPlayer class is used to get and hold data of an instance of a baseball player. the instance object holds the name, number of hits and bats of the player.
The constructor is used to initialize the name, hits and atBats variables of an instance. The "printBattlingAverage" method returns the ratio of the hits and atBat variable while the string method "toString" returns the name and the hits to atBats ratio in string format.