Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop JAVA PROGRAMMING LAB Eng. Mustafa J. Dalloul Computer Programming Lab 18 October 2016
Suppose that you need to display a string (e.g., Welcome to Java) a hundred times. It would be tedious to have to write the following statement a hundred times: System. out. println("welcome to java"); System. out. println("welcome to java"); 100 times.. { System. out. println("welcome to java"); So, how do you solve this problem? while A while loop executes statements repeatedly while the condition is true. The syntax for the while loop is: And the flow chart is: The flow chart in figure (a) show how the while loop work. while we have a true continuation condition, the statements in while body will be executed. So we can solve the introduced problem by using while loop as illustrated in figure (b). 1
Example: Find the sum for listed input numbers Code: Scanner s = new Scanner(System.in); double sum = 0; System.out.print("Enter a list of numbers (ends if 0):"); double n = s.nextdouble(); while (n!= 0) { sum += n; n = s.nextdouble(); System.out.println("The sum of numbers is: " + sum); Example: (The 3n + 1 Problem) consider the following algorithm to generate a sequence of numbers. Start with an integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this process with the new value of n, terminating when n = 1. For example, the following sequence of numbers will be generated for n = 22: 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Write a program to read a positive integer n and generate a sequence of numbers until 1. Code: Scanner input = new Scanner(System.in); int n = input.nextint(); while (n > 1) { System.out.print(n + " "); if (n % 2 == 0) n /= 2; else n = n * 3 + 1; System.out.println(n); 2
Example: Write a program to read a file contains unknown number of lines, each one contains exactly two integers A and B. For each line, print the sum of both numbers, in the following format: Line #N: S Where N is the line number, and S is the sum of two numbers in that line. Sample input: Code: import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class Main { public static void main(string[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("input.txt")); int a, b, line = 1; while (input.hasnext()) { a = input.nextint(); b = input.nextint(); System.out.println("Line #" + line + ": " + (a + b)); // OR // System.out.printf("Line #%d: %d\n", line, a + b); line++; T1: Repeat Addition Quiz Write a program that prompts the user to enter an answer for a question on addition of two single digits. The program will exit if and only if the user enter a true answer. 3
Code: int number1 = (int)(math.random() * 10); int number2 = (int)(math.random() * 10); int result = number1+number2; Scanner s = new Scanner(System.in); System.out.print("What is "+number1+"+"+number2+"?"); int answer = s.nextint(); while (answer!= result) { System.out.print("Wrong answer! Try again. "); System.out.print("What is "+number1+"+"+number2+"?"); answer = s.nextint(); System.out.println("You got it!"); FOR MORE SEE LISTING 5.4 FROM THE BOOK AT PAGE (164) THIS EXAMPLE IS VERY VERY IMPUTRANT, PLEASE READ AND UNDERSTAND IT 4
do-while A do-while loop is the same as a while loop except that it executes the loop body first and then checks the loop continuation condition. The do-while loop is a variation of the while loop. Its syntax is: And the flow chart is: Example: Find the sum for listed input numbers Code: Scanner s = new Scanner(System.in); double sum = 0; System.out.print("Enter a list of numbers (ends if 0):"); double n = s.nextdouble(); do { sum += n; n = s.nextdouble(); while (n!= 0); System.out.println("The sum of numbers is: " + sum); When I Use do-while? Use a do-while loop if you have statements inside the loop that must be executed at least once, as in the case of the do-while loop in the preceding Test Do While program. These statements must appear before the loop as well as inside it if you use a while loop. 5
for A for loop has a concise syntax for writing loops. It can be used to simplify the preceding loop as: Instead of The syntax of a for loop is: And the flow chart is: A for loop generally uses a variable to control how many times the loop body is executed and when the loop terminates. This variable is referred to as a control variable. The initial action often initializes a control variable, the action-after-eachiteration usually increments or decrements the control variable, and the loopcontinuation-condition control variable tests whether the control variable has reached a termination value. For example, the following for loop prints Welcome to Java! a hundred times:s 6
int i; for (i = 0; i < 100; i++) { System.out.println("Welcome to Java!"); Notes: 1- The control variable must be declared inside the control structure of the loop or before the loop. a. If you will use the control variable only inside a for loop structure, you need to declare it in the for loop structure. b. If you will use the control variable outside the for loop structure, you need to declare it outside the for loop. 2- The initial-action in a for loop can be a list of zero or more comma-separated variable declaration statements or assignment expressions. 3- The action-after-each-iteration in a for loop can be a list of zero or more comma-separated statements. 4- If the loop-continuation-condition in a for loop is omitted, it is implicitly true. For example (a),(b) and (c): Example: (Count Uppercase Letters) write a program that counts how many uppercase letters exist in a given string. Code: Scanner input = new Scanner(System.in); String line = input.nextline(); int count = 0; for (int i = 0; i < line.length(); i++) { if (Character.isUpperCase(line.charAt(i))) { count++; System.out.println(count); 7
1- An integer N greater than 1 is prime if its only positive divisor is 1 or itself. In other words, (N % X!= 0, for all integers X where 1 < X < N). Write a program that reads an integer and check if it is a prime number or not. 2- Write a program that prompts the user to enter a string and displays the characters at odd positions. 3- Write a program that asks the user to type 10 integers and writes the smallest value. 8
Netbeans Tips & Tricks for + TAB Use this shortcut for writing for loop header quickly. Write: for, then press TAB button and see the result. 9