Java help

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

    #16
    Re: Java help

    This was my old code

    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());
    	
    	}
    	
    }

    But then you changed it to this but you didn't add the die.roll() part and I was wondering where it went.
    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());
    	}
    }
    Last edited by FFRGreen; 09-20-2011, 11:49 PM.

    Comment

    • MaxGhost
      FFR Veteran
      • Feb 2008
      • 2141

      #17
      Re: Java help

      Like I explained, you don't need it anymore the way I changed your roll() function.

      Originally, roll() just randomized a number between 1 and 6, and gave that value to the 'number' variable. Now, roll() does the same as above, but also returns the value when called. What that means is that when you try to print 'die.roll()', it will replace that with the number it randomized.

      Comment

      • FFRGreen
        Greeeeeeeeeeeeeeeeeeeeeen
        • Oct 2010
        • 1151

        #18
        Re: Java help

        Originally posted by MaxGhost
        Like I explained, you don't need it anymore the way I changed your roll() function.

        Originally, roll() just randomized a number between 1 and 6, and gave that value to the 'number' variable. Now, roll() does the same as above, but also returns the value when called. What that means is that when you try to print 'die.roll()', it will replace that with the number it randomized.
        Ok I got it thanks.


        Also thanks to everyone else that helped me.

        Comment

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

          #19
          Re: Java help

          Now integrate it again without reviewing your old code.
          MaxGhost was very helpful, but you should try to figure it out yourself as well.

          Comment

          • FFRGreen
            Greeeeeeeeeeeeeeeeeeeeeen
            • Oct 2010
            • 1151

            #20
            Re: Java help

            Originally posted by Zageron
            Now integrate it again without reviewing your old code.
            MaxGhost was very helpful, but you should try to figure it out yourself as well.
            Yeah I got what he did I learn better if someone gives me an example.

            Comment

            • Izzy
              Snek
              FFR Simfile Author
              • Jan 2003
              • 9195

              #21
              Re: Java help

              Are you not allowed to use the random number generator that is built into java libraries?

              Random r = new Random();

              int num = r.nextInt(6) + 1;
              Last edited by Izzy; 09-21-2011, 04:44 AM.

              Comment

              • MrGiggles
                Senior Member
                • Aug 2005
                • 2846

                #22
                Re: Java help

                you all have too many semi coloms, if you get rid of those ur prog will run much faster because CPU's have a tougher time with semi colons compared to alphanumeric characters
                Are you not allowed to use the random number generator that is built into java libraries?

                Random r = new Random();

                int num = r.nextInt(6) + 1;
                Yeah but I think the point of his assignment is to help familiarize him with how object-oriented programming operates, and it helps to make your own die class rather than just calling some dudebros random ass shit RNG
                Last edited by MrGiggles; 09-21-2011, 09:32 AM.

                Comment

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

                  #23
                  Re: Java help

                  never in my life have I seen someone use a variable to represent " " rofl.


                  Comment

                  • Izzy
                    Snek
                    FFR Simfile Author
                    • Jan 2003
                    • 9195

                    #24
                    Re: Java help

                    I just meant to use the random number generator inside of the Die class instead of using your own min and max values. I especially don't see the point in defining a variable to 0 and using that variable instead of 0 itself.

                    Code:
                    class Die {
                    	private int num;
                            Random r = new Random();
                    
                    	public Die() {
                    	       num = 0;
                    	}
                    	
                    	public void roll() {
                    		num = r.nextInt(6) + 1;
                    	}
                    	
                    	public int getRoll() {
                    		return num;
                    	}
                    }

                    Originally posted by rushyrulz
                    never in my life have I seen someone use a variable to represent " " rofl.
                    Yea.. that really didn't make any sense, just put a space where you want a space. lol

                    Comment

                    • YoshL
                      Celestial Harbor
                      FFR Simfile Author
                      FFR Music Producer
                      • Aug 2008
                      • 6156

                      #25
                      Re: Java help

                      Code:
                      class Die {
                      
                      	private int num;
                      
                      	public Die() {
                      	       num = 0;
                      	}
                      	
                      	public void roll() {
                      		num = (int) (Math.random() * 6) + 1;
                      	}
                      	
                      	public int getRoll() {
                      		return num;
                      	}
                      }
                      or you can use the Math.random() that's already there. no need to use another Random private instance i think


                      Originally posted by Charu
                      Only yours, for an easy price of $19.99! You too can experience the wonders of full motion rump sticking.

                      Comment

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

                        #26
                        Re: Java help

                        math commands <3


                        Comment

                        • MrGiggles
                          Senior Member
                          • Aug 2005
                          • 2846

                          #27
                          Re: Java help

                          Originally posted by Izzy
                          I just meant to use the random number generator inside of the Die class instead of using your own min and max values. I especially don't see the point in defining a variable to 0 and using that variable instead of 0 itself.
                          I guess it's possibly more secure that way? javadocs claims math.random is simpler for most applications but it's not like the random class is really complex or anything so idk.

                          I was told it's good practice to use constants whenever possible when I was learning Java, though I'm not sure exactly why (edit: probably so if you suddenly decide you want a 7 sided die that starts at 4 you can simply change the constants instead of ctrl-f-ing all instances of '6' and changing it to '7' or '0' to '3') so I don't worry about it too much, myself.
                          Last edited by MrGiggles; 09-21-2011, 12:14 PM.

                          Comment

                          • MaxGhost
                            FFR Veteran
                            • Feb 2008
                            • 2141

                            #28
                            Re: Java help

                            Math is automatically included whereas the Random class is not, so it makes your code bulkier to include classes not included by default.

                            Also constants are kind-of pointless in his case, cause he's not using it more than once. It saves memory and lines of code to not include the constants. YOSHl has it right, "num = (int) (Math.random() * 6) + 1;" is the better line of code. I still think returning as well is just generally useful to do.

                            Comment

                            • Izzy
                              Snek
                              FFR Simfile Author
                              • Jan 2003
                              • 9195

                              #29
                              Re: Java help

                              You are probably right, I am not used to coding in java and am not familiar with the best method of generating a random number. If you look how to get random numbers in C or C++ it is kind of annoying if you aren't using predefined random number generators.

                              I remember I was making a command line dungeon game where you had to explore a 2D array and I was putting in traps and obstacles at random and for some reason it kept making the same dungeon even though I was using the Rand() function.

                              Comment

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

                                #30
                                Re: Java help

                                Originally posted by rushyrulz
                                never in my life have I seen someone use a variable to represent " " rofl.
                                you're not supposed to use magic constants! what if the value of a space changes? you'll have to rewrite your entire program, but not this dude
                                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

                                Working...