LAB 5  (Comp 170)

PART IBranching statements. Consider the following simple if-else construct:

int n;
if (n%2 == 0)
    System.out.println(n + " is an even number.");
else
    System.out.println(n + " is an odd number.");

After studying the above example, write and test a simple Java program which reads a string (representing a first name), and prints either "This name is long." if the string has more than 8 characters, or else prints "This name is short."  (So for example, if the input is Albertine, then the output is This name is long If the input is Clive, then the output is This name is short.)

Multi-branching if-else statements.  Often we wish to branch in more than two directions.  For example, we may wish to decide if an integer is positive, negative, or zero  (three directions).  This can be achieved as follows:
int n;
if (n > 0)
    System.out.println(n + " is a positive number.");
else if (n < 0)
    System.out.println(n + " is a negative number.");
else
    System.out.println(n + " is zero.")

Or, you may wish to convert a numerical test score to a letter grade (requiring five branchs):

int score;
char grade = '?';  // initialize grade in case it is not defined later
System.out.println("Enter your test score: ");
score = Keyboard.readInt();
if (score>=88)
    grade = 'A';
else if (score>=78)
    grade = 'B';
else if (score>=67)
    grade = 'C';
else if (score>=50)
    grade = 'D';
else
    grade = 'F';
System.out.println("Your grade is:  " + grade);

Write a java program (using multi-branching) which reads three integers (a, b, c) representing the sides of a triangle, and prints if the triangle is equilateral, isosceles, or scalene. (Recall that equilateral means all sides equal, isosceles means only one pair of sides are equal, and scalene means no two sides are equal.)

Write and test a java program which reads an integer (representing an amount in dollars) and prints one of the following:

Less than $100.
Less than $1,000 but greater than or equal to $100.
Less than $10,000 but greater than or equal to $1,000.
Less than $100,000 but greater than or equal to $10,000.
Less than $1,000,000 but greater than or equal to $100,000.
Greater than or equal to $1,000,000.

PART II Switch statements.  The switch statement is often an attractive alternative to the simple if-else or the multi-branching if-else.  It is primarily useful only when an integer variable or expression (or a char variable) is known to assume a small number of discrete values.  For example the odd/even choice may be made via:

int n;
remainder = n%2;
switch(remainder)    {
    case 0:
        System.out.println("Even.");
        break;
    case 1:
        System.out.println("Odd.");
        break;
}

Another example (given in our text) is that of the number of days in each month of the year.  Ignoring leap year, the following program segment prints the number of days in each month:
 

int numberOfDays;
int month = Keyboard.readInt();  // read in the month as an integer between 1 and 12
switch (month) {
    case 2:  numberOfDays = 28;    //  February
                    break;
    case 4:
    case 6:
    case 9:
    case 11:  numberOfDays = 30;   // April, June, September, November
                    break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:   numberOfDays = 31;  // January, March, May, July, August, October, December
                    break;
}
 

Write and test a simple Java program which reads a string (representing a sentence) and prints one of the following messages:

This statement is declarative.
This statement is interrogatory.
This statement is exclamatory.
This statement has no punctuation mark at the end.

Use a switch statement that chooses an option based upon the last character of the string:

Hint:  Here is a skeleton of the code.  Note that default allows us to handle "all other cases."

switch (sentence.charAt(sentence.length() - 1) ) {
      case '.':   System.out.println( ...);
      case '?':
      case '!':
      default:

}
 

Part IIIDEBUGGING DECISION MAKING:  We will work through the debugging exercises of section 4.4 of our text.

1.  Enter the code given on page 101 for the class Hose.  Run the program and observe the logical error.

2.  Correct the error (as is done on page 102).  Run the new program and observe the logical error as specified on page 103.

3.  Add the debugging statement:
    System.out.print ("x = " + x + ", y = " + y + "\n");
Next observe the result (negative value of x), and make the corrections necessary to obtain the final version of class Hose, as given on pages 104-105.
 

 Course Home Page          Department Home Page        Loyola Home Page