Homework 2

Part A

Answer the following in a Word or .txt document. Attach this to the same submission as for part B
  1. Describe a specific situation where a case statement would make more sense than an if-else-if structure. Aslo describe a specific situation where an if-else-if would make more sense than a case statement. "Specific situation" means: make up a program scenario; you may not use any scenario already used in a classwork, homework, or lecture example.
  2. Create a single long boolean expression that matches each of the following situations. You don't need to write surrounding code, but do make up variable names as needed.

Part B

We will use Java to do a few programming tasks:
The following example code shows using conditionals in java. (Remember you have examples of other Java elements in the previous homework)
// This code sets things up for a general java program with input
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

        // print and then read in a double from the user
        double a = promptDouble("what is a?");

        // print and then read in from the user
        double b = promptDouble("what is b?");
        
        // if-else if-else structure  
        // note that there is no semicolon after { or }
        if (a > b) {
            System.out.println(a + " is biggest");
        } else if (b > a) {
            System.out.println(b + " is biggest");
        } else {
            System.out.println(a + " and " + b + " are the same");
        } // end of whole if-else structure
        
        

        // the type String is capitalized in this language
        String userWord = promptString("Are you a ghost?");
        
        // Strings in java use a special method called .equals() to check equality
        // this checks if userWord is equal to "Yes"
        if ( userWord.equals("Yes") ) {
            System.out.println("Well boo to you too");
        } else {
            System.out.println("Phew, I was scared for a minute there!");
        }

    } // end main code
    
     /*    Simple Utility methods that prompt the user and return input
          If you enter the wrong type of input, the program will end
          with an error!
    */
    
    // utility for reading in integer input
    public static int promptInt(String promptText) {
        System.out.print(promptText + " ");
        Scanner scan = new Scanner(System.in);
        int response = scan.nextInt();
        return response;
    }
    
    // utility for reading in double input
    public static double promptDouble(String promptText) {
        System.out.print(promptText + " ");
        Scanner scan = new Scanner(System.in);
        double response = scan.nextDouble();
        return response;
    }
    
    // utility for reading in String input
    public static String promptString(String promptText) {
        System.out.print(promptText + " ");
        Scanner scan = new Scanner(System.in);
        String response = scan.nextLine();
        return response;
    }

}

Using this code as a model, write code (each should be a separate program) to
  1. Welcome the user to the zoo. Ask the user their age. Kids under 5 get in free, under 18 cost $5, adults cost $10, and over 65 cost $5. Also ask them if they want a ticket to the Vulture Ballet performance, which costs (for anyone) $8. Then tell them their total cost.
  2. Ask the user if they want to do a square, or a rectangle. Depending on which one they want, ask them for the necessary information and print the correct area for the shape. [EC+20] Give them four options: area of a square, area of a rectangle, volume of a cube, or volume of a cylinder.
  3. Each separate program should be a separate .java file. Attach all your java files and your part A to a single submission in blackboard, not multiple submissions.