Java Assignment 3 -- Dice Applet

Click Reload or Refresh to roll again

Program Requirements

  1. Your applet will use Math.random() to generate the number shown on each die.
  2. Your applet will use two switch statements, one to draw each die.
  3. Your applet will use an init() method to set the background color in addition to a paint method.
  4. Your applet will construct a correct English sentence with the total for the roll. (e.g. "an eight" or "a seven")

Random Numbers in Java:
Math.random()

There is more than one way you can generate a random number in Java. For this assignment I suggest that you use Math.random(), a method from the java math library. Math.random() returns a double between 0 and 1. Below is an applet that generates some random numbers. You can use it as a guide to begin your assignment.


import java.awt.*;
import java.applet.*;

public class Demo extends Applet 
{
	public void init()
        {
		setBackground(Color.blue);
	}
	public void paint(Graphics g) 
        {
	
		//generate a random double between 0 and 1
		double dNum = Math.random();
		g.drawString("A random decimal: " + dNum, 25, 35);
		
		//generate a random integer between 0 and 100
		int nNum1 = (int)(Math.random() * 101);
		g.drawString("A random integer: " + nNum1, 25, 60);
		
		//generate a random integer between 10 and 20
		int nNum2 = (int)(Math.random() * 11) + 10;
		g.drawString("Another random integer: " + nNum2, 25, 85);
	}
}

You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.

Submit the URL of your applet along with your Java code (the .java file) in an email message to mrsimon@lycos.com.