Download presentation

Presentation is loading. Please wait.

Published byPaige Barry Modified over 3 years ago

1
Introduction to Programming Java Lab 7: Loops 22 February 2013 1 JavaLab7 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

2
Java Project Project Name: JavaLab7 2 Rectangle NumberProperties

3
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 ***

4
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

5
for (int i=1; i <= numberRows; i++) { System.out.println("***"); } for (int i=1; i <= numberRows; i++) { System.out.println("***"); } 1 1 2 2 3 3 4 4 5 True False println ("***"); i <= numberRows ? i <= numberRows ? i=1; 1 1 2 2 3 3 4 4 End i++; Flowchart of a for Loop Start

6
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

7
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 ****

8
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 Rectangle 8 Which one of the two variables to use?

9
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 values iii.the largest of the values iv.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

10
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 */

11
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

12
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

13
while (currentInt != 0) { currentInt = in.nextInt(); /* Write statements to solve the problem */ } while (currentInt != 0) { currentInt = in.nextInt(); /* Write statements to solve the problem */ } 1 1 2 2 3 3 13 True False currentInt = in.nextInt(); currentInt != 0 ? currentInt != 0 ? 1 1 2 2 3 3 End Write statements to solve problem Flowchart of a while Loop Start

14
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 NumberProperties 14

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google