Java help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FFRGreen
    Greeeeeeeeeeeeeeeeeeeeeen
    • Oct 2010
    • 1151

    #1

    Java help

    anyone here that knows java and wants to help me?

    There is this program I have to make a random dice generator and I have to have one die object but let it generate 3 numbers anyone know how to do it?


    Code:
    class DieRoll {
    	public static void main ( String [] args) {
    		
    	String space;
    	
    	space = " ";
    	Die die;
    	
    	
    	die = new Die();
    
    	
    	
    	die.roll();
    
    	
    	System.out.println("Your Results are" + space + die.getRoll());
    	
    	}
    	
    }
    forgot to add the other file
    Code:
    import java.util.*;
    
    class Die {
    	private int number;
    	private static final int MAX_NUMBER = 6;
    	private static final int MIN_NUMBER = 1;
    	private static final int NO_NUMBER = 0;
    	
    	//Constructor
    	
    	public Die() {
    	number = NO_NUMBER;
    	}
    	
    	//Roll a Die
    	public void roll() {
    		number = (int) Math.floor((Math.random() * (MAX_NUMBER - MIN_NUMBER +1) +1));
    	}
    	
    	// Get number from roll
    	public int getRoll() {
    		return number;
    	}
    }
    thats all I got so far.

    I guess I can offer you credits
    Last edited by FFRGreen; 09-20-2011, 10:29 PM.
  • Sprite-
    HAVE ME WITH VODKA
    FFR Simfile Author
    • Aug 2005
    • 7316

    #2
    Re: Java help

    I took two semesters of it back in college. Not sure if I remember enough to help you. Why not ask the problem in your post so others can respond in the thread to it?

    Comment

    • FFRGreen
      Greeeeeeeeeeeeeeeeeeeeeen
      • Oct 2010
      • 1151

      #3
      Re: Java help

      Originally posted by Sprite-
      I took two semesters of it back in college. Not sure if I remember enough to help you. Why not ask the problem in your post so others can respond in the thread to it?
      Don't know why I didn't

      Comment

      • Zageron
        Zageron E. Tazaterra
        FFR Administrator
        • Apr 2007
        • 6592

        #4
        Re: Java help

        So post it.

        Comment

        • FFRGreen
          Greeeeeeeeeeeeeeeeeeeeeen
          • Oct 2010
          • 1151

          #5
          Re: Java help

          Originally posted by Zageron
          So post it.
          already ahead of you

          Comment

          • Zageron
            Zageron E. Tazaterra
            FFR Administrator
            • Apr 2007
            • 6592

            #6
            Re: Java help

            Well I'll tell you one thing you need.

            Code:
            import java.util.Random;
            
            public int roll() {
            	return generator.nextInt(6) + 1;
            }
            This function would appear in the die class.

            If you aren't required to be object oriented, then there would be no need to create a die class at all.
            Just add a function in the DieRoll class that does the same thing, and call that.

            ----------------------------------
            Never mind you can't do that, deleted it.
            You need to make a loop.

            Create a string to store your answers in.
            Loop: 3 times
            die.roll();
            string += die.getRoll() + " ";

            Simplified way of doing it.
            Last edited by Zageron; 09-20-2011, 10:43 PM.

            Comment

            • FFRGreen
              Greeeeeeeeeeeeeeeeeeeeeen
              • Oct 2010
              • 1151

              #7
              Re: Java help

              Originally posted by Zageron
              Well I'll tell you one thing you need.

              Code:
              import java.util.Random;
              
              public int roll() {
              	return generator.nextInt(6) + 1;
              }
              This function would appear in the die class.

              If you aren't required to be object oriented, then there would be no need to create a die class at all.
              Just add a function in the DieRoll class that does the same thing, and call that.

              ----------------------------------
              Never mind you can't do that, deleted it.
              You need to make a loop.

              Create a string to store your answers in.
              Loop: 3 times
              die.roll();
              string += die.getRoll() + " ";

              Simplified way of doing it.
              What do you mean to create a string to store your answers in.

              Comment

              • rushyrulz
                Digital Dancing!
                FFR Simfile Author
                FFR Music Producer
                • Feb 2006
                • 12985

                #8
                Re: Java help

                he means string += die.getRoll() + " " lol


                Comment

                • FFRGreen
                  Greeeeeeeeeeeeeeeeeeeeeen
                  • Oct 2010
                  • 1151

                  #9
                  Re: Java help

                  Originally posted by rushyrulz
                  he means string += die.getRoll() + " " lol
                  I feel stupid because I don't get what you are saying lol

                  Comment

                  • Zageron
                    Zageron E. Tazaterra
                    FFR Administrator
                    • Apr 2007
                    • 6592

                    #10
                    Re: Java help

                    String result = "";

                    Would create a string to store your answer in.

                    Then in a loop, you roll the die, and then acquire the roll result.
                    The roll result is then appended on to the string.

                    die.getRoll() will be a number, so a toString method will likely be required.

                    Google is your friend! The java documentation never hurt either.

                    Comment

                    • FFRGreen
                      Greeeeeeeeeeeeeeeeeeeeeen
                      • Oct 2010
                      • 1151

                      #11
                      Re: Java help

                      Originally posted by Zageron
                      String result = "";

                      Would create a string to store your answer in.

                      Then in a loop, you roll the die, and then acquire the roll result.
                      The roll result is then appended on to the string.

                      die.getRoll() will be a number, so a toString method will likely be required.

                      Google is your friend! The java documentation never hurt either.
                      Its just that I never learned that before so this is kind of foreign to me.

                      Comment

                      • Patashu
                        FFR Simfile Author
                        FFR Simfile Author
                        • Apr 2006
                        • 8609

                        #12
                        Re: Java help

                        You haven't done strings yet or you haven't done loops yet? You can just google about both anyway
                        Patashu makes Chiptunes in Famitracker:
                        http://soundcloud.com/patashu/8bit-progressive-metal-fading-world
                        http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png
                        Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1)
                        http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png

                        Comment

                        • MaxGhost
                          FFR Veteran
                          • Feb 2008
                          • 2141

                          #13
                          Re: Java help

                          Originally posted by FFRGreen
                          anyone here that knows java and wants to help me?

                          There is this program I have to make a random dice generator and I have to have one die object but let it generate 3 numbers anyone know how to do it?


                          Code:
                          class DieRoll {
                          	public static void main ( String [] args) {
                          		Die die = new Die();
                          		System.out.println("Your Results are " + die.roll());
                          	}
                          }
                          forgot to add the other file
                          Code:
                          import java.util.*;
                          
                          class Die {
                          	private int number = 0;
                          	private static final int MAX_NUMBER = 6;
                          	private static final int MIN_NUMBER = 1;
                          	private static final int NO_NUMBER = 0;
                          	
                          	//Constructor
                          	public Die() {
                          		number = NO_NUMBER;
                          	}
                          	
                          	//Roll a Die
                          	public int roll() {
                          		return number = (int)(Math.floor((Math.random() * (MAX_NUMBER - MIN_NUMBER +1) +1)));
                          	}
                          
                          	//Get the Die value
                          	public int getNumber() {
                          		return number;
                          	}
                          }
                          thats all I got so far.

                          I guess I can offer you credits
                          Your code is extremely messy so I fixed it.

                          A few things to note: don't make variables for things like 'space', that's just bad practice. There's no use in making a variable for a string that you use only once. It uses extra space in memory that can be avoided. Best thing to do is just add an extra space after the word 'are' instead of concatenating a space to it. Less code, simpler to read. Also, you can declare and create objects on the same line without a problem. It's usually faster to do it like this anyways, cause the object gets set to null, then set the the new value with your method, but creating the object right away skips that 'null' step, thus making it faster (nanoseconds, but those count). There's also no point in rolling then getting the value, you can do that all in one function. roll() will now assign the randomized number to the number variable, then return the same value to your println() function (which called roll()). This last change is optional though, but I like to make my life easier by doing multi-purpose functions. You can still roll the same way even though it now returns a value. It just makes the roll() function more useful.

                          Anyways, as to your actual question:

                          Code:
                          class DieRoll {
                          	public static void main ( String [] args) {		
                          		Die die = new Die();
                          		for(int i = 0; i < 3; i++)
                          			System.out.println("Result: " + die.roll());
                          	}
                          }
                          This will work and print something out like this:

                          Code:
                          Result: 3
                          Result: 4
                          Result: 1
                          If you need the numbers stored in some way, you could do that with an ArrayList, but that's a little more complicated to explain. You'll probably learn that in class in a few classes from now. But if you just need to output X number of die rolls to the console, a for loop is the best way.
                          A for loop is basically works like this: "Starting from 0 (declare your iterator with int i = 0; ), do the following operations (code under the for loop code), and then increment the iterator by one afterwards (i++), until the condition (i < 3; ) is no longer true." So the first result will happen when i = 0, the second result will happen when i = 1, and the third result will happen when i = 2. When i = 3, the for loop condition is no longer true, so it will not print a fourth result.

                          Let me know if you have any questions.


                          Btw I got A+ in both my java classes last year, I know my stuff. I'm definitely willing to help out some more with whatever questions you might have, I like explaining programming concepts
                          Last edited by MaxGhost; 09-20-2011, 11:43 PM.

                          Comment

                          • FFRGreen
                            Greeeeeeeeeeeeeeeeeeeeeen
                            • Oct 2010
                            • 1151

                            #14
                            Re: Java help

                            Originally posted by MaxGhost
                            class DieRoll {
                            public static void main ( String [] args) {
                            //old code
                            // Die die = new Die();
                            // System.out.println("Your Results are " + die.roll());

                            Die die = new Die();
                            for(int i = 0; i < 3; i++)
                            System.out.println("Result: " + die.roll());
                            }
                            }
                            From this I am wondering where the die.roll() went.

                            Comment

                            • MaxGhost
                              FFR Veteran
                              • Feb 2008
                              • 2141

                              #15
                              Re: Java help

                              It's two separate classes, I only included the DieRoll class, but that code assumes you're compiling it with the Die class as well. The first three lines were just comments, assume those are not there as well.

                              Does that answer your question? Your question wasn't very clear.

                              Comment

                              Working...