CPE207 Object Oriented Programming (Week 4)
CPE207 Object Oriented Programming (Week 4)
2
A class can have total control over what is
stored in its attributes.
Attributes of a class can be made read-only or write-only.
The users of a class do not know how the class stores its
data.
◦ A class can change the data type of a attributes and users of the
class do not need to change any of their code.
Compile Time
Errors Run Time Errors
-Syntax error Exceptions
in a; -exceptions are events that disrupt
-Type mismatch, wrong the normal flow of the program's
casting instructions during the execution of
- int x = “asdsad”; a program.
- …
Value1/value2 is undefined
7
try{
//statements that may cause an exception
}
catch (Exception(type) e(object)){
//error handling code
//System.out.println(e.getMessage());
}
9
• Class Time1 represents the time
of day. Time1
-hour:int
• private int instance variables
-minute:int
hour, minute and second
represent the time in universal- -second:int
time format (24-hour clock <<constructor>>Time1(hour:int, minute: int,
format in which hours are in the second:int)
range 0–23, and minutes and +setHour(h: int)
seconds are each in the range 0– +setMinute(m: int)
59). +setSecond(s: int)
• public methods setHour(), +toString():String
setMinute(), setSecond(), +toUniversalString():Sting
toUniversalString() and
toString().
10
11
12
class TestClass{
void m(){
int data=50/0;
}
void n(){
m();
}
void p(){
try{
n();
}catch(Exception e){
System.out.println("exception handled");}
}
public static void main(String args[]){
TestClass obj=new TestClass();
obj.p();
System.out.println("normal flow...");
}
https://simplesnippets.tech/throw-throws-in-java-exception-handling-part-3/
13
Scanner scanner = new Scanner(System.in);
If there is no error in input, then try{
no exception is thrown, and the int num = scanner.nextInt();
output will be if (num > 100) {
DONE throw new Exception("Out of bound");
If there is an error in input, one }
of the two exceptions is thrown,
} catch (InputMismatchException e) {
and the output will be
Not an integer
System.out.println("Not an
DONE integer");
or } catch (Exception e) {
Error: Out of bound System.out.println("Error: "+
DONE e.getMessage());
} finally {
System.out.println("DONE");
}
14
The static keyword is a non-access modifiers and used in java
mainly for memory management. It is used with variables,
methods, blocks and nested classes.
It is a keyword that are used for share the same variable or
method of a given class.
This is used for a constant variable or a method that is the same
for every instance of a class.
15
16
If any variable, we declared as static is known as static variable.
The static variable allocate memory only once in class area at the
time of class loading.
17
Suppose we want to store record of all employee of
any company, in this case employee id is unique for
every employee but company name is common for
all. So, use static variable to store the company name.
When we create a static variable as a company name
then only once memory is allocated otherwise it
allocate a memory space each time for every
employee object.
18
19
static keyword always fixed the memory that means that will be
located only once in the program
final keyword always fixes the value that means it makes
variable values constant
Note: As for as real time statement there concern every final
variable should be declared the static but there is no obligation
that every static variable declared as final.
20
21
You need to read an integer value from
keyboard. However, let's say a user typed a
string value.
This will cause an input mismatch exception.
Handle this exception.
(Hint: use InputMismatchException
exception.)
22
• Create a Worker class which contains name salary attributes
• Create setName and setSalary methods.
• Your program should throw an exception when salary value is less than zero. (When
you enter an illegal argument)
• The exception message is “salary amount must be greater than zero”
• In main method handle the exception and display the exception message.
23