Colorado State University

CS160: Foundations in Programming: Summer 2016


Recitation R2 - Conditional Logic


Preview

In this recitation we will:

Interpretation Section

  • Does the following code require braces and if so, why?
  • public static void main (String [] args) {
    
        int num = 3;
        if (num != 3)
            System.out.println("Num is not 3");
            num = 3;
            System.out.println("Num is now 3");
        else
            System.out.println("Num has always be 3");
    
    } 
    
  • What does the following code print?
  • public static void main (String [] args) {
    
        String s0 = "Hello";
        String s1 = "Hello";
            if (s0 == s1)
                System.out.println("s0 and s1 are the same!");
    	else
    	    System.out.prinlnt("s0 and s1 are NOT the same!");
    
    } 
  • What would the following code print?
  • public static void main (String [] args){
    
        char c = '@';
        switch (c) {
            case '@': System.out.println("char c = '@'");
            case '$': System.out.println("char c = '$'");
            case '4': System.out.println("char c = '4'");
            case 'u': System.out.println("char c = 'u'");
            case ' ': System.out.println("char c = ' '");
            default: System.out.println("char c is not '@', '$', '4', 'u', ' '");
        }
    
    } 
    
  • What would the following code print?
  • public static void main(String[] args) {
    
        int month = 2;
        int year = 2000;
        int numDays = 0;
    
        switch (month) {
            case 1: case 3: case 5:
            case 7: case 8: case 10:
            case 12:
                numDays = 31;
                break;
            case 4: case 6:
            case 9: case 11:
                numDays = 30;
                break;
            case 2:
                if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
                    numDays = 29;
                else
                    numDays = 28;
                    break;
            default:
                System.out.println("Invalid month.");
                break;
        }
        System.out.println("Number of Days = " + numDays);
    
    }
    

    Practice Problems


    Getting Started: Calculator Program


    Specifications: Finishing the Program

    This code is not complete. You need to add code whenever the comment starts with "ADD:". The following numbers will correspond with the comments in the code above.
    1. Create a variable of type character and use the Scanner object to store the operator.
    2. Create a variable of type double and use the Scanner object to store the second number.
    3. Write a switch statement that calculates the result and stores it in the double variable previously declared.
    4. If you're curious about exponential math, you can read more here.
    5. Print the result to 2 decimal places using printf.
    6. In the switch statement figure out a way to gracefully handle the user typing in a bad operator (like '@'). Print "Bad user input." and end the program.
    7. In the switch statement figure out a way to print "Cannot divide by zero." when the user tries to divide by zero.


    Sample Output

    Correct Output
    
    Welcome to a simple calculator program!
    ---------------------------------------
    All we know how to do is add, subtract, multiply, divide, compute modulo and calculate exponential expressions.
    
    Please enter a the first number of your calculation: 5
    Please enter the operator (+|-|*|/|%|^): +
    Please enter a the second number of your calculation: 8.7
    ---------------------------------------
    Result:
    5.00 + 8.70 = 13.70
    ---------------------------------------
    
    Dividing by Zero
    
    Welcome to a simple calculator program!
    ---------------------------------------
    All we know how to do is add, subtract, multiply, divide, compute modulo and calculate exponential expressions.
    
    Please enter a the first number of your calculation: 4
    Please enter the operator (+|-|*|/|%|^): /
    Please enter a the second number of your calculation: 0
    Cannot divide by zero.
    
    Incorrect Operator
    
    Welcome to a simple calculator program!
    ---------------------------------------
    All we know how to do is add, subtract, multiply, divide, compute modulo and calculate exponential expressions.
    
    Please enter a the first number of your calculation: 8
    Please enter the operator (+|-|*|/|%|^): &
    Please enter a the second number of your calculation: 6
    Bad user input.
    


    Once you have completed the interpretation exercise, Hackerrank activities, and submitted R2.java to Checkin you are finished.
    Last interpretation question originated from Oracle
    © 2016 CS160 Colorado State University. All Rights Reserved.