 |
|

|
 |

09/17/09 - Selection Statements add intelligence to programs
|
 |
 |
 |
  | Adding intelligence (decision-making) capability to your programs
|
 |
 |
 |
 |
  | if (age >= 16) { System.out.println("You're old enough to drive."); }
|
 |
 |
 |
 |
  | if (age >= 16) { System.out.println("You're old enough to drive."); } else { System.out.println("Don't worry. Your time will come."); }
|
 |
 |
 |
 |
  | if (score >= 90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else if (score >= 70) { grade = 'C'; } else if (score >= 60) { grade = 'D' } else { grade = 'F'; }
|
 |
 |
 |
 |
  | Nested if-else statements
|
 |
 |
 |
 |
  | if (iValue >= 10) { if (iValue <= 20) { System.out.println(iValue + " is between 10 and 20"); } else { System.out.println(iValue + " is > 20"); } } else { System.out.println(iValue + " is < 10"); }
|
 |
 |
 |
 |
  | Combining tests in if statements with && (logical-and)
|
 |
 |
 |
 |
  | if (iValue >= 10 && iValue <= 20) { System.out.println(iValue + " is between 10 and 20"); } else { System.out.println(iValue + " is < 10 or " + iValue + " is > 20"); }
|
 |
 |
 |
 |
  | Combining tests in if statements with || (logical-or)
|
 |
 |
 |
 |
  | if (iValue < 10 || iValue > 20) { System.out.println("Either " + iValue + " is less than 10 or greater than 20"); }
|
 |
 |
 |
 |
  | switch statements for testing specific integer or char values
|
 |
 |
 |
 |
  | switch (choice) { case 'A': doOptionA(); break; case 'B': doOptionB(); break; case 'C': doOptionC(); break; default: System.out.println("Unknown option"); }
|
 |
 |
 |
 |
  | Write code in main that takes 10 integers from the user and displays the largest, the smallest and the average of the 10 integers.
|
 |
 |
 |
 |
  | /** * Get 10 values from user, find max, min and average. * * @author Chuck Iverson * @version 9/17/09 */
import java.util.Scanner;
public class ValueTest { public static void main(String [] args) { Scanner keyboard = new Scanner(System.in); int value; int max = -9999999; int min = 9999999; double average; int sum = 0; for (int i = 1; i <= 10; ++i) { System.out.print("Enter integer #" + i + ": "); value = keyboard.nextInt(); if (value > max) { max = value; } if (value < min) { min = value; } sum += value; } System.out.println("The max value is " + max); System.out.println("The min value is " + min); System.out.println("The average value is " + sum/10.0); } }
/* sample run
Enter integer #1: 3 Enter integer #2: 5 Enter integer #3: -2 Enter integer #4: 17 Enter integer #5: 23 Enter integer #6: 98 Enter integer #7: -234 Enter integer #8: 2345 Enter integer #9: 345 Enter integer #10: 987 The max value is 2345 The min value is -234 The average value is 358.7
*/
|
 |
 |
 |
 |
  | The standard grading scale is:
 Create a public char letterGrade(int score1, int score2, int score3) method that takes three test scores as parameters and returns the letter grade based on the table above. Create a main method that interactively gets the three test scores from the user and calls your letterGrade method and displays the result.
|
 |
 |
 |
 |
  | Write a method void displayDivisors(int number) that takes a positive integer as a parameter and displays each integer divisor of that parameter.
|
 |
 |
 |
 |
  | Write a method boolean isPrime(int number) that takes a positive integer as a parameter and returns true if the number is prime (has no integer divisors other than itself and 1) and false otherwise.
|
 |
 |
 |
 |
  | A new tax law has just been passed by the government: the first $6000 of income is free of tax, the next $15,000 is taxed at 10%, the next $30,000 is taxed at 20%, and the rest is taxed at 30%. Write an interactive program that prompts for a user's income and reports the corresponding tax.
|
 |
 |
 |
 |
  | The Fast Freight Shipping Company charges the following rates:
 The shipping charges per 500 miles are not prorated. For example, if a 2-kg package is shipped 550 miles, the charges would be $2.20.
Create a public double shippingCharge(double weight, double distance) method that takes the weight of a package and the distance it is to be shipped as parameters and returns the correct shipping charge for the package. Create a main method that interactively gets the package weight and distance to be shipped from the user and calls your shippingCharge method and displays the result.
|
 |
 |
|


 |
 |
 |