Results 1 to 2 of 2
  1. #1
    JKM
    JKM is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default 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:
    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.
    My code so far:
    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("-------------------");
        }
    }
    Please give me some help here, and please show me some code! Thanks for any help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 4
    Last Post: 01-01-2009, 12:03 PM
  2. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 07:59 AM
  3. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:28 PM
  4. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •