Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Notes – Week 4 Chapter 5 (Loops).

Similar presentations


Presentation on theme: "Lecture Notes – Week 4 Chapter 5 (Loops)."— Presentation transcript:

1 Lecture Notes – Week 4 Chapter 5 (Loops)

2 Outline Loops Case studies To do list

3 Case Study 1 – Predicting Future House Prices
In this case study, we learn how to design a solution for a given problem, and how to write, compile, run and test a program to implement the designed solution. 3

4 Problem Specification
The problem is to design, implement, run and test a program that predict future house prices and display them. Suppose that the average house price for a city is £200,000 this year and it increases 5% every year. In how many years will the average house price be doubled?. 4 4 4 4 4

5 How Would We Do it? 1. Set price to 200,000; 2. Set year to 0;
3. While price < 400,000 3.1. Increase price by 5%; 3.2. Increment year by 1; 4. Display prompt message and year. 5 5 5 5 5

6 Implementation public class FutureHousePrice {
public static void main(String[] args) { double price = 20000; // price in year 0 int year = 0; while (price < ) { price = price * 1.05; //price increases for another year year++; //another year } System.out.println(”Price will be doubled in ” + year + " years"); System.out.printf(”Price will be £%.2f in %1d years", price, year); 6 6 6 6 6 6 6 6 6 6

7 Case Study 2 – Displaying Prime Numbers
The problem: Display the first fifty prime numbers in five lines, each containing ten numbers. 7 7

8 Algorithm Design - Pseudocode
Set number of primes to 50; Set count to 0; Set number to 2; While count < 50 { 4.1. Test if number is prime and assign truth value to Boolean variable isPrime; 4.2. If isPrime { Display number; Increment count by 1; } 4.3. Increment number by 1; An algorithm describes how a problem is solved by listing the actions that need to be taken and the order of their execution. Algorithms can be described in natural languages or in pseudocode (natural language mixed with some programming code). 8 8 8 8 8 8

9 Algorithm Refinement 4.1. (Test If number is prime and assign truth value to isPrime) is replaced by Set isPrime to true; for divisor = 2 to number/2 { if (number % divisor == 0) { Set isPrime to false; Exit the loop; } 9 9 9 9 9 9 9 9

10 Algorithm Refinement (4.2.1. Display number) is replaced by
if (count % 10 == 0) { Display number and new line; else Display number; 10 10 10 10 10 10 10 10

11 Final Algorithm Set number of primes to 50; Set count to 0;
Set number to 2; While count < 50 { 4.1. Test if number is prime and assign truth value to Boolean variable isPrime; 4.2. If isPrime { Display number; Increment count by 1; } 4.3. Increment number by 1; 11 11 11 11 11 11 11 11 11

12 Final Algorithm 1. Set number of primes to 50; 2. Set count to 0;
3. Set number to 2; 4. While count < 50 { Set isPrime to true; for divisor = 2 to number/2 { if (number % divisor == 0) { Set isPrime to false; Exit the loop; } 4.2. If isPrime { Display number; Increment count by 1; 4.3. Increment number by 1; 12 12 12 12 12 12 12

13 Final Algorithm 1. Set number of primes to 50; 2. Set count to 0;
3. Set number to 2; 4. While count < 50 { Set isPrime to true; for divisor = 2 to number/2 { if (number % divisor == 0) { Set isPrime to false; Exit the loop; } 4.2. If isPrime { Display number; Increment count by 1; 4.3. Increment number by 1; 13 13 13 13 13 13 13 13

14 Final Algorithm 1. Set number of primes to 50; 2. Set count to 0;
3. Set number to 2; 4. While count < 50 { Set isPrime to true; for divisor = 2 to number/2 { if (number % divisor == 0) { Set isPrime to false; Exit the loop; } 4.2. If isPrime { if (count % 10 == 0) { Display number and new line; else Display number; Increment count by 1; 4.3. Increment number by 1; 14 14 14 14 14 14 14 14

15 Final Algorithm 1. Set number of primes to 50; 2. Set count to 0;
3. Set number to 2; 4. While count < 50 { Set isPrime to true; for divisor = 2 to number/2 { if (number % divisor == 0) { Set isPrime to false; Exit the loop; } 4.2. If isPrime { if (count % 10 == 0) { Display number and new line; else Display number; Increment count by 1; 4.3. Increment number by 1; 15 15 15 15 15 15 15 15 15

16 Implementation public class PrimeNumber {
public static void main(String[] args) { final int NUMBER_OF_PRIMES = 50; final int NUMBER_OF_PRIMES_PER_LINE = 10; int count = 0; // number of primes so far int number = 2; // A number to be tested System.out.println("The first 50 prime numbers are \n"); // Repeatedly find prime numbers while (count < NUMBER_OF_PRIMES) { // Assume the number is prime boolean isPrime = true; // Is the current number prime? // Test whether number is prime for (int divisor = 2; divisor <= number / 2; divisor++) { if (number % divisor == 0) { // If true, not prime isPrime = false; // Set isPrime to false break; // Exit the for loop – no further test } 16 16 16 16 16 16 16 16 16 16 16

17 Implementation // Display the prime number and increase the count
if (isPrime) { count++; // Increase the count if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { // Display the number and advance to the new line System.out.println(number); } else System.out.print(number + " "); // Check if the next number is prime number++; 17 17 17 17 17 17 17 17 17 17 17 17

18 Which Loop to Use? Three forms of loops are expressively equivalent. You can write a loop in any of them. In general, a for loop may be used if the number of iterations is known; A while loop may be used if the number of iterations is not known; A do-while loop can be used to replace a while loop if the loop body has to be executed at least once. 18 18 18

19 To Do List Before Next Lecture
Read Week 4 lecture slides. Read and run Week 4 program examples. Selectively read those sections in Chapter 5 that cover the topics in this lecture. Attend your practical session in Week 4, consisting of a tutorial and a set of programming exercises. Glance through those sections in Chapter 6 that cover the topics in Week 4’s lecture.


Download ppt "Lecture Notes – Week 4 Chapter 5 (Loops)."

Similar presentations


Ads by Google