CSBP219: Object Oriented Programming
Spring 2025
Week 3 – Part 1
Basics of Java
Control Structure – Repetition
Objectives
In this chapter you will learn:
Repetition (looping) control structures
Counter-controlled repetition structures
Sentinel-controlled repetition structures
Flag-controlled repetition structures
EOF-controlled repetition structures
break, continue
for looping (repetition) structures
Nested control structures
Slide 2
while Looping (Repetition) Structure
Slide 3
Counter controlled while Loops
When we know how many times certain
statements need to be executed, we use a
counter controlled while loop
Slide 4
while Loops
Question: How many times this loop is going
to iterate?
Slide 5
Sentinel-Controlled while Loops
A sentinel-controlled while loop uses a
sentinel value to control the loop.
Slide 6
Flag-Controlled while Loops
A flag-controlled while loop uses a Boolean
variable to control the loop.
Slide 7
EOF-Controlled while Loops
When reading from a file, the End-Of-File
while loop tests for the end of file reached
using the scanner’s hasNext() method.
Slide 8
break And continue Statements
The break and continue statements alter
the flow of control in a program.
The break statement provides an immediate
exit from the loop.
The continue statement skips the remaining
statements in the loop & proceeds with the
next iteration of the loop.
Slide 9
break Statements
Example:
Slide 10
continue Statement
Example:
Slide 11
for Looping Structure
Syntax:
Example:
Slide 12
for Looping Structure
Example:
System.out.print( i ); // 10
OR
System.out.print( i ); // Error ???? I don’t know i
Slide 13
for Loop Vs while Loop
Slide 14
Nested Controlled Structures
Nested repetition is when a control structure
is placed inside of the body or main part of
another control structure.
Nested repetition are often used for
processing things like multi-dimensional
arrays.
Example: Output:
for (int i=0; i < 5; i++) { **********
for (int j = 0; j < 10; j++) { **********
System.out.print("*");
}
**********
System.out.println(); **********
} **********
Slide 15
Exercises
Write a program to display all odd numbers between
100 and 200. Use a while loop.
Write a program to display all numbers between 100
and 200 that are divisible by 3. Use a for loop
Write a program that will repeatedly ask the user to
input a number, then count all the positive and
negative numbers entered by the user. The program
stops when the user enters a value of zero.
Write a program that will ask the user to input the
number of items purchased, then for each item, asks
for the item price. The program then calculates and
displays the total price of all items, the highest price
of all items, and the lowest price of all items.
CSBP219: Object Oriented Programming
Spring 2024
Week 3 – Part 2
Basics of Java
Methods
Objectives
In this chapter you will learn:
Pre-defined methods.
Value returning methods.
Void methods.
Actual and formal parameters.
Methods with no parameters
The scope of identifiers.
Method overloading.
Slide 18
Pre-defined Classes and Methods
• Many predefined packages, classes, and
methods in Java
• Library: collection of packages
• Package: contains several classes
• Class: contains several methods
• Method:
Examples set of classes:
of Predefined instructions
• String, Math, Random, …
• Scanner, FileReader, PrintWriter, …
• Integer, Double, Float, Character, …
Check the online Java documentation for the full
information about the pre-defined classes and
methods.
Slide 19
Class String methods
Slide 20
Class String methods
Slide 21
class Math (Package: java.lang)
Method Short Deception
Expression
abs(x) Returns absolute value of the value x.
ceil (x) Returns the smallest integer value that not less than x (double
value).
exp (x) Returns exponential value of the value x.
floor(x) Returns the largest integer value that less than x (double value).
log(x) Returns the natural logarithm of x
log10(x) Returns the common logarithm of x
pow(x) Returns x to power y
max(x, y) Returns the maximum between x and y
min(x, y) Returns the minimum between x and y
round (x) Returns the closest integer to x
sqrt (x) Returns the square root of x
sin (x) / cos (x) Returns sine/cosine of x measured in radians
Slide 22
class Character (Package: java.lang)
Method Short Deception
Expression
isDigit(ch) Returns true if the character ch is a digit
isLetter (ch) Returns true if the character ch is a letter
isLowerCase(ch Returns true if the character ch is a lowercase letter
)
isLUpperCase(c Returns true if the character ch is a uppercase letter
h)
toLowerCase(ch Returns the lowercase equivalent of character ch
)
toUpperCase(ch Returns the upper equivalent of character ch
)
Slide 23
Example
User-Defined Methods
User-defined methods in Java are classified into
two categories:
1. Value-returning methods: methods that have a
return datatype and return a value.
2. Void methods: methods that do not use a return
statement to return a value.
Slide 25
Value-Returning Methods
Value-returning methods compute and
return a value.
Syntax:
Header
Body
Slide 26
Value-Returning Methods
Example:
x Mai
n
Squar
e
number
The variable declared in the header, number, within the
parentheses is called the formal parameter.
square(x) is a call to the method; x is called the actual
parameter or argument.
Slide 27
Void Methods
Void methods do not return any value
To call a void method, you use the method name
together with the actual parameters(if any) in a
stand-alone statement.
When a void method exits, control goes back to the
calling environment at the statement immediately
following the point where it was called.
You may not use a void methods in an expression
(for example, as an argument to
System.out.println()) .
Slide 28
Void Methods
Example:
Slide 29
Methods With No Parameters
A method’s formal parameter list can be
empty, but the parentheses are still needed:
Example
Slide 30
Variable Scope
A variable, x, declared within a
block is accessible only within its
block and by those blocks that are
nested within that block.
Slide 31
Method Overloading
Method overloading: creating several
methods, within a class, with the same name
but with differnet parameter list.
The following methods correctly overload the method myFun:
public void myFun ()
public void myFun (int x, double y)
public void myFun (double x, int y)
public void myFun (int x, double y, char ch)
If a method’s name is overloaded, then in a call to that method, the formal
parameter list of the method determines which method to execute.
Slide 32
Exercises
Write a method that receives the radius of a
circle, then calculates and returns the area of
that circle.
Write a main method that will ask the user to
input the radius, calls the method above, then
displays the returned value of the area.
Write a method that receives the price of an item,
and the discount on that item, then it calculates
and prints the price after discount and after
adding tax ( a constant 5%).
In the main method, ask the user to input the
price of an item, then call the above method to
calculate and print the final price.