SlideShare a Scribd company logo
1 of 5
Download to read offline
Object Oriented Programming
COSC-1202
Laboratory Manual
Fall 2017
LAB 13
Packages in Exception Handling Java
Instructor: Sameen Fatima
_______________________
TEACHING ASISTANT SIGNATURE & DATE
MARKS AWARDED: /10
__________________________________________________________________
KHAWAJA FAREED UNIVERSITY OF ENGINEERING AND INFORMATION TECHNOLOGY
Last Edited By Ms. Sameen Fatima Version 01
Prepared By Ms. Sameen Fatima Date 08 Aug, 2017
LAB 13 Interfaces and Packages in Java
Lab Objectives:
1. Understanding the concept of Interfaces in Java
Software Required:
Netbeans IDE
Packages
The package is both a naming and a visibility control mechanism. You can define classes inside
a package that are not accessible by code outside that package. You can also define class
members that are exposed only to other members of the same package. This allows your classes
to have intimate knowledge of each other, but not expose that knowledge to the rest of the world.
Because of the interplay between classes and packages, Java addresses four categories of
visibility for class members:
 Subclasses in the same package
 Non-subclasses in the same package
 Subclasses in different packages
 Classes that are neither in the same package nor subclasses
TASK 01: Create packages and try to access private, protected, public and default data members in
another package. Also create subclasses a class outside its package and try to access private, protected,
default and public data members.
EXCEPTIONS
Exception handling is a very important yet often neglected aspect of writing robust software. When an
error occurs in a Java program it usually results in an exception being thrown. How you throw, catch and
handle these exception matters. Runtime errors appear in Java as exceptions, exception is a special type of
classes that could be thrown to indicate a runtime error and provide additional data about that error. If a
method is declared to throw an exception, any other method calls it should deal with this exception by
throwing it (if it appears) or handle it.
Three categories of errors (syntax errors, runtime errors, and logic errors):
1. Syntax errors arise because the rules of the language have not been followed. They are detected
by the compiler.
2. You can use the debugging techniques to find logic errors.
3. Exception handling to deal with runtime errors and using assertions to help ensure program
correctness.
TASK 02: Run following code
TASK 03: Run the following code
TASK 04: Run the following code
// File Name InsufficientFundsException.java
import java.io.*;
public class InsufficientFundsException extends Exception {
private double amount;
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
// File Name CheckingAccount.java
import java.io.*;
public class CheckingAccount {
private double balance;
private int number;
public CheckingAccount(int number) {
this.number = number;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) throws InsufficientFundsException {
if(amount <= balance) {
balance -= amount;
}else {
double needs = amount - balance;
throw new InsufficientFundsException(needs);
}
}
public double getBalance() {
return balance;
}
public int getNumber() {
return number;
}
}
// File Name BankDemo.java
public class BankDemo {
public static void main(String [] args) {
CheckingAccount c = new CheckingAccount(101);
System.out.println("Depositing $500...");
c.deposit(500.00);
try {
System.out.println("nWithdrawing $100...");
c.withdraw(100.00);
System.out.println("nWithdrawing $600...");
c.withdraw(600.00);
} catch (InsufficientFundsException e) {
System.out.println("Sorry, but you are short $" + e.getAmount());
e.printStackTrace();
}
}
}
TASK 05: Write a program that counts even numbers between minimum and maximum values.
Maximum and minimum values are provided by user. If minimum value is greater than or equal to
maximum value, the program should throw an InvalidRange exception and handle it to display a message
to the user on the following format: Invalid range: minimum is greater than or equal to maximum.

More Related Content

What's hot

Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in javaTharuniDiddekunta
 
Java căn bản - Chapter8
Java căn bản - Chapter8Java căn bản - Chapter8
Java căn bản - Chapter8Vince Vo
 
Bt0074 oops with java2
Bt0074 oops with java2Bt0074 oops with java2
Bt0074 oops with java2Techglyphs
 
Introduction
IntroductionIntroduction
Introductionrichsoden
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Mohamed Essam
 
JAVA - Throwable class
JAVA - Throwable classJAVA - Throwable class
JAVA - Throwable classasifpatel20
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
GENERIC CODE CLONING METHOD FOR DETECTION OF CLONE CODE IN SOFTWARE DEVELOPMENT
GENERIC CODE CLONING METHOD FOR DETECTION OF CLONE CODE IN SOFTWARE DEVELOPMENT GENERIC CODE CLONING METHOD FOR DETECTION OF CLONE CODE IN SOFTWARE DEVELOPMENT
GENERIC CODE CLONING METHOD FOR DETECTION OF CLONE CODE IN SOFTWARE DEVELOPMENT IAEME Publication
 
Dot net softvamp techno solutions_technical interview questions 2 years exper...
Dot net softvamp techno solutions_technical interview questions 2 years exper...Dot net softvamp techno solutions_technical interview questions 2 years exper...
Dot net softvamp techno solutions_technical interview questions 2 years exper...SoftvampTechnoSoluti
 

What's hot (20)

Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in java
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
Java căn bản - Chapter8
Java căn bản - Chapter8Java căn bản - Chapter8
Java căn bản - Chapter8
 
Bt0074 oops with java2
Bt0074 oops with java2Bt0074 oops with java2
Bt0074 oops with java2
 
Introduction
IntroductionIntroduction
Introduction
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Java
JavaJava
Java
 
Creating your own exception
Creating your own exceptionCreating your own exception
Creating your own exception
 
Getting Program Input
Getting Program Input Getting Program Input
Getting Program Input
 
Event handling
Event handlingEvent handling
Event handling
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 09
Chapter 09Chapter 09
Chapter 09
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1
 
JAVA - Throwable class
JAVA - Throwable classJAVA - Throwable class
JAVA - Throwable class
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Exception handling
Exception handlingException handling
Exception handling
 
Tutorial 6
Tutorial 6Tutorial 6
Tutorial 6
 
GENERIC CODE CLONING METHOD FOR DETECTION OF CLONE CODE IN SOFTWARE DEVELOPMENT
GENERIC CODE CLONING METHOD FOR DETECTION OF CLONE CODE IN SOFTWARE DEVELOPMENT GENERIC CODE CLONING METHOD FOR DETECTION OF CLONE CODE IN SOFTWARE DEVELOPMENT
GENERIC CODE CLONING METHOD FOR DETECTION OF CLONE CODE IN SOFTWARE DEVELOPMENT
 
Dot net softvamp techno solutions_technical interview questions 2 years exper...
Dot net softvamp techno solutions_technical interview questions 2 years exper...Dot net softvamp techno solutions_technical interview questions 2 years exper...
Dot net softvamp techno solutions_technical interview questions 2 years exper...
 

Similar to Lab13 (20)

Exception handler
Exception handler Exception handler
Exception handler
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
 
01slide
01slide01slide
01slide
 
01slide
01slide01slide
01slide
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Java intro
Java introJava intro
Java intro
 
DAY_1.1.pptx
DAY_1.1.pptxDAY_1.1.pptx
DAY_1.1.pptx
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
01slide
01slide01slide
01slide
 
Core java-introduction
Core java-introductionCore java-introduction
Core java-introduction
 
Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event Handling
 
Bca 4020 java programming
Bca 4020   java programmingBca 4020   java programming
Bca 4020 java programming
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
 
Java
JavaJava
Java
 
3350703
33507033350703
3350703
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 

Recently uploaded

办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一F dds
 
Freegle User Survey as visual display - BH
Freegle User Survey as visual display - BHFreegle User Survey as visual display - BH
Freegle User Survey as visual display - BHbill846304
 
VIP Call Girls Moti Ganpur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
VIP Call Girls Moti Ganpur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...VIP Call Girls Moti Ganpur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
VIP Call Girls Moti Ganpur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...Suhani Kapoor
 
(ZARA) Call Girls Talegaon Dabhade ( 7001035870 ) HI-Fi Pune Escorts Service
(ZARA) Call Girls Talegaon Dabhade ( 7001035870 ) HI-Fi Pune Escorts Service(ZARA) Call Girls Talegaon Dabhade ( 7001035870 ) HI-Fi Pune Escorts Service
(ZARA) Call Girls Talegaon Dabhade ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Sustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and ChallengesSustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and ChallengesDr. Salem Baidas
 
Soil pollution causes effects remedial measures
Soil pollution causes effects remedial measuresSoil pollution causes effects remedial measures
Soil pollution causes effects remedial measuresvasubhanot1234
 
VIP Call Girls Mahadevpur Colony ( Hyderabad ) Phone 8250192130 | ₹5k To 25k ...
VIP Call Girls Mahadevpur Colony ( Hyderabad ) Phone 8250192130 | ₹5k To 25k ...VIP Call Girls Mahadevpur Colony ( Hyderabad ) Phone 8250192130 | ₹5k To 25k ...
VIP Call Girls Mahadevpur Colony ( Hyderabad ) Phone 8250192130 | ₹5k To 25k ...Suhani Kapoor
 
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...Suhani Kapoor
 
(AISHA) Wagholi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(AISHA) Wagholi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(AISHA) Wagholi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(AISHA) Wagholi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girlsMumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girlsPooja Nehwal
 
9873940964 High Profile Call Girls Delhi |Defence Colony ( MAYA CHOPRA ) DE...
9873940964 High Profile  Call Girls  Delhi |Defence Colony ( MAYA CHOPRA ) DE...9873940964 High Profile  Call Girls  Delhi |Defence Colony ( MAYA CHOPRA ) DE...
9873940964 High Profile Call Girls Delhi |Defence Colony ( MAYA CHOPRA ) DE...Delhi Escorts
 
Abu Dhabi Sea Beach Visitor Community pp
Abu Dhabi Sea Beach Visitor Community ppAbu Dhabi Sea Beach Visitor Community pp
Abu Dhabi Sea Beach Visitor Community pp202215407
 
Along the Lakefront, "Menacing Unknown"s
Along the Lakefront, "Menacing Unknown"sAlong the Lakefront, "Menacing Unknown"s
Along the Lakefront, "Menacing Unknown"syalehistoricalreview
 

Recently uploaded (20)

办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
 
Call Girls In Dhaula Kuan꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Dhaula Kuan꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCeCall Girls In Dhaula Kuan꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Dhaula Kuan꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
 
Freegle User Survey as visual display - BH
Freegle User Survey as visual display - BHFreegle User Survey as visual display - BH
Freegle User Survey as visual display - BH
 
VIP Call Girls Moti Ganpur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
VIP Call Girls Moti Ganpur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...VIP Call Girls Moti Ganpur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
VIP Call Girls Moti Ganpur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
 
(ZARA) Call Girls Talegaon Dabhade ( 7001035870 ) HI-Fi Pune Escorts Service
(ZARA) Call Girls Talegaon Dabhade ( 7001035870 ) HI-Fi Pune Escorts Service(ZARA) Call Girls Talegaon Dabhade ( 7001035870 ) HI-Fi Pune Escorts Service
(ZARA) Call Girls Talegaon Dabhade ( 7001035870 ) HI-Fi Pune Escorts Service
 
Sustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and ChallengesSustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and Challenges
 
Escort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCR
Escort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCREscort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCR
Escort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCR
 
Soil pollution causes effects remedial measures
Soil pollution causes effects remedial measuresSoil pollution causes effects remedial measures
Soil pollution causes effects remedial measures
 
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls ServicesGandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
 
VIP Call Girls Mahadevpur Colony ( Hyderabad ) Phone 8250192130 | ₹5k To 25k ...
VIP Call Girls Mahadevpur Colony ( Hyderabad ) Phone 8250192130 | ₹5k To 25k ...VIP Call Girls Mahadevpur Colony ( Hyderabad ) Phone 8250192130 | ₹5k To 25k ...
VIP Call Girls Mahadevpur Colony ( Hyderabad ) Phone 8250192130 | ₹5k To 25k ...
 
FULL ENJOY Call Girls In kashmiri gate (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In  kashmiri gate (Delhi) Call Us 9953056974FULL ENJOY Call Girls In  kashmiri gate (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In kashmiri gate (Delhi) Call Us 9953056974
 
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
 
Model Call Girl in Rajiv Chowk Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Rajiv Chowk Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Rajiv Chowk Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Rajiv Chowk Delhi reach out to us at 🔝9953056974🔝
 
Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...
Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...
Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...
 
(AISHA) Wagholi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(AISHA) Wagholi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(AISHA) Wagholi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(AISHA) Wagholi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girlsMumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girls
 
Hot Sexy call girls in Nehru Place, 🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Nehru Place, 🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Nehru Place, 🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Nehru Place, 🔝 9953056974 🔝 escort Service
 
9873940964 High Profile Call Girls Delhi |Defence Colony ( MAYA CHOPRA ) DE...
9873940964 High Profile  Call Girls  Delhi |Defence Colony ( MAYA CHOPRA ) DE...9873940964 High Profile  Call Girls  Delhi |Defence Colony ( MAYA CHOPRA ) DE...
9873940964 High Profile Call Girls Delhi |Defence Colony ( MAYA CHOPRA ) DE...
 
Abu Dhabi Sea Beach Visitor Community pp
Abu Dhabi Sea Beach Visitor Community ppAbu Dhabi Sea Beach Visitor Community pp
Abu Dhabi Sea Beach Visitor Community pp
 
Along the Lakefront, "Menacing Unknown"s
Along the Lakefront, "Menacing Unknown"sAlong the Lakefront, "Menacing Unknown"s
Along the Lakefront, "Menacing Unknown"s
 

Lab13

  • 1. Object Oriented Programming COSC-1202 Laboratory Manual Fall 2017 LAB 13 Packages in Exception Handling Java Instructor: Sameen Fatima _______________________ TEACHING ASISTANT SIGNATURE & DATE MARKS AWARDED: /10 __________________________________________________________________ KHAWAJA FAREED UNIVERSITY OF ENGINEERING AND INFORMATION TECHNOLOGY Last Edited By Ms. Sameen Fatima Version 01 Prepared By Ms. Sameen Fatima Date 08 Aug, 2017
  • 2. LAB 13 Interfaces and Packages in Java Lab Objectives: 1. Understanding the concept of Interfaces in Java Software Required: Netbeans IDE Packages The package is both a naming and a visibility control mechanism. You can define classes inside a package that are not accessible by code outside that package. You can also define class members that are exposed only to other members of the same package. This allows your classes to have intimate knowledge of each other, but not expose that knowledge to the rest of the world. Because of the interplay between classes and packages, Java addresses four categories of visibility for class members:  Subclasses in the same package  Non-subclasses in the same package  Subclasses in different packages  Classes that are neither in the same package nor subclasses TASK 01: Create packages and try to access private, protected, public and default data members in another package. Also create subclasses a class outside its package and try to access private, protected, default and public data members. EXCEPTIONS Exception handling is a very important yet often neglected aspect of writing robust software. When an error occurs in a Java program it usually results in an exception being thrown. How you throw, catch and handle these exception matters. Runtime errors appear in Java as exceptions, exception is a special type of classes that could be thrown to indicate a runtime error and provide additional data about that error. If a method is declared to throw an exception, any other method calls it should deal with this exception by throwing it (if it appears) or handle it. Three categories of errors (syntax errors, runtime errors, and logic errors): 1. Syntax errors arise because the rules of the language have not been followed. They are detected by the compiler. 2. You can use the debugging techniques to find logic errors.
  • 3. 3. Exception handling to deal with runtime errors and using assertions to help ensure program correctness. TASK 02: Run following code TASK 03: Run the following code
  • 4. TASK 04: Run the following code // File Name InsufficientFundsException.java import java.io.*; public class InsufficientFundsException extends Exception { private double amount; public InsufficientFundsException(double amount) { this.amount = amount; } public double getAmount() { return amount; } } // File Name CheckingAccount.java import java.io.*; public class CheckingAccount { private double balance; private int number; public CheckingAccount(int number) { this.number = number; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) throws InsufficientFundsException { if(amount <= balance) { balance -= amount; }else { double needs = amount - balance; throw new InsufficientFundsException(needs); } } public double getBalance() { return balance; } public int getNumber() { return number; } }
  • 5. // File Name BankDemo.java public class BankDemo { public static void main(String [] args) { CheckingAccount c = new CheckingAccount(101); System.out.println("Depositing $500..."); c.deposit(500.00); try { System.out.println("nWithdrawing $100..."); c.withdraw(100.00); System.out.println("nWithdrawing $600..."); c.withdraw(600.00); } catch (InsufficientFundsException e) { System.out.println("Sorry, but you are short $" + e.getAmount()); e.printStackTrace(); } } } TASK 05: Write a program that counts even numbers between minimum and maximum values. Maximum and minimum values are provided by user. If minimum value is greater than or equal to maximum value, the program should throw an InvalidRange exception and handle it to display a message to the user on the following format: Invalid range: minimum is greater than or equal to maximum.