Results 1 to 5 of 5
  1. #1
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default classes and creating objects

    Create a Dice class that has a face data field (that indicates the current value showing). The class should have a getFace method which returns the current value of face, and a roll method that chooses a random value for face. Write a main program that creates 2 Dice objects. The main program should roll both dice and display the value showing for each. Be sure to use proper style. Create a jar file of your program and attach to this assignment page.

    This is what I have:

    Java Code:
    public class Dice
    {
    int face = 6;
    
    
    // returns the current value of face
     int getFace()
     {
         return face;
     }
     
     
     int roll()
     {
        return 1 + (int)(Math.random() * 7); 
     }
     
    
    }
    
    public class diceGame
    {
        public static void main(String[] args)
        {
            Dice dice1 = new Dice();
            
            System.out.println("dice1 is " + dice1.getFace());
            
            
            Dice dice2 = new Dice();
            
            System.out.println("dice2 is " + dice2.roll());
        }
    }
    Is this correct?
    Last edited by Latanyar; 11-01-2010 at 04:28 PM.

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

    Default

    No, it's not correct. The roll() method should change face and that's it. It shouldn't return an int. Rather the result of the roll is obtained by calling getFace(). And your formula for rolling is not right as it will allow a result of 7 (check it and see).

    Also, you will want to test your code to find out best if it works or not.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    <pedant>...and it's Die not Dice in the singular...</pedant>
    ;)

  4. #4
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, it's not correct. The roll() method should change face and that's it. It shouldn't return an int. Rather the result of the roll is obtained by calling getFace(). And your formula for rolling is not right as it will allow a result of 7 (check it and see).

    Also, you will want to test your code to find out best if it works or not.
    Could you give me an example, because I am really confused about this. I changed the math.random to 6.

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

Similar Threads

  1. Assign objects to classes
    By dellacpa in forum New To Java
    Replies: 4
    Last Post: 08-02-2010, 09:27 PM
  2. Classes and Objects Help
    By collin389 in forum New To Java
    Replies: 1
    Last Post: 12-14-2009, 12:44 AM
  3. Constructors Objects and Classes
    By Tykk in forum New To Java
    Replies: 4
    Last Post: 10-10-2009, 11:31 PM
  4. classes as objects
    By kroiz in forum New To Java
    Replies: 4
    Last Post: 07-25-2009, 05:22 AM
  5. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 08:05 AM

Posting Permissions

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