IET - 4 OOP Lab Manual Updated
IET - 4 OOP Lab Manual Updated
CSC 1012L
OBJECT ORIENTED PROGRAMMING
(v. 1.1)
Student Name:
Class & Section:
Roll Number:
CGPA:
Email ID:
This lab manual has been prepared to facilitate the students of Information engineering
Technology in studying and analyzing object orientation. Object Orientation is a technique
used extensively in software engineering and IT Projects to build software products as a
bottom-up method. The object orientation allows us to view the world as objects having
attributes and methods. To perform a task objects, communicate with other objects by
invoking methods.
PREPARED BY
Lab manual is prepared by Mr.Waqas Ahmed under the supervision of Head of Department
Dr. Shariq Hussain in the year 2021.
GENERAL INSTRUCTIONS
a. Students are required to maintain the lab manual with them till the end of the semester.
b. All readings, answers to questions and illustrations must be solved on the place
provided. If more space is required then additional sheets may be attached.
c. It is the responsibility of the student to have the manual graded before deadlines as given
by the instructor
d. Loss of manual will result in re-submission of the complete manual.
e. Students are required to go through the experiment before coming to the lab session. Lab
session details will be given in training schedule.
f. Students must bring the manual in each lab.
g. Keep the manual neat clean and presentable.
h. Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarised and
no re submission will be entertained.
i. Marks will be deducted for late submission.
j. Error handling in a program is the responsibility of the Student.
VERSION HISTORY
4 10
5 10
6 10
7 10
8 10
9 10
10 10
11 10
12 10
13 10
14 10
SESSIONAL TOTAL
PROJECT PHASE - I 10
PROJECT PHASE - II 20
PROJECT TOTAL 30
1. Objec
t
ives:
Learning Java Language
(a).
(b). Difference between C++ and Java
4. Software Required:
(a). Windows OS
(b). NetBeans / Eclipse Kepler
5. Java Compiler is software that compiles the Java code into Bytecode. Bytecode is a set
of commands that the java virtual machine understands.
6. Java Runtime Environment (JRE) is the virtual machine over which the java code is
executed. This virtual machine executes the java code. The JRE must be provided before
executing a Java program.
8.
1. An identifier may use all letters, a – z and A – Z, all digits, 0 – 9, the underscore (_)
and $.
2. An identifier may not begin with a digit.
3. An identifier may not be a Java reserved word. Reserved words, or key words are
words that have a special meaning in Java.
abstact boolean break byte case char class const continue default double else
extends final finally for goto if implements import int interface long native new
private protected public return short
strictfp super switch synchronized this throws transient try void volatile
1. Identifiers: Choosing names that indicate the purpose of the class, method or
data value is known as self – documentation.
2. Class Names: begin with a capital letter, additional words are capitalized.
Examples: FirstClass, HelloWorld, JFrame, JOptionPane.
3. Variable Names: begin with a lower case letter, additional words are
capitalized. Examples: myWindow, visible, width, height, interestRate € Note:
no parentheses
4. Method Names: begin with a lower case letter, additional words are
capitalized. Examples: setTitle(),setSize(), showMessageDialog() € Note:
always parentheses
At all times, you are expected to follow the Java guidelines for choosing
identifiers. Failure to do so will result in points being deducted.
package kamran;
double v1 = 7; boolean
v2 = false;
int v3 = 9; float
v4 = 11; long v5
= 0; String v6 =
null;
System.out.println("values");
System.out.println("Val1 = " + v1);
System.out.println("Val2 = " + v2);
System.out.println("Val3 = " + v3);
System.out.println("Val4 = " + v4);
System.out.println("Val5 = " + v5);
System.out.println("Val6 = " + v6);
}
Output:
package kamran;
if(style == style2)
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
Output:
Code 01:
package kamran;
Output:
Code 02:
package kamran;
Code 03:
System.out.println("~a = " + ~a);
package kamran;
Output:
Code 04:
package kamran;
Code 05:
package kamran;
a &= b;
System.out.print ln("a= " + a);
}
21 CSC 103L – Introduction to Java
}
Output:
Code 06:
package kamran;
Output:
These are used to choose the path for execution. There are some types of control
statements:
• If statement
• If-else statement
• If-else if statement/ ladder if statements
• Switch statement
2. Loops in Java
Code Here:
package kamran;
import java.util.*;
Program 13: Write a java program to display the first 20 odd numbers.
Code Here:
package kamran;
}
OUTPUT
Program 13 : Write a java Program to check the input number is Prime or not
Code Here:
package kamran;
if (num % i == 0) {
flag = true;
break;
}
}
Code Here:
package kamran;
for(int i=0;i<N;i++){
System.out.println("3 3");
if(i<N){
System.out.println("3333333");
}
}
}
}
2 CSC 103L – Introduction to Java
8
Output
Program 15: Write a program to give your own examples of control statements.
Code Here:
1 nested for
Code
package kamran;
if(marks>50){
System.out.println("pass");
public class Nestedfor {
if(marks<50){
public static void main(String args[])
System.out.println("fail");
} {
} int class1=1;
int students=10;
for(int i=1;i<=class1;i++){
Output System.out.println("class="+i);
for(int j=1;j<=students;++j){
System.out.println("student no"+j);
}
}
}
Output
3
Do statement
Code
package kamran;
Summary: This is the basic tutorial of Java that allows the student to create basic Java
programs similar to C++/C. The programs include variables, arithmetic statements and
Operators , Control Structures – Loops.
Java Arrays
Normally, an array is a collection of similar type of elements which has contiguous memory location.
Java array is an object which contains elements of a similar data type. Additionally, the elements of an
array are stored in a contiguous memory location. It is a data structure where we store similar elements.
We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, and 2nd element is
stored on 1st index and so on.
Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need to use the
size of operator.
In Java, array is an object of a dynamically generated class. Java array inherits the Object class, and
implements the Serializable as well as Cloneable interfaces. We can store primitive values or objects in an
array in Java. Like C/C++, we can also create single dimensional or multidimensional arrays in Java.
Moreover, Java provides the feature of anonymous arrays which is not available in C/C++.
Advantages
Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime.
To solve this problem, collection framework is used in Java which grows automatically.
Multidimensional Array
Instantiation of an Array in
Java
arrayRefVar=newdatatype[size];
Let's see the simple example of java array, here we are going to declare, instantiate, initialize and traverse
an array.
4/14
3/24/2021
class Testarray{
public static void main(String args[]){
int a[]= new int[ 5 ] ; //declaration and instantiation
a[ 0 ]= 10 ; //initialization
a[ 1 ]= 20 ;
a[ 2 ]= 70 ;
a[ 3 ]= 40 ;
a[ 4 ]= 50 ;
//traversing array
Code Output:
5/14
3/24/2021
We can declare, instantiate and initialize the java array together by:
Code
Output:
6/14
3/24/2021
7/14
3/24/2021
We can also print the Java array using for-each loop. The Java for-each loop prints the array elements one
by one. It holds an array element in a variable, then executes the body of the loop.
Let us see the example of print the elements of Java array using the for-each loop.
Code
Output:
8/14
3/24/2021
We can pass the java array to method so that we can reuse the same logic on any array.
Let's see the simple example to get the minimum number of an array using a method.
System.out.println(min);
}
Code Output:
9/14
3/24/2021
10/14
3/24/2021
11/14
3/24/2021
We can also return an array from the method in Java.
CodeOutput
:
In such case, data is stored in row and column based index (also known as matrix form).
12/1
4
3/24/2021
Syntax to Declare Multidimensional Array in Java
dataType[][]arrayRefVar;(or)
dataType [][]arrayRefVar;(or)
dataType arrayRefVar[][];(or)
dataType []arrayRefVar[];
Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional array.
13/1
4
3/24/2021
Code Output:
If we are creating odd number of columns in a 2D array, it is known as a jagged array. In other words, it is
an array of arrays with different number of columns.
14/1
4
3/24/2021
Code
Outpt
u :
15/1
4
3/24/2021
16/1
4
3/24/2021
Code Output:
Since, Java array implements the Cloneable interface, we can create the clone of the Java array. If we
create the clone of a single-dimensional array, it creates the deep copy of the Java array. It means, it will
copy the actual value. But, if we create the clone of a multidimensional array, it creates the shallow copy of
the Java array which means it copies the references.
17/1
4
3/24/2021
System.out.println(
"Printing clone of the array:"
);
intcarr[]=arr.clone();
fo (inti:carr)
r
System.out.println(i);
System.out.println(
"Are both equal?"
);
System.out.println(arr==carr);
}}
Code Output:
18/1
4
3/24/2021
19/1
4
3/24/2021
Let's see a simple example that adds two matrices.
}}
Code Output:
20/1
4
3/24/2021
Multiplication of 2 Matrices in Java
In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns
of the second matrix which can be understood by the image given below.
Let's see a simple example to multiply two matrices of 3 rows and 3 columns.
21/1
4
3/24/2021
Code Output:
22/1
4
3/24/2021
Lab Exercise:
1. Declare three arrays of two dimensional of size m and n that is a[][],b[][] and c[][].
3. Compute multiplication and store the product in c[][] array by using c[i][j]=c[i][j]+a[i][k]*b[k][j];
23/1
4
3/24/2021
c[m][n]+=a[m][k]*b[k][n];
System.out.print(c[m][n]+" ");
System.out.println();
}}
Output
24/1
4
3/24/2021
MAXIMUM MARKS: 10
DEADLINE: After OHT - I
Instructions
This is a project assignment and is based on some of the lab experiments
carried out by you.
Plagiarism is strictly forbidden.
You have to create the project for the given language specification.
You have to show the working project for evaluation before deadline.
25/1
4
3/24/2021
The evaluation is based on project demonstration, working of the project
for different test patterns and associated viva.
Project will be graded as a syndicate effort.
1. Objectives:
(a). Syndicate formation
(b). Project proposal
2. Programming Language: Java
3. Software Required: NetBeans/Kawa/Eclipse/JCreator
4. TASK
You are required to create a project proposal identifying what kind of project
you are going to develop. The project proposal must have the following
(a). Abstract statement
(b). Some functional requirements i.e., what the project will do
(c). A snapshot of how will the GUI look like
(d). Hard copy of proposal
26/1
4
3/24/2021
PROJECT –OBJECT ORIENTED PROGRAMMING - PHASE – II
Goal: In this course our main goal is to learn how to build and evolve small to large-scale programs using object-
oriented programming, and work in teams learning from each other.
1. Design
2. Primitives
3. Implementation
In Design, how do we think about a program in terms of objects? To answer this question, we explore CRC
cards, UML, and design patterns.
In Primitives, how do we express object orientation in terms of encapsulation, abstraction, inheritance and
polymorphism? To answer this question, we explore classes, interfaces, method dispatch, method overriding,
generics, operator overloading, and reflection, etc.
In Implementation, how do we realize object-oriented primitives? To answer this question, we explore dynamic
method dispatch, garbage collection and automatic memory management in detail.
Design and primitives matter because they represent the essence of object-oriented programming.
Implementation matters because it enables us to debug object-oriented programs and tune their performance.
General Rules:
27/1
4
3/24/2021
Submission Method:
All project files are zipped and submitted named as “proj_LeaderStudentID.zip”, where “LeaderStudentID” is
replaced by a leader team member chosen by all team members. The zip file should contain:
1. All source code used to develop the project, either as a rar file, or a folder of the project packages and
files.
2. A Design document contains a class diagram describing the project classes, and associations, and one
paragraph describing the methods, one paragraph as a user manual, and one paragraph describing team
member’s individual contributions.
Grading Criteria:
The project is worth 20% of your final mark. These marks are graded individually by asking each student in the
presentation for their contribution and testing their understanding.
29/1
4
3/24/2021
Sample Project Details
1. Bank Management System:
Develop an application to help a bank manager manage customer accounts. The bank offer several bank
accounts types. Each customer can have one or more accounts. The customer can go to the operations
permitted by the account type, such as deposit, withdraw, or balance enquire. The bank manages the account
by debiting the fees, or crediting the profits. Both the bank employees and the customers can print reports
about the current account details.
Design:
A. The Account is a general account class that contains balance as instance variable, deposit,
withdraw, and balanceEnquiry as instance methods.
B. Checking Account is a subclass from the Account class that allows overdraft while withdrawing
(making the balance go below zero up to the specified credit limit), by debiting the account
balance with an overdraft fee. It has a creditlimit as an instance variable.
C. The Saving Account is a subclass from the Account class that has an interest rate as an instance
variable. The system credit the balance with monthly interest based on the account balance and
the interest rate.
D. The loan account is a subclass from the Account class that has principal amount, interest rate,
loan duration in months as instance variables. The loan balance is debited by monthly interest
each month based on the interest rate and the loan balance.
E. Each customer can have any number of accounts of any type.
F. The banking system provide the customer with an interface to access all banking operations
described above and review reports about transactions and current balance.
G. A banking administrator can print a report about all customers and their current balances.
30/1
4
3/24/2021
Sample data include:
customers.txt
accounts.txt
Loan
Account ID Customer ID Type Balance CreditLimit or Principal Duration
1 1 Saving …
2 1 Loan …
Checking
3 2 Account …
accountTransactions.txt
3 … Fees …
31/1
4
3/24/2021
Confucius
Marie Curie
32/1
4