Lecture 3 DrFarouk
Lecture 3 DrFarouk
Interface Programming in
Java with JBuilder 4
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
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;
}
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
TestMulTable Run
The break Keyword
The continue Keyword
Using break and continue
Examples for using the break and continue
keywords:
TestBreak Run
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
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
TestMax Run
Passing Parameters
void nPrintln(String message, int n)
{
for (int i=0; i<n; i++)
System.out.println(message);
}
Pass by Value
TestPassByValue Run
Overloading Methods
Example 4.3 Overloading the max Method
TestMethodOverloading Run
Method Abstraction
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
PrintCalendar Run
Design Diagram
printCalendar
(main)
readInput printMonth
isLeapYear
Recursion (Optional)
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
ComputeFactorial Run
Fibonnaci Numbers
ComputeFibonacci Run
Towers of Hanoi
TowersOfHanoi Run