= 5)] print text counter++ loop initialization loop condition loop body loop condition update"> = 5)] print text counter++ loop initialization loop condition loop body loop condition update">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java the UML Way version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and.

Similar presentations


Presentation on theme: "Java the UML Way version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and."— Presentation transcript:

1 Java the UML Way http://www.tisip.no/JavaTheUmlWay/ version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6 Loop as a Control Structure What is a loop?page 2 The while statement with an examplepage 3 Testing loopspage 5 A graphics examplepage 6-7 The for statementpage 8-9 Nested control structurespage 10 The do-while statementpage 11 Choosing the right loop statementpage 12 Controlling input datapage 13

2 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 2 What is a Loop? An algorithm is “a limited and ordered set of well-defined rules for solving a problem” (see chapter 2) Three categories of execution order or control structures: –sequential (chapters 2-4) –selection (chapter 5, the if and switch statements) –loop (this chapter, the while, for, and do statements) A loop makes it possible to repeat a single statement or a block one or more times. A counter-controlled loop is a loop where the number of iterations is known in advance.

3 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 3 The while Statement with an Example class PrintManyLines { public static void main(String[] args) { int counter = 0; while (counter < 5) { System.out.println("This is a line."); counter++; } Printout: This is a line. The while statement: while (boolean expression) statement/block Solve problem, page 163. counter = 0 [counter < 5] [counter >= 5)] print text counter++ loop initialization loop condition loop body loop condition update

4 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 4 class SearchForNumbers { public static void main(String[] args) { final int limit1 = 0; final int limit2 = 70; final int limit3 = 80; final int limit4 = 100; int noOfOK = 0; int number = (int) ((limit4 - limit1) * Math.random() + 1); while (number limit3) { noOfOK++; System.out.println(number); number = (int) ((limit4 - limit1) * Math.random() + 1); } System.out.println("We had to draw " + noOfOK + " numbers until we got an illegal one: " + number); } A Loop with a General Condition

5 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 5 Testing Loops Formulate test data catching the following situations: –The loop runs 0 times. –The loop runs exactly one time. –The loop runs several times (the most common situation). –Is it possible to construct data that never makes the loop condition false? If yes, that means you’ve created something that can become an endless loop. This is usually a mistake. Solve problems, 1-3 page 165.

6 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 6 A Graphics Examples The placement of the 11 spots in the applet window is randomly distributed. We have these constants declared: private static final int windowWidth = 400; // have to agree private static final int windowHeight = 300; // with the html file private static final int diameter = 20; private static final int center = diameter / 2; private static final int noOfLines = 10; A random x value is drawn: x = (int) ((windowWidth - diameter) * Math.random() + 1); A circle is drawn: window.fillOval(x, y, diameter, diameter); A line between two points: window.drawLine(x1, y1, x2, y2); Problems –How many loop iterations do we need? –State the loop condition. –Construct the rest of the loop.

7 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 7 Solution import java.awt.*; import javax.swing.*; public class SpotsAndLines extends JApplet { public void init() { Container content = getContentPane(); Drawing theDrawing = new Drawing(); content.add(theDrawing); } class Drawing extends JPanel { private static final int windowWidth = 400; private static final int windowHeight = 300; private static final int diameter = 20; private static final int center = diameter / 2; private static final int noOfLines = 10; Solve all problems, pp. 167-168. public void paintComponent(Graphics window) { super.paintComponent(window); int x = (int) ((windowWidth - diameter) * Math.random() + 1); int y = (int) ((windowHeight - diameter) * Math.random() + 1); window.fillOval(x, y, diameter, diameter); int counter = 0; while (counter < noOfLines) { int lastX = x; int lastY = y; x = (int) ((windowWidth - diameter) * Math.random() + 1); y = (int) ((windowHeight - diameter) * Math.random() + 1); window.drawLine(lastX + center, lastY + center, x + center, y + center); window.fillOval(x, y, diameter, diameter); counter++; }

8 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 8 The for Statement The while statement may be used for all kinds of loops. However, for counter-controlled loops the for statement is usually used. Problem: Identify loop initialization, condition, and condition update in these two loop constructions. Syntax: for (loopInitialization; loopCondition; loopConditionUpdate) statement/block Variables declared in the initialization portion of the for statement have the loop body as their scope. class PrintManyLines { public static void main(String[] args) { for (int counter = 0; counter < 5; counter++) { System.out.println("This is a line."); counter++; } class PrintManyLines { public static void main(String[] args) { int counter = 0; while (counter < 5) { System.out.println("This is a line."); counter++; }

9 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 9 The for Statement, Examples Draws 30 circles one after the other: int x = 0; int y = 50; for (int counter = 0; counter < 30; counter++) { window.drawOval(x, y, 20, 20); x += 20; } Other for statements: –Counts down from 1000 to 501 for (int number = 1000; number > 500; number--) { –Increases by 5 for every iteration for (int xPos = 100; xPos < 800; xPos += 5) { –Member no. from 101 to 900 for (int membNo = 101; membNo <= 900; membNo++) {

10 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 10 Nested Control Structures if statements can be nested inside each other, loops can be contained within other loops, if statements can be inside loops or loops inside if statements, etc... What will be printed out when this program runs? class SumNumbers { public static void main(String[] args) { for (int lineCounter = 0; lineCounter < 10; lineCounter++) { int sum = 0; for (int number = 1; number <= lineCounter; number++) { sum += number; System.out.print(number + " "); } System.out.println("The sum is: " + sum); } Solve problem 1, page 170.

11 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 11 The do-while Statement The do-while statement distinguishes itself from the other loop statements in that its loop body is always executed at least once. Useful when input data should be controlled: An example: int noOfStudents; do { String inputText = JOptionPane.showInputDialog("How many students (max. 30)? "); noOfStudents = Integer.parseInt(inputText); } while (noOfStudents 30); Syntax: do statement/block while boolean expression

12 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 12 Choosing the Right Loop Statement Use a for statement if the number of loop iterations is known in advance, or if there is an integer variable that increases or decreases by a fixed value for every iteration. Use a while statement if the number of loop iterations is controlled by a general condition. Use a do-while statement if the number of loop iterations is controlled by a general condition and the loop will always be gone through at least once.

13 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 6, page 13 Controlling Input Data Show program listing 6.5 pp. 174-175.


Download ppt "Java the UML Way version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and."

Similar presentations


Ads by Google