Rudiments of Control Statement in Object Oriented Programming | CS 201, Lab Reports of Computer Science

Material Type: Lab; Class: Intro Obj Orient Programming; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 04/12/2010

koofers-user-oq3
koofers-user-oq3 🇺🇸

10 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Control Statements
In this laboratory you will:
1. Review the while statement
2. Compare the while and do-while statements
3. Examine the relational and logical operators and how they are used in control statements
4. Investigate if and if-else statements
5. Learn the rudiments of the for statement
6. Learn the rudiments of the switch statement
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Rudiments of Control Statement in Object Oriented Programming | CS 201 and more Lab Reports Computer Science in PDF only on Docsity!

Control Statements

In this laboratory you will:

  1. Review the while statement
  2. Compare the while and do-while statements
  3. Examine the relational and logical operators and how they are used in control statements
  4. Investigate if and if-else statements
  5. Learn the rudiments of the for statement
  6. Learn the rudiments of the switch statement

The while and do-while Statements

Recall that the while statement, introduced in Session 2, has the form while( condition ) { body } and directs that the cycle of testing the condition and executing the body be continued as long as the test confirms that the condition is true. This iterative statement is called a looping structure and the while statement is called a while loop. A slight variation of the while loop is the do-while loop. It has the form do { body }while( condition ); and directs that the process of executing the body and then testing the condition be continued until the condition becomes false. Note that the while loop tests the condition before executing the body, and the do-while loop tests the condition after executing the body. Therefore, the body of the do-while loop is always executed at least once.

Experiment 5.

Step 1. Predict the output of this program. class J05E { public static void main(String[] args) { int numChips = 1; System.out.println("Open the bag and ..."); do { System.out.println(" can't stop ..."); numChips = numChips + 1; } while(numChips < 6); System.out.println(" CAN'T STOP!"); } }






Step 3. Predict the output of this program, which is similar to the program in Step 1, but uses a do-while loop instead of a while loop. class J05E02B { public static void main(String[] args) { int x = 5; do { System.out.println(x); x++; } while(x < 5); System.out.println("That's all folks!"); } }







Step 4. Compile and execute program J05E02B.java. Record the results.






Step 5. Explain the differences in the output between the two programs in Steps 1 and 3.








Experiment 5.

Step 1. Rewrite the following program segment using a do-while loop rather than a while loop. Your segment should produce the same output as the original while loop. glassPaper = 5; while(glassPaper > 0) { System.out.println("Reduce!\nReuse!\nRecycle!\n"); glassPaper--; }













Step 2. Expand the given segment and the segment you wrote in Step 1 into two programs, and test them to confirm that your version is correct. Summarize your work below.











We have now mentioned all eight of the primitive data types in Java: byte, short, int, long, float, double, char and boolean. All other data types in Java are defined by a class.

Experiment 5.

Step 1. Compile and execute J05E04.java. Test the program by responding with 6. Record the results. import java.util.Scanner; class J05E { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num; System.out.print("Enter an integer between 1 and 10: "); num = scan.nextInt(); while(num < 1 || num > 10) { System.out.print("Error: Enter again: "); num =scan.nextInt(); } System.out.println(num + " is my number too!"); } }







Step 2. Execute the program again. This time respond with numbers less than 1 or greater than 10 such as 15, 0, 23... How many times can the body of the loop be executed? Record your observations and the results.






Step 3. Modify the program by changing the condition. Replace (num < 1 || num > 10) with (num < 1 && num > 10). Execute the modified program, testing different input values. For what values of num is the body of the loop executed? Record and explain your results.









Step 4. Rewrite the program in Step 1 using a do-while loop instead of a while loop. Summarize your code and comment on the results.




















numerator denominator quotient 5 2 ______ 10 0 ______ -33 6 ______ 0 -25 ______

Step 2. Modify the program in Step 1 by inserting an if statement that protects against division by 0. Summarize your work below.













Step 3. Modify the code in Step 2 using an if-else statement that either prints the message "Error, division by 0 is not allowed" or performs the calculation and prints the original message.











Experiment 5.

Step 1. Compile and execute the program J05E06.java multiple times, each time modifying the response as indicated in the table below. Record the results. import java.util.Scanner; class J05E { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x, y, z = 1; System.out.print("Enter an integer between 0 and 10: "); x = scan.nextInt(); if(x >= 0 && x <= 10) { y = 1; while(y <= x) { z = z * y; y++; } System.out.println("Calculated value is " + z); } } } x value Calculated result

  • 0 3 5 10 12

Step 2. Explain in your own words, what this program does.







j = 2; while (j < 4) { System.out.println("Every day is"); System.out.println("Earth Day!\n"); j++; } Both loops cause the message Every day is Earth Day! Every day is Earth Day! to be displayed. More precisely, the variable j is first initialized to 2, the condition j < 4 is tested and found to be true, and the body of the loop is executed. Then, j is incremented to 3 and the condition j < 4 is tested again. Since the condition is still true, the body is executed a second time, and once more the value of j is incremented, this time to 4. Now when the condition j < 4 is tested, the result is false, which terminates the loop.

Experiment 5.

Step 1. Predict the results of executing the program J05E07.java. class J05E { public static void main(String[] args) { int j; System.out.println("Open the fridge ..."); for(j = 0; j < 3; j++) { System.out.println("Chomp!"); } System.out.println("... close the fridge"); System.out.println("When done the loop counter is " + j); } }








Step 2. Execute the program and compare your prediction with the actual results.










Step 3. A common programming error is to place a semicolon (;) at the end of the line containing the opening statement of the for loop. Modify the program from Step 1 to include this error. for(j = 0; j < 3; j++); Compile and execute the program. Record the results.







Step 4. Explain the results from the program in Step 3.









The switch Statement

The switch statement is an alternate selection statement that selects among many options. It has the form: switch ( variable ) { case option_1 : statement_sequence_1; break; case option_2 : statement_sequence_2 ; break; . . . default: default_statement_sequence ; } where variable is a variable identifier and option_1, option_2, etc., are possible values of that variable. The value of variable determines which of the statement sequences is executed. Each statement_sequence can consist of zero or more statements. The break statement transfers control to the statement following the switch statement. If the value of variable does not appear as one of the listed options, then the default statement sequence is executed. (The terms switch, case, default and break are reserved words.) As an example, if sym is defined to be of type char, then switch (sym) { case 'a': System.out.println("The symbol is an a."); break; case 'b': System.out.println("The symbol is a b."); break; case 'c': System.out.println("The symbol is a c."); break; default: System.out.println("The symbol is not a, b, or c"); } reports whether the value of sym is a, b, c, or another symbol.

Experiment 5.

Step 1. Execute the following program J05E09.java and record the results. class J05E { public static void main(String[] args) { char sym; sym = 'a'; switch (sym) { case 'a': System.out.println("The symbol is an a."); case 'b': System.out.println("The symbol is a b."); case 'c': System.out.println("The symbol is a c."); default: System.out.println("The symbol is not a, b, or c"); } System.out.println("Switch is completed"); } }

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

Step 2. What can you conclude about the absence of break statements in a switch statement? Correct the program by inserting the appropriate break statements, and confirm that the program performs correctly.






Step 3. Delete the default line from the program in Step 1, and change the statement sym = 'a'; to the statement sym = 'd'; Execute the modified program and record the results. What can you conclude about the absence of the default option?