CMT4001 -- Programming Software Applications Dr. Xiaohong Gao Repetitions and Methods Week 4.

Slides:



Advertisements
Similar presentations
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Advertisements

Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Loops.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
1 Repetition structures Overview while statement for statement do while statement.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
CMT Programming Software Applications
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Java Programming, 4E Y. Daniel Liang.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Using break and continue
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Building Java Programs Program Logic and Indefinite Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
1 Chapter 5 Control Statements. 2 Objectives F To understand the flow of control in selection and loop statements. F To use Boolean expressions to control.
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.
Methods.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control Structures Lecture 3 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Chapter 5 Function Basics
Chapter 5 Functions.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5 Repetition.
Chapter 4 Control structures and Loops
Chapter 5 Control Statements
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
Lecture Notes – Week 3 Lecture-2
Group Status Project Status.
Chapter 3 Control Statements
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Week 4 Lecture-2 Chapter 6 (Methods).
الكلية الجامعية للعلوم التطبيقية
Corresponds with Chapter 5
Chapter 4: Loops and Iteration
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

CMT Programming Software Applications Dr. Xiaohong Gao Repetitions and Methods Week 4

Contents  To become familiar with repetitions;  To become familiar with while loop;  To become familiar with do loop;  To become familiar with for loop;  To become familiar with nested for loop;  To become familiar with methods;

Repetitions  while Loops  do Loops  for Loops  break and continue

while Loop The syntax for the while loop is: while ( continue – condition) { // Loop body } Example : TestWhile.java

while Loop Flow Chart Continue condition ? Statement(s) Next statement false true

//TestWhile.java public class TestWhile { // main method public static void main(String[]args) { int data; int sum = 0; // read an initial data System.out.println(“Enter an int value); data = MyInput.readInt(); // keep reading until the input is 0

while (data !=0) { sum+= data; System.out.println(“Enter an int value, the program exists if the input is 0”); data =MyInput.readInt(); } System.out.println(“ The sum is” + sum); }

do Loop The do loop is a variation of while loop. Its syntax is : do { // loop body }while(condition) Example: TestDo.java

do Loop Statement(s) Continue condition ? Next statement true false

TestDo.java // TestDo.java; test the do loop public class TestDo { public static void main(String[]args) { int data; int sum = 0; do { data = MyInput.readInt(); sum+= data; }while (data !=0);

TestDo.java System.out.println(“The sum is “+ sum): }

The for Loop The general syntax for for loop is: for( i= initialValue; i<endValue; i++) { // for loop body }

The for Loop int i = 0; while (i < 100) { System.out.println("Welcome to Java! ); i++; } Example: int i; for (i = 0; i<100; i++) { System.out.println("Welcome to Java! ); }

for Loop Flow Chart

The for Loop i = 0; i < 100 ?i=++ System.out.println (“Welcome to Java!”); Next statement

TestSum.java // TestSum.java; Compute sum = public class TestSum { // main method public static void main(String[]args) { //initialize sum float sum = 0; // keep adding 0.01 to sum for(float i=0; i<=1.0f;i+0.01f) sum+=i;

TestSum.java // display result System.out.println(“The sum is “+ sum); }

Using nested for loops //TestMulTable.java; display a multiplication //table; public class TestMulTable { //main method public static void main(String[]args) { //display the table heading System.out.println(“ Multiplication table”); System.out.println(“…………………………………………..”); // display the number title System.out.println(“ | “);

TestMulTable.java for(int j=1; j<=9; j++) System.out.println(“ “ + j): System.out.println(“ “); // print table body for(int i =1; i<=9; i++) { for(int j=1;j<=9;j++) { // display the product and align properly if(i*j <10) System.out.print(“ “ +i*j); else System.out.print(“ ”+i*j); }

TestMulTable.java System.out.println(); }

The break Keyword

The continue Keyword

TestBreak.java //TestBreak.java;test the break keyword //in the loop public class TestBreak { // main method public static void main(String[]args) { int sum =0; int item =0; while(item<5) { item++; sum+= item;

TestBreak.java if(sum >=6) break; } System.out.println(“The sum is”+ sum); }

TestContinue.java //TestContinue.java; test the continue //keyword public class TestContinue { public static void main(String[]args) { int sum = 0; int item = 0; while(item<5) { item++; if(item == 2) continue; sum+=item; }

TestContinue.java System.out.println(“The sum is “+ sum); }

PrintPyramid.java //PrintPyramid.java;print a pyramid of numbers public class PrintPyramid { public static void main(String[]args) { for(int row = 1;row<6; row++) { // print leading spaces for(int column =1;column<6-row;column++) System.out.print(“ “); // print leading numbers for(int num=row; num>=1; num--) System.out.print(num);

PrintPyramid.java //print ending numbers for(int num=2; num<=row; num++) System.out.print(num); // start a new line System.out.println(); }

Methods public static int max(int num1, int num2) { int result = 0; if(num1>num2) result = num1; else result = num2; return result; } Return value returnValueTypemethodName parameters modifier Method body

TestMax.java //TestMax.java; public class TestMax { public static void main(String[]args) { int i = 5; int j = 2; int k = max(i,j); System.out.println(“The maximum between “ +i+” and”+j+” is”+k); }

TestMax.java // a method for finding the max //between two numbers static int max(int num1, int num2) { if(num1>num2) return num1; else return num2; }

TestMax.java void nPrintln(String message, int n) { for( int i=0; i< n; i++) System.out.println(message); }

TestPassByValue.java //TestPassByValue.java public class testPassByValue { public static void main(String[]args) { // declare and initialise variables int num1 =1; int num2 =2; System.out.println(“Before invoking the swap method, num1 is”+num1+”and num2 is”+num2);

TestPassByValue.java //invoke the swap method to attempt to // swap two variables swap(num1, num2); System.out.println(“After invoking the swap method, num1 is”+num1+”and num2 is”+num2); //the method for swapping two variables static void swap(int num1, int num2) { System.out.println(“ inside the swap method”); System.out.println(“ before swapping n1 is”+n1+”n2 is”+ n2);

TestPassByValue.java //swapping n1 with n2 int temp=n1; n1=n2; n2=temp; System.out.println(“ After swapping n1 is “+n1+”n2 is”+n2); }

TestPassByValue.java n1 n2 temp 1 2 n1 n2 1 2 num1 num2 swap(n1,n2) swap(num1, num2) Pass by value Execute swap swap

Summary Repetitions; while loop; do loop; for loop; nested for loop; methods;