Progamming With Java - Lecture Notes
Progamming With Java - Lecture Notes
Progamming With Java - Lecture Notes
Operators Op
== Equal op1 = = op2
!= Not Equal op1 != op2
< Less than op1 < op2
> Greater than op1 > op2
<= Less than or op1 <= op2
equal
>= Greater than or op1 >= op2
equal
Logic Operators
Logical Use Return true if
Operators Op
&& op1 && op2 If both are true
! !op Op is false
int x = 10;
int y = 50;
}}
Control/Conditional Statement
If-else statement
• General form;
if (condition) statement1;
else statement2
• If the condition is true, then statement1 is executed.
Otherwise, statement2 (if it exists) is executed. In no
case will both statements be executed.
• A statement may be a single statement or a compound
statement enclosed in curly braces (i.e. a block).
• The condition is any expression that returns a boolean
value.
• The else clause is optional.
Control/Conditional Statement
if-Else Example
int x = 10;
int y = 50;
}
Control/Conditional Statement
• The do-while loop is especially useful when
you process a menu selection, because you
will usually want the body of a menu loop to
execute at least once.
Control/Conditional Statement
• In the program, the do-while loop is used to
verify that the user has entered a valid choice.
If not, then the user is re-prompted. Since the
menu must be displayed at least once, the do-
while is the perfect loop to accomplish this.
Control/Conditional Statement
Break Statement
• By using break, you can force immediate
termination of a loop, bypassing the
conditional expression and any remaining
code in the body of the loop. When a break
statement is encountered inside a loop, the
loop is terminated and program control
resumes at the next statement following the
loop.
Control/Conditional Statement
// Using break to exit a loop.
class BreakLoop {
public static void main(String args[ ]) {
for(int i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
Control/Conditional Statement
• As you can see, although the for loop is
designed to run from 0 to 99, the break
statement causes it to terminate early, when i
equal 10.
Control/Conditional Statement
Continue Statement
• Sometimes it is useful to force an early
iteration of a loop. That is, you might want to
continue running the loop, but stop
processing the remainder of the code in its
body for this particular iteration.
Control/Conditional Statement
• This is, in effect a goto, just pass the body of the
loop to the loop's end.
• The continue statement performs such an action.
In while and do-while loops, a continue
statement causes control to be transferred
directly to the conditional expression that
controls the loop.
• In a for loop, control goes first to the iteration
portion of the for statement and then to the
conditional expression. For all three loops, any
intermediate code is bypassed.
Control/Conditional Statement
// Demonstration continue statement.
class Continue {
public static void main (String[] args) {
for (int i=0; i<10; i++) {
System.out.print(i + " ");
if (i%2 == 0) continue;
System.out.println ("");
}
}
}
Control/Conditional Statement
• This code uses the % operator to check if i is
even. If it is, the loop continues without
printing a newline.
• As with the break statement, continue may
specify a label to describe which enclosing
loops to continue.
Lecture Session 4
Object Oriented
Programming (OOP) in
Java
OOP Concepts
Features of OOP
• Object Oriented Programming approach is the
bundling of both data and functions into one
unit known as object.
• The functions of a particular object can only
access the data in the object providing high
level of security for the data.
• The functions in the object are known as
member functions or sometimes as methods.
Features of OOP
The key features of OOP programming languages
are; Objects and Classes
• An Object is a program representation of some
real-world thing (i.e person, place or an event).
• Objects can have both attributes (data) and
behaviours (functions or methods). Attributes
describe the object with respect to certain
parameters, and Behaviour or functions describe
the functionality of the object.
Features of OOP
• Example of object
Polygon Object Bank Account
• Note that one class/file contains attributes, and the other class/file contains the main() method for
execution.
Classes and Objects
Example 2
class Student{
int idx;
String name;
int age;
}
class TestStudent1{
public static void main(String args[]){
Student s1=new Student(); //object from Student class
System.out.println(s1.idx);
System.out.println(s1.name);
}
}
Methods
public class methodz {
void computeSine( )
{System.out.println(opposite/hypotenuse);}
}
Objects initialization
//second class with object defined from test_oop calss
public class triangle2 {
public static void main(String[] args) {
Class name name of object = new class name
triangle sine1 = new triangle();//new object from test_oop class
triangle sine2 = new tringle ();
sine1.insertData(10, 7);
sine2.insertData(7, 20);
sine1.computeSine();
sine2.computeSine(); } }
Objects initialization
Initializing objects by Constructor
• A constructor is a special method that is used to initialize objects
• Note, when initializing an object through a constructor the constructor name must be the
same as the class name
public class Main {
String fName;
String sName;
int age;
//defining a Main constructor with the same
public Main (String fn, String sn, int a) {
fName = fn;
sName = sn;
int age = a; }
//defining the main method for executing the code
public static void main(String[] args) {
Main records = new Main( “Daniel”, “Adda“, 78); //initializing Main constructor with data
System.out.println(records.fName + " " + record.sName + “ “ + record.age); } }
Inheritance
• As mentioned earlier, inheritance which is one
of the key features of OOP allows us to create
a class that can take or inherit all the methods
and attributes of another class. This makes it
very flexible for reuse of Java code
• We use the keyword extends to inherit from a
parent class.
Inheritance
public class test_oop {
int a = 4;
int b = 6;
double opposite;
double hypotenuse;
//Parent/Super Class
public class Employee {
//class parameters
private int empNum; //integers declare private cannot be
accessed in other subclasses (data hiding)
private double empSal;
//class methods
public int getEmpNum(){
return empNum;}
public double getEmpSal() {
return empSal;}
}
//finally example
public class ExceptionHandling {
public InvalidNumberException() {
super("You entered an Invalid number; should be
between 1 and 5");
}}
//program that uses the custom exception
//in this program, you want a user to enter numbers between 1
and 5, otherwise an exception should be thrown**/
import java.util.Scanner;
public class CustomException{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
}
//Example 2: program that uses the custom exception
/**here the error text is passed to the constructor
InvalidNumberException() **/
import java.util.Scanner;
public class CustomException{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);