CSC 120

Week 3


Reading Assignment:

Readng: Finish Chapter 2, start reading Chapter 3.
Here are the programs we looked at in class: BooleanOps.java TruthTables.java Dice.java Relations.java Chars.java IntegerTest.java Increment.java ArithmeticTest.java RootFinder.java Valentine_1.java Valentine_2.java

Lab/Homework Assignment:

  1. 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
    

  2. Using simple probability theory, determine the probability of getting a pair (1/2 ? 1/36 ? 1/6 ?); that is, on the average, how many throws you have to make to get a pair. Run your program 10 times and record (by hand, using a pencil) the number of throws it took to get a pair. Now, compute the average of these numbers and see how close it gets to the theoretical probability.
  3. /* 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.

  4. Write a program that prints out all the characters of lower case letters of the English alphabet and their corresponding integer values. Use a while statement to do this. Your output should look like:

    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.