CS160: Foundations in Programming: Summer 2016
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"); }
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!"); }
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', ' '"); } }
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); }
Scanner keyboard = new Scanner(System.in); //Prints a welcome message System.out.println("Welcome to a simple calculator program!"); System.out.println("---------------------------------------"); System.out.println("All we know how to do is add, subtract, multiply, " + "divide, compute modulo and calculate exponential expressions."); System.out.println(); // Get input from the user System.out.print("Please enter a the first number of your calculation: "); double d1 = keyboard.nextDouble(); System.out.print("Please enter the operator (+|-|*|/|%|^): "); // 1. ADD code here System.out.print("Please enter a the second number of your calculation: "); // 2. ADD code here // Here we will calculate the result: double result; // 3. ADD code here // Print the result System.out.println("---------------------------------------"); // 4. ADD code here System.out.println("---------------------------------------"); keyboard.close();
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.