In java and use netbean please 53. Write the code for a Player class. a. Write t
ID: 3851549 • Letter: I
Question
In java and use netbean please
53. Write the code for a Player class.
a. Write the code to begin the Player class
b. Set 3 private attributes string name, int atBats and int hits
c. Write a no argument constructor
d. Write a constructor with the 3 arguments
e. Write a battingAverage method to return a double of the average
f. Write a toString which will simply show the name
g. Write a display method which will have no arguments and no return but will display each attribute on its own line.
54. Write the code for the ClientPlayer
a. Write the code to create an instance of Player called player 1 that has “Justin”, 23, 7
b. Write the code to show all the data for player 1
c. Write the code to show the last name.
Explanation / Answer
ClientPlayer.java
public class ClientPlayer {
public static void main(String[] args) {
Player player1 = new Player("Justin", 23, 7);
player1.display();
System.out.println(player1.toString());
}
}
Player.java
public class Player {
private String name;
private int atBats;
private int hits;
public Player() {
}
public Player(String name, int atBats, int hits) {
this.name = name;
this.atBats = atBats;
this.hits = hits;
}
public double battingAverage() {
return hits/(double)atBats;
}
public String toString() {
return "Name: "+name;
}
public void display() {
System.out.println("Name: "+name);
System.out.println("At Bats: "+atBats);
System.out.println("Hits: "+hits);
}
}
Output:
Name: Justin
At Bats: 23
Hits: 7
Name: Justin
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.