Introduction to Programming Java Lab 7: Loops 22 February 2013 1 JavaLab7 lecture slides.ppt Ping...

Post on 28-Mar-2015

213 views 0 download

Tags:

Transcript of Introduction to Programming Java Lab 7: Loops 22 February 2013 1 JavaLab7 lecture slides.ppt Ping...

Introduction to Programming

Java Lab 7:Loops

22 February 2013 1JavaLab7 lecture slides.ppt

Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Java Project

Project Name: JavaLab7

2

RectangleRectangle

NumberProperties

Class Rectangle (part 1)

• Reads a non-negative integer, numberRows, from key board.

• Prints out numberRows rows of the form

• Objective

– Understand the use of a single for loop to solve problems.

• Method (i)

3

Input

int numberRows=0;

Example of input numberRows= 2;

Loop

One for loop

Print out two rows of three asterisks.

Output

******

***

Syntax of the for Statement

for (int i = 1; i <= numberRows; i++){

/* more Java statement(s) */}/* i = 1: initialisation, executed once on entering the

loop.i <= numberRows: condition to be checked before each iteration.i++: update is executed after each iteration. */

4

for (int i=1;

i <= numberRows; i+

+)

{

System.out.println("***"

);

}

for (int i=1;

i <= numberRows; i+

+)

{

System.out.println("***"

);

}

11

22

33

44

5

True

False

println ("***");

i <=numberRo

ws?

i <=numberRo

ws?

i=1;11

22

33

44

End

i++;

Flowchart of a for Loop Start

Anatomy of Class Rectangle (part 1)

import java.util.Scanner;

public class Rectangle{

public static void main(String[] args){

/* To Do - write code to read numberRows from the key board which is a non-negative number. */

for (int i = 1; i <= numberRows; i++){ System.out.println('***');

}} // end of main

} // end of class Rectangle

6

Class Rectangle (part 2)

• Reads two non-negative integers numberRows and numberColumns from key board.

• Prints out numberRows rows of asterisks, such that each row contains numberColumns asterisks.

• Objective - understand the use of nested for loops.

• Method (ii)

7

Input

int numberRows =

0, numberColumns = 0;

Example of input

numberRows = 2;numberColumns = 4;

Loop

Nested for

loops

Print out a matrix (2 rows by 4 columns) of asterisks.

Output

********

Anatomy of Class Rectangle (part 2)

import java.util.Scanner;

public class Rectangle{

public static void main(String[] args){

/* Write code to read two non-negative integers, numberRows and numberColumns, from key

board */

for (int i = 1; i <= ? ; i++){ for (int j = 1; j <= ?; j++) { // print out an asterisk } System.out.println();

}} // end of main

} // end of class Rectangle8

Which one of the two

variables to use?

Which one of the two

variables to use?

Which one of the two

variables to use?

Which one of the two

variables to use?

Class NumberProperties

• Reads a set of strictly positive integers from keyboard, one at a time.

• Use a while loop to read these integers.

• The end of input is indicated by the integer 0.

• Program prints out:i. the average value (use a variable of type double)ii. the smallest of the valuesiii. the largest of the valuesiv. the range, that is one more than the difference between the smallest

and the largest values.

• Add a comment with each number which is printed out, for example:The average value is: ...

9

Class NumberProperties (2)

• Objectives– Set a proper sentinel for the while loop to solve problems. – Apply the Math class methods, Math.min and Math.max

• Method (in pseudo-code)

10

int count = 0, currentInt= -1, maxValue = 0, minValue = 0;double total = 0.0;while ( currentInt != 0 ) // setting a sentinel to end the loop

{ currentInt = in.nextInt(); // in is defined as Scanner type if ( currentInt != 0 )

{ /* Write additional code to: (i) add one to count; (ii) calculate the total; (iii) update minValue and maxValue. */ }

} /* write additional code to find range, average and print results */

Class NumberProperties (3)

11

Input

A set of positive integer numbers.

For example:

2 5 3 9

Computations

1.double average =

(2 + 5 + 3 + 9) / 4

2. min = min(2, 5, 3, 9)

3. max = max(2, 5, 3, 9)

4. range = (max - min) +

1

Output

1.4.75

2.2

3.9

4.8

Syntax for the while Loop

while (boolean condition){

/* Write additional Java statement(s) */}

/* The statement(s) are carried out while the

condition is true. */

12

while (currentInt != 0)

{

currentInt =

in.nextInt(); /* Write

statements to solve the problem */

}

while (currentInt != 0)

{

currentInt =

in.nextInt(); /* Write

statements to solve the problem */

}

11

22

33

13

True

False

currentInt = in.nextInt();

currentInt!= 0?

currentInt!= 0?

11

22

33

End

Write statements to solve problem

Flowchart of a while Loop Start

Anatomy of Class NumberProperties

public class NumberProperties{

public static void main(String[] args){ int count = 0, currentInt = -1, minValue = 0, maxValue

=0; double total = 0.0;

/* write code to declare Scanner object for keyboard input */ while (currentInt != 0) {

currentInt = in.nextInt(); if (currentInt != 0)

{ /* Code to: add one to count; calculate the total;

update minValue and maxValue. */ } } /* Code to find range, average and print results */} // end of main

} // end of class NumberProperties14