Go Back   Flash Flash Revolution > General Discussion > Chit Chat
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 09-20-2011, 11:20 PM   #1
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default 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 at 11:29 PM..
FFRGreen is offline   Reply With Quote
Old 09-20-2011, 11:24 PM   #2
Sprite-
HAVE ME WITH VODKA
Retired StaffFFR Simfile AuthorFFR Veteran
 
Sprite-'s Avatar
 
Join Date: Aug 2005
Location: Long Island, New York
Age: 35
Posts: 7,316
Default 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?
Sprite- is offline   Reply With Quote
Old 09-20-2011, 11:25 PM   #3
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default Re: Java help

Quote:
Originally Posted by Sprite- View Post
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
FFRGreen is offline   Reply With Quote
Old 09-20-2011, 11:28 PM   #4
Zageron
Zageron E. Tazaterra
RRR Developer & DevOps Support
AdministratorDeveloperFFR Veteran
 
Zageron's Avatar
 
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,587
Default Re: Java help

So post it.
__________________
Zageron is offline   Reply With Quote
Old 09-20-2011, 11:34 PM   #5
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default Re: Java help

Quote:
Originally Posted by Zageron View Post
So post it.
already ahead of you
FFRGreen is offline   Reply With Quote
Old 09-20-2011, 11:34 PM   #6
Zageron
Zageron E. Tazaterra
RRR Developer & DevOps Support
AdministratorDeveloperFFR Veteran
 
Zageron's Avatar
 
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,587
Default 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 at 11:43 PM..
Zageron is offline   Reply With Quote
Old 09-20-2011, 11:50 PM   #7
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default Re: Java help

Quote:
Originally Posted by Zageron View Post
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.
FFRGreen is offline   Reply With Quote
Old 09-20-2011, 11:55 PM   #8
rushyrulz
Digital Dancing!
Retired StaffFFR Simfile AuthorFFR Music ProducerD7 Elite KeysmasherFFR Veteran
 
rushyrulz's Avatar
 
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,981
Default Re: Java help

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


rushyrulz is offline   Reply With Quote
Old 09-21-2011, 12:06 AM   #9
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default Re: Java help

Quote:
Originally Posted by rushyrulz View Post
he means string += die.getRoll() + " " lol
I feel stupid because I don't get what you are saying lol
FFRGreen is offline   Reply With Quote
Old 09-21-2011, 12:15 AM   #10
Zageron
Zageron E. Tazaterra
RRR Developer & DevOps Support
AdministratorDeveloperFFR Veteran
 
Zageron's Avatar
 
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,587
Default 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.
__________________
Zageron is offline   Reply With Quote
Old 09-21-2011, 12:16 AM   #11
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default Re: Java help

Quote:
Originally Posted by Zageron View Post
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.
FFRGreen is offline   Reply With Quote
Old 09-21-2011, 12:25 AM   #12
Patashu
FFR Simfile Author
Retired StaffFFR Simfile Author
 
Patashu's Avatar
 
Join Date: Apr 2006
Location: we traced the call...it's coming from inside the house
Age: 33
Posts: 8,609
Send a message via AIM to Patashu Send a message via MSN to Patashu Send a message via Yahoo to Patashu
Default 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
Patashu is offline   Reply With Quote
Old 09-21-2011, 12:26 AM   #13
MaxGhost
FFR Veteran
FFR Veteran
 
MaxGhost's Avatar
 
Join Date: Feb 2008
Location: Ottawa, Canada
Age: 31
Posts: 2,141
Send a message via Skype™ to MaxGhost
Default Re: Java help

Quote:
Originally Posted by FFRGreen View Post
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-21-2011 at 12:43 AM..
MaxGhost is offline   Reply With Quote
Old 09-21-2011, 12:41 AM   #14
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default Re: Java help

Quote:
Originally Posted by MaxGhost View Post
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.
FFRGreen is offline   Reply With Quote
Old 09-21-2011, 12:43 AM   #15
MaxGhost
FFR Veteran
FFR Veteran
 
MaxGhost's Avatar
 
Join Date: Feb 2008
Location: Ottawa, Canada
Age: 31
Posts: 2,141
Send a message via Skype™ to MaxGhost
Default 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.
MaxGhost is offline   Reply With Quote
Old 09-21-2011, 12:46 AM   #16
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default 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-21-2011 at 12:49 AM..
FFRGreen is offline   Reply With Quote
Old 09-21-2011, 12:49 AM   #17
MaxGhost
FFR Veteran
FFR Veteran
 
MaxGhost's Avatar
 
Join Date: Feb 2008
Location: Ottawa, Canada
Age: 31
Posts: 2,141
Send a message via Skype™ to MaxGhost
Default 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.
MaxGhost is offline   Reply With Quote
Old 09-21-2011, 12:51 AM   #18
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default Re: Java help

Quote:
Originally Posted by MaxGhost View Post
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.
FFRGreen is offline   Reply With Quote
Old 09-21-2011, 01:07 AM   #19
Zageron
Zageron E. Tazaterra
RRR Developer & DevOps Support
AdministratorDeveloperFFR Veteran
 
Zageron's Avatar
 
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,587
Default 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.
__________________
Zageron is offline   Reply With Quote
Old 09-21-2011, 01:18 AM   #20
FFRGreen
Greeeeeeeeeeeeeeeeeeeeeen
FFR Veteran
 
FFRGreen's Avatar
 
Join Date: Oct 2010
Location: California
Age: 30
Posts: 1,151
Send a message via MSN to FFRGreen Send a message via Skype™ to FFRGreen
Default Re: Java help

Quote:
Originally Posted by Zageron View Post
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.
FFRGreen is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 03:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution