Cit 207 Module Final
Cit 207 Module Final
a) Introduction
b) Object-Oriented Concept
c) Software Reusability
d) Classes and Objects
e) Calling Instance Method
f) Calling a Static Method
g) Passing Variable
Objectives:
At the end of the unit, the student must have:
a) defined object‐oriented programming and basic concepts
b) defined objects and classes
c) used instance variable and method
d) defined static method
e) used passing variables
How much do you know?
1. Use the correct method to print the length of the txt string.
2. Use the correct method to return a random number between 0 (inclusive), and
1 (exclusive).
Math. ;
int x = 50;
int y = 10;
(x y) {
System.out.println( );
}
Introduction
OOP stands for Object Oriented Programming.
Tip: The “Don’t Repeat Yourself” (DRY) principle is about reducing the repetition of
code. You should extract out the codes that are common for the application, and
place them at a single place and reuse them instead of repeating it.
Object-Oriented Concept
Object means a real-world entity such as a pen, chair, table, computer, watch, etc.
Object-Oriented Programming is a methodology or paradigm to design a program
using classes and objects. It simplifies software development and maintenance by
providing some concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Apart from these concepts, these are some other terms which are used in
Object-Oriented design:
Coupling
Cohesion
Association
Aggregation
Composition
Object - any entity that has state and behavior is known as an object. For example,
a chair, pen, table, keyboard, bike, etc. It can be physical or logical. An object can be
defined as an instance of a class. An object contains an address and takes up some
space in memory. Objects can communicate without knowing the details of each
others data or code.
Class - a collection of objects is called class. It is a logical entity. A class can also be
defined as a blueprint from which you can create an individual object. Class doesn’t
consume any space.
The following illustrations shows the difference between class and objects:
Apple Volvo
Fruit Car
Banana Audi
Mango Toyota
Inheritance - when one object acquires all the properties and behaviors of a
parent object, it is known as inheritance. It provides code reusability. It is used to
achieve run-time polymorphism.
Polymorphic Methods
A same message can be sent to objects of different classes
aCircle.surface aRectangle.surface
Encapsulation - binding or wrapping code and data together into a single unit are
known as encapsulation.
Software Reusability
Reusability is a general engineering principle whose importance derives for the
desire to avoid duplication and to capture commonality in undertaking classes of
inherently similar tasks.
Software reuse is the process of implementing new software systems using existing
software information.
Although software reuse provides many benefits for the developers, there are some
disadvantages, like it increases the maintenance cost and occasionally there may be
lack of tool support.
Apply your knowledge. In this part, you will practice what you have
learned.
Objectives:
At the end of the unit, the student must have:
a) defined Exceptions;
b) explained the catching exceptions;
c) explained the throwing exceptions;
d) discussed exceptions categories.
How much do you know?
Insert the missing part of the code below to output “Hello World”. (3pts)
public class MyClass {
public static void main(String[] args) {
. . ("Hello World");
}
}
What will happen when you compile and run the following program? (5pts)
Acquire new knowledge
Lesson Proper
What are exceptions
The Exception Handling in Java is one of the powerful mechanism to handle the
runtime errors so that normal flow of the application can be maintained.
In java, an exception is an event that disrupts the normal flow of the program. It
is an object which is thrown at runtime. (It could considered as an error)
There are given some scenarios where unchecked exception may occur. They are as
follows:
String s = null;
System.out.println(s.length()); // NullPointerException
String s = “abc”;
int i = Integer.parseInt(s); // NumberFormatException
try{
// code that may throw an exception
} catch(Exception_class_name ref){}
try{
//code that may throw an exception
}finally {}
Java catch block is used to handle the Exception by declaring the type of exception
within the parameter. The declared exception must be the parent class exception
(i.e., Exception) or the generated exception type. However, the good approach is to
declare the generated type of exception.
The catch block must be used after the try block only. You can use multiple catch
block with a single try block.
Example 1
Output:
As displayed in the above example, the rest of the code is not executed (in
such case, the rest of the code statement is not printed).
There can be 100 lines of code after exception. So all the code after exception will
not be executed.
Solution by exception handling
Let’s see the solution of the above problem by a java try-catch block.
Example 2
Output:
Now, as displayed in the above example, the rest of the code is executed, i.e., the
rest of the code statement is printed.
Example 3
Output:
Here, we can see that if an exception occurs in the try block, the rest of the block code
will not execute.
Example 4
Output:
Example 5
Output:
Example 6
Output:
25
Example 7
In this example, along with try block, we also enclose exception code in a catch block.
Output:
Here, we can see that the catch block didn’t contain the exception code. So, enclose
exception code within a try block and use catch block only yo handle the exceptions.
Example 8
Output:
Example 9
Output:
Java Multi-catch block
A try block can be followed by one or more catch blocks. Each catch block must
contain a different exception handler. So, if you have to perform different tasks at the
occurrence of different exceptions, use java multi-catch block.
Points to remember
At a time only one exception occurs and at a time only one catch block is
executed.
All catch blocks must be ordered from most specific to most general, i.e.
catch for ArithmeticException must come before catch for Exception.
Example 1
Output:
try{
statement 1;
statement 2;
try{
statement 1;
statement 2;
}
catch(Exception e){
}
}
catch(Exception e){
Java finally block is a block that is used to execute important code suchs as closing
connection, stream, etc.
Note: if you don’t handle exception before terminating the program, JVM executes
finally block (if any)
Finally block in java can be used to put “cleanup” code such as closing a file, closing
connection, etc.
Example 1
Let’s see the java finally example where exception doesn’t occur.
Output:
Example 2
Let’s see the java finally example where exception occurs and not handled.
Output:
Example 3
Let’s see the java finally example where exception occurs and handled.
Output:
Notice that the only difference with the codes on example 2 and 3 is the catch block.
Example 2 used NollPointerException, but example 3 handled the exception by using
ArithmeticException.
Rule: for each try block there can be zero or more catch blocks, but only one finally
block.
Note: the finally block will not be executed if program exits (either by calling
System.exit() or by causing a fatal error that causes the process to abort).
Apply your knowledge. In this part, you will practice what you have
learned.
Inset the missing keyword to handle the error in the code below. (4pts)
{
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
}
(Exception e) {
System.out.println("Something went wrong.");
}
Insert the missing keyword to execute code, after try.. catch, regardless of the result.
(2pts)
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} {
System.out.println("The 'try catch' is finished.");
}
2. Which of these keywords are used for the block to be examined for exceptions?
a) try
b) catch
c) throw
d) check
3. Which of these keywords are used for the block to handle the exceptions
generated by try block?
a) try
b) catch
c) throw
d) check
4. If an error occurs when code inside a try block runs, what will happen next?
a) Nothing will ever happen. The program will always crash.
b) The next line of code in the try block runs.
c) The code starts running again from the beginning of the try block.
d) The code in the catch block runs.
a) Hello
b) World
c) HelloWorld
d) Compilation Error
7. What will be the output of the following Java code?
a) Hello
b) World
c) HelloWorld
d) Compilation Error
a) Hello
b) World
c) HelloWorld
d) Compilation Error
9. What will be the output of the following code?
a) Hello
b) World
c) HelloWorld
d) Compilation Error
a) Hello
b) WorldWorld
c) HelloWorld
d) Compilation Error