Study he program Dice.java. Then modify this as described below. Write a program that simulates rolling a pair of dice. You can simulate rolling one die by choosing one of the integers 1, 2, 3, 4, 5, or 6 at random. The number you pick represents the number on the die after it is rolled. The expression
(int)(Math.random()*6) + 1
does the computation you need to select a random integer between 1 and 6. You can assign this value to a variable to represent one of the dice that are being rolled. Do this twice. Your program should roll a pair of dice until you get a pair. It should report the pair of numbers in each throw and the total number of throws.
Your output should look like:
dice: 5 6 dice: 3 4 dice: 3 2 dice: 3 1 dice: 5 4 dice: 2 2 You got a pair in 6 tries
/* Extend this program to print out AND and OR truth tables. Remember that there are 4 cases in each table. NOTE: You should use comments to put your name and the week in all your programs. Author: Huseyin Kocak Date: Week 3; February 8, 2008. Assignment: Truth Tables */ public class TruthTables { public static void main(String[] args){ boolean flag; boolean flag1 = true, flag2 = false; flag = ( flag1 && flag2 ); System.out.println(" "); System.out.println("***** AND truth table *****"); System.out.println(flag1 + " AND " + flag2 + " is: " + flag); } //end of main } //end of class
After you finish your program, do a final run with the command:
java TruthTables > tables.txt
to dump the output of your program to the file tables.txt.
You can read this file with a text editor. Now you have
a copy of truth tables to printout, if you like.
Ask your TA how to print it.
a has integer value 97 b has integer value 98 ... ... z has integer value 122
For extra credit do the same for upper case letters, the digits and their corresponding integer values.