I am really stuck on this one... here are the instructions: The player class: At
ID: 3638534 • Letter: I
Question
I am really stuck on this one...here are the instructions:
The player class:
Attributes:
String firstName, lastName
int idNumber, chips
All attributes are private.
Constructors:
Three constructors:
1 - a default constructor – nothing in the parameters and every value blank or zero.
2 - A constructor with four parameters, first name, last name, idNum, chips
3 - A constructor with two parameters idNum and chips. Use scanner to have the user enter the first name and last name.
Methods:
The methods to be created are:
Getters and setters for each attribute.
o Getters (observer) returns the value of each attribute.
o Setters takes as input 1 parameter and sets the value of an attribute
? Additionally create three methods: addChips, subChips, displayPlayer
o addChips will take a integer parameter (int data type) and add it to the total chips;
o subChips will take an integer parameter (int data type) and subtract it from the total chips.
This is what I have so far....
public class Player {
private String firstName;
private String lastName;
private int idNumber;
private int chips;
public Player() {
firstName = " ";
lastName = " ";
idNumber = 0;
chips = 0;
}
public Player(String fName, String lName, int ID, int ch){
firstName = fName;
lastName = lName;
idNumber = ID;
chips = ch;
}
Explanation / Answer
PLEASE RATE import java.util.Scanner; public class Player { public static void main(String[] args) { Player p1 = new Player(); Player p2 = new Player("John", "Smith", 12345, 100); Player p3 = new Player(12346, 100); displayPlayer(p1); displayPlayer(p2); displayPlayer(p3); p1.setFName("John"); p1.setLName("Doe"); p1.setIDNum(12344); displayPlayer(p1); p1.setChips(1); System.out.println(p1.getChips()); p1.addChips(1); System.out.println(p1.getChips()); p1.subChips(2); System.out.println(p1.getChips()); } private String firstName; private String lastName; private int idNumber; private int chips; public Player() { this.firstName = ""; this.lastName = ""; this.idNumber = 0; this.chips = 0; } public Player(String fName, String lName, int ID, int ch){ this.firstName = fName; this.lastName = lName; this.idNumber = ID; this.chips = ch; } public Player(int ID, int ch) { this.idNumber = ID; this.chips = ch; Scanner scan = new Scanner(System.in); System.out.println("Enter First name:"); String n = scan.nextLine(); this.firstName = n; System.out.println("Enter Last name:"); String m = scan.nextLine(); this.lastName = m; } public String getFName() { return this.firstName; } public String getLName() { return this.lastName; } public int getIDNum() { return this.idNumber; } public int getChips() { return this.chips; } public void setFName(String name) { this.firstName = name; } public void setLName(String name) { this.lastName = name; } public void setIDNum(int id) { this.idNumber = id; } public void setChips(int chips) { this.chips = chips; } public void addChips(int n) { this.chips += n; } public void subChips(int n) { this.chips -= n; } public static void displayPlayer(Player p) { System.out.println("First name: " + p.getFName()); System.out.println("Last name: " + p.getLName()); System.out.println("ID: " + p.getIDNum()); System.out.println("Chips: " + p.getChips()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.