Pre-Lab Assignment: Lab 6      Name ___________________________________

  1. In the code below, replace the cascading if statement with a switch statement.
    System.out.println("Enter size box you want");
    int boxSize = scan.nextInt();
    double price;
    
    if (boxSize == 10)
    {
       System.out.println("Small");
       price = 2.5;
    }
    else if (boxSize == 20) 
    {
       System.out.println("Medium");     
       price = 3.75;
    }
    else if (boxSize == 30)
    {
       System.out.println("Large");
       price = 5.0;
    }
    else
    {
       System.out.println ("Not a valid size.");     
       price = -1;
    }
    
    

  2. Rewrite each condition below in valid Java syntax (give a boolean expression):
    1. x > y > z

       

       

    2. neither x nor y is equal to 0

       

       

    3. both x and y are equal to 0

       

       

    4. x is greater than y but less than z

       

       

     

  3. Use the laws of logic to write a Java expression equivalent to the following without using the not (!) operator.
              ! (age >= 65 && income < 20000)
    
    
    
    
    
    
  4. Use truth tables to determine whether each pair of expressions is equivalent. Show work!
    1. NOT q OR p
      p OR NOT(q OR p)
      
      
      
      
      
      
      
      
      
      
      
      
      

    2. (p AND r) OR (q AND NOT r)
      (p AND q AND NOT r)
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      

  5. Recall that in Lab 3 we generated a random date for a birthday but, since we didn't know about if statements, our date would occasionally be wrong (for example, the program could generate a date of 2/30 or 9/31, neither of which is a valid date). In this week's lab we will generate valid dates and test to make sure the program is correct.

    1. In testing it is important to make sure your program works on special cases. One thing our program in lab will do is, after generating a random date (that includes month, day, and year), it will determine the next day (so if we generate 9/30/2010 it should say the next day is 10/1/2010; if we generate 10/11/2010 it should say 10/12/2010 is the next day). One special case we would want to be sure to test to make sure the program is correct is Feb. 28 in a leap year (the program should say the next day is Feb. 29 of that same year). List at least 3 other special cases that you would want to test to be sure the program gets the next day right.
      
      
      
      
      
      
      
    2. Leap year is definitely a special case when dealing with dates. We generally think of leap years as being those years divisible by 4 but the condition for leap year is more complex - a year is a leap year if it is divisible by 400 OR if it is divisible by 4 BUT NOT by 100. So, 2008 was a leap year (divisible by 4 but not 100) and 2000 was a leap year (divisible by 400) but 2100 will not be a leap year (why not?). To thoroughly test a program to see if it correctly identifies leap years one would need to test all possibilities in a truth table. Here the condition for a leap year is made up of three simple statements: Let p be the statement "the year is divisible by 400"; let q be the statement "the year is divisible by 100"; let r be the statement "the year is divisible by 4".

      Since there are three simple statements there are 8 possible combinations of truth values in the truth table. However these statements are related so not all 8 combinations are possible. For example, if p is true (that is the year is divisible by 400) then q and r must also be true (a number divisible by 400 is also divisible by 100 and is divisible by 4). Similarly if q is true r must also be true since a number divisible by 100 is also divisible by 4. Complete the truth table below indicating whether or not a particular row in the table is possible. If it is give a date that satisfies the condition (for example if p, q, and r are all three true then one example would be 1600 because it is divisble by 400, by 100, and by 4).

      
          Year divisible 
                by
         400   100    4    Possible?    Example Year
          p     q     r
       
          T     T     T      Yes           1600
      
          T     T     F      No  (Not possible - if p is true r must be true)
      
          T     F     T    ______        ____________
      
          T     F     F    ______        ____________
      
          F     T     T    ______        ____________
      
          F     T     F    ______        ____________
      
          F     F     T    ______        ____________
      
          F     F     F    ______        ____________