Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Properly using convert methods and arrays

    I'm working on a project for my introductory java programming class that requires me to create an array of 10 items, and based on the result of a math.random function, pulls the corresponding array position, which will be a piece of treasure stored as a String. I have three classes that I will post below of what I have so far. I also need to use a getter function in the player class that is always used to retrieve the object number, but I'm having trouble creating it.

    Code:
    <<player class>>
    public class jmp08cplayer_a2 {
    
    int number = 0;
    int n;
    String t;
    String Treasure;
    
    
    void setTreasure(){
    Treasure = t;
    }
    
    String getTreasure(){
    return Treasure;
    }
    
    public void guess() {
    number = (int) (Math.random() * 10);
    n = number;
    String object[] = new String[10];
    object[0] = "Gold Coin";
    object[1] = "Silver Coin";
    object[2] = "Bronze Coin";
    object[3] = "Gold Statuette";
    object[4] = "Silver Statuette";
    object[5] = "Bronze Statuette";
    object[6] = "Emerald Gem";
    object[7] = "Topaz Gem";
    object[8] = "Amethyst Gem";
    object[9] = "Ruby Gem";
    object[n] = Treasure;
    object[n] = t;
    System.out.println("I'm guessing " +getTreasure()+ "");
    
    }
    
    
    }
    
    <<guess game>>
    
    public class jmp08cguessgame_a2 {
    jmp08cplayer_a2 p1;
    jmp08cplayer_a2 p2;
    jmp08cplayer_a2 p3;
    
    public void startGame() {
    p1 = new jmp08cplayer_a2();
    p2 = new jmp08cplayer_a2();
    p3 = new jmp08cplayer_a2();
    
    
    
    int guessp1 = 0;
    int guessp2 = 0;
    int guessp3 = 0;
    //int guessp3 = 0;
    
    boolean p1isRight = false;
    boolean p2isRight = false;
    boolean p3isRight = false;
    
    int targetNumber = (int) (Math.random() * 10);
    
    
    
    System.out.println("I'm thinking of a piece of treasure in my chest containing 10 pieces of treasure");
    
    
    while(true) {
    System.out.println("The treasure piece to guess is " +object[n].setTreasure) ;
    
    p1.guess();
    p2.guess();
    p3.guess();
    
    guessp1 = p1.number;
    System.out.println("Player one guessed " +guessp1);
    
    guessp2 = p2.number;
    System.out.println("Player two guessed " +guessp2);
    
    guessp3 = p3.number;
    System.out.println("Player three guessed " +guessp3);
    
    if (guessp1 == targetNumber) {
    p1isRight = true;
    }
    if (guessp2 == targetNumber) {
    p2isRight = true;
    }
    if (guessp3 == targetNumber) {
    p3isRight = true;
    }
    if (p1isRight || p2isRight || p3isRight){
    System.out.println("We have a winner!");
    System.out.println("Player one got it right? " +p1isRight);
    System.out.println("Player two got it right? " +p2isRight);
    System.out.println("Player three got it right? " +p3isRight);
    System.out.println("Game is over.");
    break;
    }else{
    System.out.println("Players will have to try again.");
    }
    }
    }
    
    The last class I have is just used to run the actual game when it starts.
    <<Launcher>>
    public class jmp08cgamelauncher_a2 {
    public static void main (String[] args) {
    jmp08cguessgame_a2 game = new jmp08cguessgame_a2();
    game.startGame();
    
    }
    
    }
    Thank you in adavnce
    I am aware there are some mistakes
    Last edited by ShaneC; 02-04-2011 at 10:17 PM. Reason: Added [CODE] tags

  • #2
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I'm not sure I understood well your question, but I've improved your coding style, made you getters and neutralized null pointer problems:

    Code:
    public class jmp08cplayer_a2 {
    
        private int number = 0;
        private int n;
        private String t;
        private String Treasure;
        private static String object[] = new String[10];
    
        public static String[] getObjects()
        {
            return object;
        }
    
        public int getNumber()
        {
            return number;
        }
    
        public static void initialize()
        {
            object[0] = "Gold Coin";
            object[1] = "Silver Coin";
            object[2] = "Bronze Coin";
            object[3] = "Gold Statuette";
            object[4] = "Silver Statuette";
            object[5] = "Bronze Statuette";
            object[6] = "Emerald Gem";
            object[7] = "Topaz Gem";
            object[8] = "Amethyst Gem";
            object[9] = "Ruby Gem";
        }
    
        public void setTreasure() {
            Treasure = t;
        }
    
        public String getTreasure() {
            return Treasure;
        }
    
        public int getObjectNumber()
        {
            return n;
        }
    
        public void guess() {
            number = (int) (Math.random() * 10);
            n = number;
    //        String object[] = new String[10];
            Treasure = object[n];
            t = object[n];
            this.n = n;
            System.out.println("I'm guessing " + getTreasure() + "");
    
        }
    }
    Code:
    public class jmp08cguessgame_a2 {
    
        jmp08cplayer_a2 p1;
        jmp08cplayer_a2 p2;
        jmp08cplayer_a2 p3;
    
        public void startGame() {
            jmp08cplayer_a2.initialize();
            p1 = new jmp08cplayer_a2();
            p2 = new jmp08cplayer_a2();
            p3 = new jmp08cplayer_a2();
    
    
    
            int guessp1 = 0;
            int guessp2 = 0;
            int guessp3 = 0;
    //int guessp3 = 0;
    
            boolean p1isRight = false;
            boolean p2isRight = false;
            boolean p3isRight = false;
    
            int targetNumber = (int) (Math.random() * 10);
    
    
    
            System.out.println("I'm thinking of a piece of treasure in my chest containing 10 pieces of treasure");
    
    
            while (true) {
                System.out.println("The treasure piece to guess is " + jmp08cplayer_a2.getObjects()[targetNumber]);
    
                p1.guess();
                p2.guess();
                p3.guess();
    
                guessp1 = p1.getNumber();
                System.out.println("Player one guessed " + guessp1);
    
                guessp2 = p2.getNumber();
                System.out.println("Player two guessed " + guessp2);
    
                guessp3 = p3.getNumber();
                System.out.println("Player three guessed " + guessp3);
    
                if (guessp1 == targetNumber) {
                    p1isRight = true;
                }
                if (guessp2 == targetNumber) {
                    p2isRight = true;
                }
                if (guessp3 == targetNumber) {
                    p3isRight = true;
                }
                if (p1isRight || p2isRight || p3isRight) {
                    System.out.println("We have a winner!");
                    System.out.println("Player one got it right? " + p1isRight);
                    System.out.println("Player two got it right? " + p2isRight);
                    System.out.println("Player three got it right? " + p3isRight);
                    System.out.println("Game is over.");
                    break;
                } else {
                    System.out.println("Players will have to try again.");
                }
            }
        }
    }
    Code:
    public class jmp08cgamelauncher_a2 {
    
        public static void main(String[] args) {
            jmp08cguessgame_a2 game = new jmp08cguessgame_a2();
            game.startGame();
    
        }
    }


  •  

    Posting Permissions

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