Today’s topics Java Looping Upcoming Arrays in Java Reading Great Ideas, Chapter 3.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
CompSci Today’s topics Java The Database Program Upcoming Recursion Reading Great Ideas, Chapter 4.
COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping.
CS303ELoops1 CS 303E Lecture 8: Loops Self-referentially, short for GNU's not UNIX, a Unix-compatible software system developed by the Free Software Foundation.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
CPS Today’s topics Java Information Retrieval Upcoming Database Programs Reading Great Ideas, Chapter 4.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
SNU IDB Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Loops ISYS 350. A Box of Chocolate Repeat this process until box is empty: – Take one chocolate from the box – Eat the chocolate – Box has more chocolate?
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.
1 Lesson: Applets with User Input and Output with GUI ICS4M.
Repetition Statements while and do while loops
Loops ISYS 350. Compute the sum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Sum= 0 Get a number from the list Sum = Sum + the number Repeat.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Java Applet Basics (2). The Body Mass Index Calculator.
CompSci Today’s topics Java Applications Simulation Upcoming Software Engineering (Chapter 7) Reading Great Ideas, Chapters 6.
SNU OOPSLA Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
CompSci Today’s topics Java Numbers Iteration Upcoming More Java Reading Great Ideas, Chapter 3.
CompSci Today’s topics Java Writing Functions/Methods Upcoming Information Retrieval Reading Great Ideas, Chapter 4.
Loops ISYS 350. Two Types of Loops while loop for loop.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Today’s topics Java Input More Syntax Upcoming Decision Trees More formal treatment of grammers Reading Great Ideas, Chapter 2.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
CompSci Today’s Topics Computer Science Noncomputability Upcoming Special Topic: Enabled by Computer -- Decoding the Human Genome Reading Great.
CompSci Today’s topics Java Recursion Upcoming Graphics Reading Great Ideas, Chapter 4 (begin Chapter 5 for graphics)
Today’s topics Java Upcoming Reading Recursion in Graphics
Slides by Evan Gallagher
Today’s topics Java Information Retrieval Upcoming Database Programs
Chapter 4 C Program Control Part I
Today’s topics Java Arrays Upcoming Functions Reading
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Repetition-Counter control Loop
Today’s topics Java Applications Upcoming Reading Graphics
Chapter 6 The while Statement
Lecture Notes – Week 3 Lecture-2
Repetition Control Structure
Repetition Statements (Loops) - 2
Repetition Statements
Building Java Programs
The while Looping Structure
JavaScript 101 Lesson 8: Loops.
ICS103: Programming in C 5: Repetition and Loop Statements
Control Statements:.
Presentation transcript:

Today’s topics Java Looping Upcoming Arrays in Java Reading Great Ideas, Chapter 3

Looping/Iteration/Repetition Much of power of computer comes from the ability to repeat Can use “button pushing” for slow, controlled loop Use language features for full-speed looping While-loop syntax while (logical expression) { statement; … } Repeat statements between braces as long as while logical expression is true

While statement Risk of infinite loop Usually a serious error Something in body of loop must alter logical expression Gauss summation int sum = 0; int k = 0; while (k < 100) { k = k + 1; sum = sum + k; } sum = n*(n+1)/2

Compound Interest Redo our compound interest example Specify how many months to compute loan for Don’t require the push of a button for each month Code: public class CompInterest extends java.applet.Applet implements ActionListener { TextField mInstruct, mBalance; DoubleField gRate, gPrinc, gPay; Button bCompute; IntField gMonths; double rate, princ, pay, balance; int months, k;

Compound Interest.2 public void init(){ mInstruct = new TextField(80); mInstruct.setText( "Enter principal, rate, payment, #months; then press 'Compute'"); gPrinc = new DoubleField(10); gRate = new DoubleField(10); gPay = new DoubleField(10); gMonths = new IntField(10); bCompute = new Button("Compute"); mBalance = new TextField(80); bCompute.addActionListener(this); add(mInstruct); add(gPrinc); add(gRate); add(gPay); add(gMonths); add(bCompute); add(mBalance); }

Compound Interest.3 public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == bCompute) { princ = gPrinc.getDouble(); rate = gRate.getDouble()/12; pay = gPay.getDouble(); months = gMonths.getInt(); balance = princ; k = 0; while (k < months){ balance = balance*(1.0 + rate) - pay; k = k + 1; } mBalance.setText("After " + months + " months at " + 100*rate*12 + "% and payments of " + pay + " the balance is " + balance); }

Many uses for Loops Can count up or down Previous example counts up, month by month “Count-down” needs decrementing from 10, by 1 Don’t have to increment or decrement by 1 Can change by any value E.g., for even number: start at 0, increment by 2 Data dependent loop Logical expression my depend on data Increment may depend on data Data input may provide halting value: called sentinel Whimsical example to draw a diamond

String Methods (functions) String class has many functions Will limit ourselves to 3 common, useful ones String s = ”abcdefg”; // demo string Length int howmany = s.length(); // 7 characters Substring (part of a string) String part = s.substring(0, 3); // ”abc” String let = part.substring(2,3); // ”c” IndexOf (location of one string within another) int pos = s.indexOf(”de”); // 3 int loc = part.indxOf(”f”); // -1 (not found)

Diamond Example public class Diamond extends java.applet.Applet implements ActionListener { TextField tf; TextArea ta; Button bDraw; String stars = "*******************"; String spaces = " "; int k; public void init() { tf = new TextField("Hello "); ta = new TextArea(22, 20); ta.setFont(new Font("Monospaced", Font.BOLD, 12)); bDraw = new Button("Draw"); bDraw.addActionListener(this); add(tf); add(bDraw); add(ta); }

Diamond Example.2 public void actionPerformed(ActionEvent event){ Object cause = event.getSource(); if (cause == bDraw){ tf.setText("Goodbye"); k = 0; while (k < 10){ ta.append(spaces.substring(0,10-k) + stars.substring(0,2*k+1)+"\n"); k = k + 1; }

Diamond Example.3 k = 1; while (k < 10){ ta.append(spaces.substring(0,1+k) + stars.substring(0,19-2*k)+"\n"); k = k + 1; } } } } Contains many new things String: substring TextArea: setFont, append, “\n”

Loop Exercises How many times do the following loops loop? int k = 0, n = 10; while (k < n){ k = k + 1; } int k = 0, n = 10; while (k <= n){ k = k + 1; } int k = 1, n = 10; while (k < n){ k = k + 1; } int k = 1, n = 10; while (k <= n){ k = k + 1; }

Loop Exercises How many times does the following loop loop? What is the value of n? A int s = 30, n = 0; B while (s > 0){ C s = s / 2; Dn = n + 1; } E

CompSci Loop Exercises  How many times does the following loop loop ?  What is the final value of n ? A int s = 30, n = 0; B while (s > 0){ C s = s / 2; Dn = n + 1; } E Need to trace the program: #snT/F A 300 B T C 15 D1D1 B T C 7 D2D2 B T C 3 D3D3 B T C1C1 D4D4 C0C0 D5D5 B F E