CMT4001 -- Programming Software Applications Dr. Xiaohong Gao Repetitions and Methods Week 4.
-
date post
22-Dec-2015 -
Category
Documents
-
view
216 -
download
2
Transcript of CMT4001 -- 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;
while Loop
The syntax for the while loop is:
while ( continue – condition){ // Loop body}
Example: TestWhile.java
//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
TestDo.java
// TestDo.java; test the do looppublic class TestDo{ public static void main(String[]args) { int data; int sum = 0; do { data = MyInput.readInt(); sum+= data; }while (data !=0);
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! ); }
TestSum.java
// TestSum.java; Compute sum =0.01+0.02+...+1public class TestSum{ // main method public static void main(String[]args) {//initialize sum float sum = 0; // keep adding 0.01 to sumfor(float i=0; i<=1.0f;i+0.01f) sum+=i;
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 bodyfor(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); }
TestBreak.java
//TestBreak.java;test the break keyword//in the looppublic class TestBreak{ // main method public static void main(String[]args) { int sum =0; int item =0;
while(item<5) { item++; sum+= item;
TestContinue.java
//TestContinue.java; test the continue //keywordpublic class TestContinue { public static void main(String[]args) { int sum = 0; int item = 0;
while(item<5) { item++; if(item == 2) continue; sum+=item; }
PrintPyramid.java//PrintPyramid.java;print a pyramid of numberspublic 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
returnValueType methodName
parameters
modifier
Methodbody
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.javapublic class testPassByValue{ public static void main(String[]args) { // declare and initialise variables int num1 =1; int num2 =2; System.out.println(“Before invoking the swapmethod, num1 is”+num1+”and num2 is”+num2);
TestPassByValue.java
//invoke the swap method to attempt to// swap two variablesswap(num1, num2);System.out.println(“After invoking the swap method, num1 is”+num1+”and num2 is”+num2);
//the method for swapping two variablesstatic 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
2
2
1
n1
n2
temp
1
2
n1
n2
1
2
num1
num2
swap(n1,n2)
swap(num1, num2)
Pass by value
Execute swap
swap