0% found this document useful (0 votes)
11 views40 pages

Lecture 3 DrFarouk

The document covers control statements in Java, including selection statements (if, if...else, switch) and repetition statements (while, do, for loops). It also introduces methods, their declaration, calling, passing parameters, and method overloading, along with the Math class and its various functions. Additionally, it includes examples and case studies demonstrating these concepts in practical applications.

Uploaded by

Assta king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views40 pages

Lecture 3 DrFarouk

The document covers control statements in Java, including selection statements (if, if...else, switch) and repetition statements (while, do, for loops). It also introduces methods, their declaration, calling, passing parameters, and method overloading, along with the Math class and its various functions. Additionally, it includes examples and case studies demonstrating these concepts in practical applications.

Uploaded by

Assta king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Object Oriented & Graphical User

Interface Programming in
Java with JBuilder 4

Assoc. Prof. Farouk Al-Fahaidy


Chapter 3 Control Statements
Selection
Statements
–Using if and if...else
–Nested if Statements
–Using switch Statements
–Conditional Operator
Repetition Statements
–Looping: while, do, and for
–Nested loops
–Using break and continue
Selection Statements
 if Statements
 switch Statements
 Conditional Operators
if Statements
if (booleanExpression)
{
statement(s);
}

Example:
if ((i >= 0) && (i <= 10))
{
System.out.println("i is an “ +
“integer between 0 and 10");
}
The if...else Statement
if (booleanExpression)
{
statement(s)-for-the-true-case;
}
else
{
statement(s)-for-the-false-case;
}
if...else Example
if (radius >= 0)
{
area = radius*radius*PI;
System.out.println("The area for the “
+ “circle of radius " + radius +
" is " + area);
}
else
{
System.out.println("Negative input");
}
Nested if Statements

Example 3.1 Using Nested if Statements


This program reads in number of years and
loan amount and computes the monthly
payment and total payment. The interest
rate is determined by number of years.

TestIfElse Run
Conditional Operator
if (x > 0) y = 1
else y = -1;

is equivalent to

y = (x > 0) ? 1 : -1;
switch Statements
switch (year)
{
case 7: annualInterestRate = 7.25;
break;
case 15: annualInterestRate = 8.50;
break;
case 30: annualInterestRate = 9.0;
break;
default: System.out.println(
"Wrong number of years, enter 7, 15, or 30");
}
switch Statement Flow Chart
Repetitions
 while Loops
 do Loops
 for Loops
 break and continue
while Loop Flow Chart
while Loops
while (continue-condition)
{
// loop-body;
}

Example 3.2: Using while Loops

TestWhile.java

TestWhile Run
do Loops
do
{
// Loop body;
} while (continue-condition)
do Loop Flow Chart
for Loops
for (control-variable-initializer;
continue-condition; adjustment-statement)
{
//loop body;
}

int i = 0;
while (i < 100)
{
System.out.println("Welcome to Java! ” + i);
i++;
}
Example:
int i;
for (i = 0; i<100; i++)
{
System.out.println("Welcome to Java! ” + i);
}
for Loop Flow Chart
for Loop Examples
Examples for using the for loop:
 Example 3.3: Using for Loops

TestSum Run

 Example 3.4: Using Nested for Loops

TestMulTable Run
The break Keyword
The continue Keyword
Using break and continue
Examples for using the break and continue
keywords:

 Example 3.5: TestBreak.java

TestBreak Run

 Example 3.6: TestContinue.java

TestContinue Run
Chapter 4 Methods
 Introducing Methods
 Declaring Methods
 Calling Methods
 Passing Parameters
 Pass by Value
 Overloading Methods
 Method Abstraction
 The Math Class
 Recursion (Optional)
Introducing Methods
Method Structure
A method is modifier methodName

a collection returnValueType parameters

of method
heading public static int max(int num1, int num2)
{
statements method
int result = 0;
body
that are if (num1 > num2)
result = num1;
grouped else
result = num2;
together to return result;
}
perform an return value

operation.
Declaring Methods

public static int max(int num1,


int num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
Calling Methods

Example 4.1 Testing the max method


This program demonstrates calling a method
max to return the largest of the int values

TestMax Run
Passing Parameters
void nPrintln(String message, int n)
{
for (int i=0; i<n; i++)
System.out.println(message);
}
Pass by Value

Example 4.2 Testing Pass by value


This program demonstrates passing values
to the methods.

TestPassByValue Run
Overloading Methods
Example 4.3 Overloading the max Method

double max(double num1, double num2)


{
if (num1 > num2)
return num1;
else
return num2;
}

TestMethodOverloading Run
Method Abstraction

You can think of the method body as a black


box that contains the detailed
implementation for the method.
Optional Input Optional return
value

Method Signature

Method body

Black Box
The Math Class
 Class constants:
– PI
–E
 Class methods:
– Trigonometric Methods
– Exponent Methods
– Miscellaneous
Trigonometric Methods
 sin(double a)
 cos(double a)
 tan(double a)
 acos(double a)
 asin(double a)
 atan(double a)
Exponent Methods
 exp(double a)
Returns e raised to the power of a.
 log(double a)
Returns the natural logarithm of a.
 pow(double a, double b)
Returns a raised to the power of b.
 sqrt(double a)
Returns the square root of a.
Rounding Methods
 double ceil(double x)
x rounded up to its nearest integer. This
integer is returned as a double value.
 double floor(double x)
x is rounded down to its nearest integer. This
integer is returned as a double value.
 double rint(double x)
x is rounded to its nearest integer. If x is
equally close to two integers, the even one is
returned as a double.
 int round(float x)
Return (int)Math.floor(x+0.5).
 double round(double x)
Return (long)Math.floor(x+0.5).
min, max, abs, and random
 max(a, b)and min(a, b)
Returns the maximum or minimum of two
parameters.
 abs(a)
Returns the absolute value of the parameter.
 random()
Returns a random double value
in the range [0.0, 1.0).
Using Math Methods
Example 4.4 Computing Mean and Standard
Deviation
Generate 10 random numbers and compute
the mean and standard deviation
n n

 xi n (  xi ) 2
mean  i 1 x 2
i  i 1
n
n deviation  i 1
n 1

ComputeMeanDeviation Run
Case Studies

Example 4.5 Displaying Calendars

The program reads in the month and year


and displays the calendar for a given month
of the year.

PrintCalendar Run
Design Diagram
printCalendar
(main)

readInput printMonth

getStartDay printMonthTitle printMonthBody

getStartDay getNumOfDaysInMonth printMonthName

isLeapYear
Recursion (Optional)

Example 4.6 Computing Factorial

factorial(0) = 1;
factorial(n) = n*factorial(n-1);

ComputeFactorial Run
Fibonnaci Numbers

Example 4.7 Computing Finonacci Numbers

Find Fibonacci numbers using recursion.


fib(0) = 1;
fib(1) =1;
fib(n) = fib(n-2) + fib(n-1); n>=2

ComputeFibonacci Run
Towers of Hanoi

Example 4.8 Solving the Towers of Hanoi


Problem

Solve the towers of Hanoi problem.

TowersOfHanoi Run

You might also like