Results 1 to 2 of 2
- 02-01-2009, 10:03 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 1
- Rep Power
- 0
Need some help with a task, constructors.
Hi there, I've got this task here, and I was wondering if someone could help me out. I wan't to know if my code is answering my task.
The task:
My code so far:BlueJ casino in Las Vegas has given you this task to create a class where they can register their players. You should create a new project and a class called Player. The purpose with this task is that player information could be captured to a secure object and that it couldn't be changed arbitrary.
The class Player should therefore contain the private fields 'firstname', 'lastname' and 'credits'.
The official methods that player should define, does the following:
- setFirstname: Sets the first name to the player
- getFirstname: Returns the first name to the player
- setLastname: Sets the last name to the player
- getLastname: Returns the last name to the player
- addCredits: Adds the player's credits to a given amount
- getCredits: Returns the player's credits.
- validPlayer: Checks if the player credits is higher greater zero, with an if sentence. If it is greater, it should return 'true', otherwise, it should return 'false'.
- print: Prints the player information to the terminal window (with System.out.println).
The class Player should also have a constructor that gets two arguments/current parameters to initialize the Player object:
- A text string that represents the player's first name.
- A text string that represents the player's last name.
- The constructor should also set the player's credits to 100.
Please give me some help here, and please show me some code! Thanks for any help!Java Code:public class Player { private String firstname; private String lastname; private int credits; public Player(String firstname, String lastname, int credits) { this.firstname = ""; this.lastname = ""; this.credits = 100; } public void setFirstname(String name) { firstname = name; } public String getFirstname() { return firstname; } public void setLastname(String name) { lastname = name; } public String getLastname() { return lastname; } public void addCredits(int amount) { credits = credits + amount; } public int getCredits() { return credits; } public boolean validPlayer() { if(getCredits() > 0){ return true; } else { return false; } } public void print() { System.out.println("PLAYER INFORMATION:"); System.out.println("First name: " + getFirstname()); System.out.println("Last name: " + getLastname()); System.out.println("Credit: " + getCredits()); System.out.println("-------------------"); } }
-
Similar Threads
-
Constructors, Setter & Getter << need some explaining
By A.M.S in forum New To JavaReplies: 4Last Post: 01-01-2009, 12:03 PM -
Help with constructors
By Minime in forum New To JavaReplies: 3Last Post: 04-09-2008, 07:59 AM -
Initializing variables using constructors
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 08:28 PM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks