Advanced Java - Lec #2 ~ Practical Work Examples.pptx
1. 1
In the name of Allah
Ghalib University
Computer Science Faculty
General Department
Advanced Java
Lecture Title
Lecture #2 Practical Work
Examples
Subject: Advanced Java
Instructor: Mohammad Dawood Saddiqi
Date: Saturday, July 5, 2025
2. List of Contents
• At First Some Questions
• While Loop
• The do-while Loop
• The for Loop
• Methods
• Arrays
• Summary
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 2
3. At First Some Questions
• What is Loop?
• What is Loop body?
• What is iteration of a Loop?
• Can you convert a for loop to a while loop?
• What is Method?
• What is Array?
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 3
4. While Loop
Example #1
• We want to develop a program that prompts the user to enter an
answer for a question on addition of two single digits. Using a loop,
you can rewrite the program to let the user repeatedly enter a new
answer until it is correct.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 4
6. Cont.
Example #2
• The problem is to guess what number a computer has in mind. You
will write a program that randomly generates an integer between 0 and
100, inclusive. The program prompts the user to enter a number
continuously until the number matches the randomly generated
number. For each user input, the program tells the user whether the
input is too low or too high, so the user can make the next guess
intelligently. Here is a sample run:
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 6
8. The do-while Loop
Example #3
• Suppose the input is 2 3 4 5 0. What is the output of the following
code?
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 8
9. The for Loop
Example #4
• Suppose the input is 2 3 4 5 0. What is the output of the following
code?
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 9
10. Cont.
Example #5
• Write a program that uses nested for loops to display a multiplication
table.
• Like this:
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 10
14. Cont.
Example #7
• Identify and correct the errors in the following program:
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 14
15. Arrays
Example #8
• Initializing arrays with input values: The following loop
initializes the array myList with user input values.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 15
16. Cont.
Example #9
• Initializing arrays with random values: The following loop
initializes the array myList with random values between 0.0 and
100.0, but less than 100.0.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 16
17. Cont.
Example #10
• Displaying arrays: To print an array, you have to print each
element in the array using a loop like the following:
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 17
18. Summary
• While Loop
• The do-while Loop
• The for Loop
• Methods
• Arrays
• Summary
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 18