Need help

java assignment

Page 1 of 1

10 Replies - 948 Views - Last Post: 03 November 2008 - 07:39 PM Rate Topic: -----

#1 countrygirl1970  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Need help

Posted 02 November 2008 - 05:03 PM

Hello everyone,

I am trying to get this code working and it is giving me some error codes. The program is about rolling 3 dice together.

1) is to figure the frequency of the sum of all the dice rolled 60000 times
2) is to figure the precentage of the sum of all the dice rolled 60000 times
3) at the end show the total for frenquency and percentage

Here is the code:

// Displaying multiple strings
import javax.swing.*;				// To use JOptionPane.showInputDialog box
import java.util.Random;			 // To use Random number generator
import java.awt.Graphics;			// import class Graphics


public class Assignment5 extends JApplet
{
	public class Dice
	{
		public static void main (String[] args)
		{
			Random randomNumbers = new Random();	 // Random number generator
			int frequency[] = new int [ 16 ];		// Array of frequency counters
			int totalFrequency = 0;				  // Total of frequencies in the array
			
			int percentage[] = new int [ 16 ];	   // Array of percentage counters
			int totalPercentage = 0;				 // Total of percentages in the array
			
			// Summarize results of 60,000 rolls of the dice
			for ( int totalRoll = 1; totalRoll <= 60000; totalRoll++ )
			   ++frequency[ 1 + randomNumbers.nextInt (6) ];
							
			// Rolling of three dice
			int firstDie = (int) (6 * Math.random ()) + 1;	// Roll one die
			int secondDie = (int) (6 * Math.random ()) + 1;   // Roll second die
			int thirdDie = (int) (6 * Math.random ()) + 1;	// Roll third die
			int totalRoll = firstDie + secondDie + thirdDie;  // Total of the three dice
			
			// Multiply the frequency to 60000 to get the percentage
			percentage = frequency * 60000;
			
			// initialize applet by obtaining values from user
line 50			public void init()
			{			
			
			// Add each frequency's value to get total
			for ( int counter = 0; counter < array.length; counter++ )
				totalFrequency += array[ counter ];			
				
			// Add each percentage's value to get total
			for ( int counterPer = 0; counterPer < array.length; counterPer++ )
				totalPercentage += array [ counterPer ];
				
			} // End init
			
			// draw results in a rectangle on applet's background
			public void paint( Graphics g )
			{
				super.paint( g );				 // call class version of method paint
				
				// draw results as a String
				g.drawString( "Sum"  ,  "Frequency"   ,  "%" );
				
				// draw line as a String
 line 72		   	g.drawString(---------------------------------);
				
				// draw results as a String
 75		   	g.drawString( "3" + frequency +, + percentage );
 76			  g.drawString( "4" + frequency +, + percentage );
 77		   	g.drawString( "5" + frequency +, + percentage );
 78		   	g.drawString( "6" + frequency +, + percentage );
 79		   	g.drawString( "7" + frequency +, + percentage );
 80		   	g.drawString( "8" + frequency +, + percentage );
 81		   	g.drawString( "9" + frequency +, + percentage );
 82		   	g.drawString( "10" + frequency +, + percentage );
 83		   	g.drawString( "11" + frequency +, + percentage );
 84		   	g.drawString( "12" + frequency +, + percentage );
 85		   	g.drawString( "13" + frequency +, + percentage );
 86		   	g.drawString( "14" + frequency +, + percentage );
 87		   	g.drawString( "15" + frequency +, + percentage );
 88		   	g.drawString( "16" + frequency +, + percentage );
 89		   	g.drawString( "17" + frequency +, + percentage );
 90		   	g.drawString( "18" + frequency +, + percentage );
		   
				// draw line as a String
 93		   	g.drawString(----------------------------------);
				
				// draw results as a String
 96		   	g.drawString( "Total"	 + totalFrequency +,	+ totalPercentage );
	  
	 
			 }  // end method paint
			
			
		} // end main
		
	} // end class dice
	
106	 } // end class Assignment5





here are the error codes that I am getting:
illegal start of expression on lines:
50, 72, 75-90, 93, 96
class, interface, or enum expected:
106

Here is what the program will look like at the end:

sum frequency percentage
-------------------------------------------
3 254 0.42%
" " "
18 284 0.47%
-------------------------------------------
total 60000 100.00%

I just need some help to point me in the right direction.

Thank you,
countrygirl1970

Is This A Good Question/Topic? 0
  • +

Replies To: Need help

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Need help

Posted 02 November 2008 - 05:37 PM

the drawString method takes 3 arguments a string and two ints
drawString( String s, int a, int b );


i see that you have given it 2 args only...
Was This Post Helpful? 0
  • +
  • -

#3 countrygirl1970  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Need help

Posted 02 November 2008 - 07:04 PM

I was able to fix lines:
72, 93

I added this to lines 75-90:
g.drawString( "5" + frequency +, + percentage, 45, 95);

I am still getting the error code:
illegal start of expression on lines:

Thanks,
countrygirl1970
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Need help

Posted 02 November 2008 - 07:07 PM

View Postcountrygirl1970, on 2 Nov, 2008 - 07:04 PM, said:

I was able to fix lines:
72, 93

I added this to lines 75-90:
g.drawString( "5" + frequency +, + percentage, 45, 95);

I am still getting the error code:
illegal start of expression on lines:

Thanks,
countrygirl1970


An extra coma in your statment, Java sees 4 parameters
try this
g.drawString( "5" + frequency + percentage, 45, 95);
Was This Post Helpful? 0
  • +
  • -

#5 countrygirl1970  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Need help

Posted 02 November 2008 - 07:26 PM

Thank you, I was able to fix lines 75-90
Was This Post Helpful? 0
  • +
  • -

#6 countrygirl1970  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Need help

Posted 02 November 2008 - 08:45 PM

I was able to fix everything except one line. Here is what I have so far:
// Displaying multiple strings
import javax.swing.*;				// To use JOptionPane.showInputDialog box
import java.util.Random;			 // To use Random number generator
import java.awt.Graphics;			// import class Graphics


public class Assignment5 extends JApplet
{
	Random randomNumbers = new Random();	 // Random number generator
	int frequency[] = new int [ 16 ];		// Array of frequency counters
	int totalFrequency = 0;				  // Total of frequencies in the array
	int percentage[] = new int [ 16 ];	   // Array of percentage counters
	int totalPercentage = 0;				 // Total of percentages in the array
	
	// Rolling of three dice
	int firstDie = (int) (6 * Math.random ()) + 1;	// Roll one die
	int secondDie = (int) (6 * Math.random ()) + 1;   // Roll second die
	int thirdDie = (int) (6 * Math.random ()) + 1;	// Roll third die
	int totalRoll = firstDie + secondDie + thirdDie;  // Total of the three dice
			
	// Multiply the frequency to 60000 to get the percentage
line 38	percentage = frequency * 60000;			
	
	public void init()
	{
		// Summarize results of 60,000 rolls of the dice
		for ( int totalRoll = 1; totalRoll <= 60000; totalRoll++ )
			++frequency[ 1 + randomNumbers.nextInt (6) ];
					
		// Add each frequency's value to get total
		for ( int counter = 0; counter < array.length; counter++ )
			totalFrequency += array[ counter ];
				
		// Add each percentage's value to get total
		for ( int counterPer = 0; counterPer < array.length; counterPer++ )
			totalPercentage += array [ counterPer ];
		
		} // end method init
				
		// draw results in a rectangle on applet's background
		public void paint( Graphics g )
		{
			super.paint( g );				 // call class version of method paint
					
		   	// draw results as a String
		   	g.drawString( "Sum"  +  "Frequency"  +   "%" , 45, 35);
					
			  // draw line as a String
			  g.drawLine(10, 55, 100, 55);
					
			// draw results as a String
			g.drawString( "3" + frequency + percentage, 45, 75);
			g.drawString( "4" + frequency + percentage, 45, 85);
			g.drawString( "5" + frequency + percentage, 45, 95);
			g.drawString( "6" + frequency + percentage, 45, 105);
			g.drawString( "7" + frequency + percentage, 45, 115);
			g.drawString( "8" + frequency + percentage, 45, 125);
			g.drawString( "9" + frequency + percentage, 45, 135);
			g.drawString( "10" + frequency + percentage, 45, 145);
			g.drawString( "11" + frequency + percentage, 45, 155);
			g.drawString( "12" + frequency + percentage, 45, 165);
			g.drawString( "13" + frequency + percentage, 45, 175);
			g.drawString( "14" + frequency + percentage, 45, 185);
			g.drawString( "15" + frequency + percentage, 45, 195);
			g.drawString( "16" + frequency + percentage, 45, 205);
			g.drawString( "17" + frequency + percentage, 45, 215);
			g.drawString( "18" + frequency + percentage, 45, 225);
					
			// draw line as a String
			g.drawLine(10, 245, 100, 245);
					
			// draw results as a String
			g.drawString( "Total"	 + totalFrequency +	totalPercentage, 45, 265 );
					
			}  // end method paint
												  
} // end Assignment5




I am getting this code for line 38:
<identifier> expected
Exactly what does that mean?

Thanks again,
countrygirl1970
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Need help

Posted 02 November 2008 - 09:20 PM

	   // Add each frequency's value to get total
		for ( int counter = 0; counter < array.length; counter++ )
			totalFrequency += array[ counter ];
				
		// Add each percentage's value to get total
		for ( int counterPer = 0; counterPer < array.length; counterPer++ )
			totalPercentage += array [ counterPer ];
 


The variable array is not defined... you sure you do not mean frequency or percentage
Was This Post Helpful? 0
  • +
  • -

#8 countrygirl1970  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Need help

Posted 03 November 2008 - 03:07 PM

I am not understanding what you mean by that. Can you explain a little more.

Thank you,
countrygirl1970
Was This Post Helpful? 0
  • +
  • -

#9 stauffski  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 210
  • Joined: 03-November 07

Re: Need help

Posted 03 November 2008 - 04:40 PM

// Displaying multiple strings
import javax.swing.*;				// To use JOptionPane.showInputDialog box
import java.util.Random;			 // To use Random number generator
import java.awt.Graphics;			// import class Graphics


public class Assignment5 extends JApplet
{
	public class Dice
	{
		public static void main (String[] args)
		{
			Random randomNumbers = new Random();	 // Random number generator
			int frequency[] = new int [ 16 ];		// Array of frequency counters
			int totalFrequency = 0;				  // Total of frequencies in the array
			
			int percentage[] = new int [ 16 ];	   // Array of percentage counters
			int totalPercentage = 0;				 // Total of percentages in the array
			
			// Summarize results of 60,000 rolls of the dice
			for ( int totalRoll = 1; totalRoll <= 60000; totalRoll++ )
			   ++frequency[ 1 + randomNumbers.nextInt (6) ];
							
			// Rolling of three dice
			int firstDie = (int) (6 * Math.random ()) + 1;	// Roll one die
			int secondDie = (int) (6 * Math.random ()) + 1;   // Roll second die
			int thirdDie = (int) (6 * Math.random ()) + 1;	// Roll third die
			int totalRoll = firstDie + secondDie + thirdDie;  // Total of the three dice
			
			// Multiply the frequency to 60000 to get the percentage
			percentage = frequency * 60000;
		
					}	
//add "}"			// initialize applet by obtaining values from user
line 50			public void init()
			{			
			
			// Add each frequency's value to get total
			for ( int counter = 0; counter < array.length; counter++ )
				totalFrequency += array[ counter ];			
				
			// Add each percentage's value to get total
			for ( int counterPer = 0; counterPer < array.length; counterPer++ )
				totalPercentage += array [ counterPer ];
				
			} // End init
			
			// draw results in a rectangle on applet's background
			public void paint( Graphics g )
			{
				super.paint( g );				 // call class version of method paint
				
				// draw results as a String
				g.drawString( "Sum"  ,  "Frequency"   ,  "%" );
				
//add quotes				// draw line as a String
 line 72		   	g.drawString("---------------------------------");
				
//remove extra "+"				// draw results as a String
 75		   	g.drawString( "3" + frequency, percentage );
 76			  g.drawString( "4" + frequency, percentage );
 77		   	g.drawString( "5" + frequency, percentage );
 78		   	g.drawString( "6" + frequency, percentage );
 79		   	g.drawString( "7" + frequency, percentage );
 80		   	g.drawString( "8" + frequency, percentage );
 81		   	g.drawString( "9" + frequency, percentage );
 82		   	g.drawString( "10" + frequency, percentage );
 83		   	g.drawString( "11" + frequency, percentage );
 84		   	g.drawString( "12" + frequency, percentage );
 85		   	g.drawString( "13" + frequency, percentage );
 86		   	g.drawString( "14" + frequency, percentage );
 87		   	g.drawString( "15" + frequency, percentage );
 88		   	g.drawString( "16" + frequency, percentage );
 89		   	g.drawString( "17" + frequency, percentage );
 90		   	g.drawString( "18" + frequency, percentage );
		   
//add quotes			// draw line as a String
 93		   	g.drawString("----------------------------------)";
				
//remove extra "+"			// draw results as a String
 96		   	g.drawString( "Total"	 + totalFrequency,  totalPercentage );
	  
	 
			 }  // end method paint
			
			
		} // end main
		
	} // end class dice
	
106	 } // end class Assignment5





That should fix it.

Good Luck!
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Need help

Posted 03 November 2008 - 04:46 PM

View Postcountrygirl1970, on 3 Nov, 2008 - 02:07 PM, said:

I am not understanding what you mean by that. Can you explain a little more.

Thank you,
countrygirl1970

to be able to use a variable, you need to declare it first and initialize it depending on where you want to use it and also depending on the scope...lets say you have a for loop that prints i
for( int i = 0; i < 6; i++ )
{
  System.out.println( i );
}	
System.out.println( i + 2 ); // this statement will give an error


you see, you have initialized variable i inside the loop itself and so the scope of the variable i is within the body of the loop only and once you go outside the loop, the variable i will not be recognized because it was not defined in that scope and you will have to reinitialize it ...this tip should help you with ur code...you have method void init() using an array reference called array which was not defined anywhere in the class or even given in the method parameter, so the java compiler will issue an error that it does not recognize this array...hope this helped
Was This Post Helpful? 0
  • +
  • -

#11 countrygirl1970  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 08

Re: Need help

Posted 03 November 2008 - 07:39 PM

Thank you all for helping me out. I will keep working on it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1