Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 24th, 2006, 09:41 PM
Reetu Reetu is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 3 Reetu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 14 sec
Reputation Power: 0
New To Java - Need some help

Hi,
I am very new to Java, and trying to solve a problem:

A player rolls two dice.If the total is seven, the player wins otherwise the player loses.


Need help in this.

Thankyou

Reply With Quote
  #2  
Old September 25th, 2006, 07:54 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
So, where's the problem?

Why not post some code and we can try to help you out...
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter!
DevArticles Forum Moderator

"The net is a waste of time, and that's exactly what's right about it." -- William Gibson

Reply With Quote
  #3  
Old September 25th, 2006, 04:44 PM
colton22's Avatar
colton22 colton22 is offline
\ ^_^ / - *Local Friend*
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: near chicago, Illinois
Posts: 469 colton22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 1 h 36 m 37 sec
Reputation Power: 13
Send a message via AIM to colton22 Send a message via MSN to colton22 Send a message via Yahoo to colton22
Code:
package ;

import java.awt.*;

public class {

public boolean roll() {
double dice1,dice2,ans=7;
dice1=Math.round(Math.random()*6); // a dice has six sides
dice2=Math.round(Math.random()*6); // a dice has six sides

dice1=dice1+dice2; //use one dice as a temp variable

if (dice1=ans) {return true;}
else {return false;}
}



public void init() {

if (roll()) {JOptionPane.showMessageDialog(null, "You rolled a seven!!");}

else {JOptionPane.showMessageDialog(null,"You did not roll a seven!!");}
}
}


just insert the package decloration and the class name above...

hope this helped

colton22

Last edited by colton22 : September 25th, 2006 at 04:46 PM.

Reply With Quote
  #4  
Old September 25th, 2006, 07:27 PM
Reetu Reetu is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 3 Reetu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 14 sec
Reputation Power: 0
Re:

Thanks for the code..
I tried this and seems to work fine:

Code:

public class PairOfDice {
	public static void main(String[] args) {
		   
		   PairOfDice dice;          // A variable that will refer to the dice.
		   int rollCount;    // Number of times the dice have been rolled.

		   dice = new PairOfDice();  // Create the PairOfDice object.
		   rollCount = 0;
		   
		   /* Roll the dice until they come up snake eyes. */
		   
/*		   do {
		       dice.roll();
		       System.out.println("Dice1: " + dice.getDie1() 
		                                        + " Dice2: " + dice.getDie2());
		       rollCount++;
		   } while (dice.getTotal() != 7);
*/		   
		   
		   System.out.println("Dice 1: " + dice.getDie1() 
                + " and Dice 2: " + dice.getDie2());	   	
		   
		   /* Report the number of rolls. */
		   
		   //System.out.println("\nIt took " + rollCount + " rolls to get a 2.");
		   if (dice.getTotal() == 7)
		   	System.out.println("\nPlayer won!!!");
		   else
		   	System.out.println("\nPlayer lost!!!");		   		   			   	
		   
	}
       private int die1;   // Number showing on the first die.
       private int die2;   // Number showing on the second die.
       
       public PairOfDice() {
               // Constructor.  Rolls the dice, so that they initially
               // show some random values.
           roll();  // Call the roll() method to roll the dice.
       }
       
       public void roll() {
               // Roll the dice by setting each of the dice to be
               // a random number between 1 and 6.
           die1 = (int)(Math.random()*6) + 1;
           die2 = (int)(Math.random()*6) + 1;
       }
                
       public int getDie1() {
             // Return the number showing on the first die.
          return die1;
       }
       
       public int getDie2() {
             // Return the number showing on the second die.
          return die2;
       }
       
       public int getTotal() {
             // Return the total showing on the two dice.
          return die1 + die2;
       }
       
    }  // end class PairOfDice


/* 
Rolls a pair of dice until the dice come up snake eyes
(with a total value of 2).  Counts and reports the
number of rolls.
*/



Do you know how I can use the above code to follow this question:

roll a pair of dice 1000 times, counting the number of box cars (two sixes) that occurs.

The below doesnt seem to work..Can you pls help me..in modifying whats required.

Code:
public class PairOf1000Dice
{
	public static void main (String[]args){
	private final int MAX = 6; // maximum face value
	private int FV1; // current value showing on die 1
	private int FV2; //current value showing on die 2
	
public PairOf1000Dice(){
	{
		FV1 = 0;
		FV2 = 0;
	}


public int roll_1()
	{

		FV1 = (int)(Math.random() * MAX) + 1;

		return FV1;
	}

public int roll_2()
	{
		FV2 = (int)(Math.random() * MAX) + 1;
		return FV2;
	}


public void setFaceValue1 (int value1)
	{
		FV1 = value1;
	}
public void setFaceValue2(int value2)
	{
		FV2 = value2;
	}
public int getFaceValue1()
	{
		return FV1;
	}
public int getFaceValue2()
	{
		return FV2;
	}
public String toString()
	{
		String result = Integer.toString(FV1+FV2);

		return result;
	}
		}
	}
}



Thanks

Last edited by MadCowDzz : September 26th, 2006 at 09:03 AM. Reason: Added [code] tags

Reply With Quote
  #5  
Old September 25th, 2006, 07:31 PM
Reetu Reetu is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 3 Reetu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 14 sec
Reputation Power: 0
Re: Error with the following code

Hi:

When I roll a pair of dice 1000 times, counting the number of box cars (two sixes) that occurs.

Heres a code for it but I get an error saying
syntax error on token public , "new" expected.
Can you please help in correcting what might have been wrong here. Thank you

Heres the code:
Code:
/*
 * Created on Sep 24, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * 
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PairOf1000Dice
{
	public static void main (String[]args){
	private final int MAX = 6; // maximum face value
	private int FV1; // current value showing on die 1
	private int FV2; //current value showing on die 2
	
public PairOf1000Dice(){
	{
		FV1 = 0;
		FV2 = 0;
	}


public int roll_1()
	{

		FV1 = (int)(Math.random() * MAX) + 1;

		return FV1;
	}

public int roll_2()
	{
		FV2 = (int)(Math.random() * MAX) + 1;
		return FV2;
	}


public void setFaceValue1 (int value1)
	{
		FV1 = value1;
	}
public void setFaceValue2(int value2)
	{
		FV2 = value2;
	}
public int getFaceValue1()
	{
		return FV1;
	}
public int getFaceValue2()
	{
		return FV2;
	}
public String toString()
	{
		String result = Integer.toString(FV1+FV2);

		return result;
	}
		}
	}
}

Last edited by MadCowDzz : September 26th, 2006 at 09:04 AM. Reason: Added [code] tags

Reply With Quote
  #6  
Old September 26th, 2006, 09:05 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
Please wrap your code in [code][/code] when pasting code into the forums...
It makes the code easier to read.

Reply With Quote
  #7  
Old September 28th, 2006, 09:51 AM
Robbert Robbert is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 3 Robbert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 9 sec
Reputation Power: 0
Doing this in a graphical surrounding is a tad hard if you're very new, so I'll limit it to console output.

Get a Main class and a Dice class. The Dice class has the (possibly static) int returning method named throw(), which uses the Math.random() method to simulate a dice throw.

When you launch Main, it'll create a new instance of Dice (Dice d = new Dice()). Then it'll execute Dice's throw command 1000 times (Dice.throw();), with a for-loop and counts the number of times a 6 has been thrown with the help of an internal counter.

In the end, it outputs it to the console (System.out.print();). If you need more help, go and post.


As for your current code: It's probabely expecting something akin to: Pair p = new PairOf1000Dice.

Reply With Quote
  #8  
Old November 28th, 2006, 07:40 AM
in_lax in_lax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 2 in_lax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 29 sec
Reputation Power: 0
Thumbs down

i think hes a skiddie free-loading.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > New To Java - Need some help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap