LM OOP Java (Azhar Jamil)
LM OOP Java (Azhar Jamil)
Lab Manual
Object Oriented
Programming
A simplified practical work book of Object Oriented programming course
for Computer Science, Information Technology and Software
Engineering students
Student Name:______________________________________
Registration No:___________________________________
Section / Semester: _________________________________
Submission Date: _____________________________________
Student Signature: __________________________________
Azhar Jamil
Lab Manual Object Oriented Programming: First Edition
(Publisher:???)
Author(s): Azhar Jamil
ISBN: ????
Publication Date: ????
A publication of BIIT Series
Copyrights © Reserved By Authors
Learning Objectives: By the end of the lab work, students should have following skills;
10
11
12
13
14
15
16
Table of Contents
LAB # 1________________________________________________________________________5
Theory: Familarity with Java Environment_____________________________________________5
1.1: Installing JDK________________________________________________________________7
Task 1.1:_Executing Basic Java Commands___________________________________________15
Comments/Signature (Student & Teacher):____________________________________________15
LAB # 2________________________________________________________________________16
Theory: Primitive Data Types, Expressions and Branches__________________________________7
Task 2.1: Exercise - 1 (Basics)______________________________________________________19
Task 2.2: Bike Race _____________________________________________________________20
Comments/Signature (Student & Teacher):____________________________________________21
LAB # 3_______________________________________________________________________22
Theory: Use of Operators, Expressions, control Flow____________________________________22
Task 3.1: Implementation of Binary search mechanism___________________________________21
Comments/Signature (Student & Teacher):____________________________________________21
LAB # 4_______________________________________________________________________27
Theory: Introduction to classes______________________________________________________27
Task 4.1: Use of Classes __________________________________________________________27
Task 4.2: Array of Objects________________ ________________________________________ 28
Comments/Signature (Student & Teacher):____________________________________________30
LAB # 5_______________________________________________________________________31
Theory: An Overview of Objects and Classes (cont..)____________________________________31
Task 5.1: Demonstration of Classes and Objects________________________________________31
Task 5.2: Access Specifiers_______________________________________________________ 32
Comments/Signature (Student & Teacher):____________________________________________32
LAB # 6 ______________________________________________________________________33
Theory: Theory: Constructors and Destructors_________________________________________33
Task 6.1: Counting number of objects created__________________________________________33
Comments/Signature (Student & Teacher):____________________________________________35
LAB # 7_______________________________________________________________________36
Theory: Constructor overloading____________________________________________________38
Task 7.1: Auto Generation of Student Arid No_________________________________________38
Comments/Signature (Student & Teacher):____________________________________________40
LAB # 8_______________________________________________________________________41
Theory: Friend Functions and Friend Classes__________________________________________ 41
Task 8.1: Exercise________________________________________________________________42
Comments/Signature (Student & Teacher):____________________________________________44
LAB # 9_______________________________________________________________________45
Theory: Inheritance_______________________________________________________________45
Task 9.1: Exercise________________________________________________________________46
Comments/Signature (Student & Teacher):____________________________________________48
LAB # 10______________________________________________________________________49
Theory: inheritance (cont…)_______________________________________________________49
Task 10.1: Exercises______________________________________________________________50
Comments/Signature (Student & Teacher):____________________________________________51
LAB # 11______________________________________________________________________52
Theory: Inheritance and Method Overriding___________________________________________52
Task 11.1: Exercises______________________________________________________________53
Comments/Signature (Student & Teacher):____________________________________________57
LAB # 12______________________________________________________________________58
Theory: File Handling_____________________________________________________________58
Task 12.1:Files Input Output_______________________________________________________58
Task 12.2: Reading an Lines fromDisk_______________________________________________ 61
Comments/Signature (Student & Teacher)_____________________________________________62
LAB # 13______________________________________________________________________63
Theory:Character I/O_____________________________________________________________63
Task 13.1: Writing characters_______________________________________________________63
Task 13.2: Reading characters______________________________________________________64
Comments/Signature (Student & Teacher):____________________________________________66
LAB # 14______________________________________________________________________67
Theory: Object Read Write_________________________________________________________67
Task 14.1: Writing Objects_________________________________________________________67
Comments/Signature (Student & Teacher):____________________________________________73
LAB # 15______________________________________________________________________74
Theory: Object Read Write (cont..)__________________________________________________74
Task 15.1: Writing an object to Disk_________________________________________________74
Task 15.2: Reading an object from Disk _____________________________________________76
Task 15.3: Random Access_________________________________________________________78
Comments/Signature (Student & Teacher):____________________________________________79
LAB # 16______________________________________________________________________80
Theory: String___________________________________________________________________80
Task 16.1: String functions_________________________________________________________80
Comments/Signature (Student & Teacher):____________________________________________86
-5-
LAB # 1
5
-6-
6
-7-
3. Click on dowload.
7
-8-
Figure 1
8
-9-
Figure 3
9
- 10 -
Figure 5
10
- 11 -
Figure 7
11
- 12 -
Figure 9
12
- 13 -
Figure 11
13
- 14 -
Figure 13
14
AJ/Lab Manual -15- Object Oriented Programming
Execute the following commands (in the given sequence) and write the answers below each expression.
1. System.out.println(“Hello Class!”);
4. System.out.print(4);
6. System.out.println(true);
15
AJ/Lab Manual -16- Object Oriented Programming
LAB # 2
Primitive Data Types, Expressions and Branches
16
AJ/Lab Manual -17- Object Oriented Programming
a = b = c = d;
7. What is print out? Key is determining when the value of a variable is changed.
int a = 1;
System.out.print("a=");
System.out.print( (a = 2) + a);
int b = 1; System.out.print(", b=");
System.out.print( b + (b = 2));
8. boolean completed = false;
if (completed || !completed) {
char ch = 'b';
String str = "|";
switch (ch) {
case 'a': str += ch;
case 'b': str = ch + str;
case 'c': str += 'c';
break;
case 'd': str += 'd';
default:
str += ch;
break;
}
System.out.print(str);
10. boolean happy = false; //Will both of the following if statements result in the same output?
if (happy) System.out.println("happy");
else System.out.println("not happy");
System.out.println("done");
17
AJ/Lab Manual -18- Object Oriented Programming
if (happy) {
System.out.println("happy");
} else {
System.out.println("not happy");
}
System.out.println("done");
11. Write the code to write the category name for a character in a char variable named ch. Ignore case.
You may try using both switch and if statements to see which is easier for someone else to read.
Characters Category
12. Write a program to prompt the user for their age. Based on their age print out which generation they
Lab Manual BIIT Series
are, when they think youth ends, when old age begins and what age is considered the prime of life.
Aim: To write a JAVA program to display default value of all primitive data type of JAVA
class defaultdemo {
static byte b;
static short s;
static int i;
static long l;
static float f;
static double d;
static char c;
System.out.println("Int :"+i);
System.out.println("Long :"+l);
System.out.println("Float :"+f);
System.out.println("Double :"+d);
System.out.println("Char :"+c);
System.out.println("Boolean :"+bl);
}
}
Write Output here:
|
|
|
|
|
|
19
AJ/Lab Manual -20- Object Oriented Programming
Aim: Five Bikers Compete in a race such that they drive at a constant speed which may or may not
be the same as the other. To qualify the race, the speed of a racer must be more than the average
speed of all 5 racers. Take as input the speed of each racer and print back the speed of qualifying
racers.
Program: import java.util.*;
class racedemo {
public static void main(String[] args)
{
float s1,s2,s3,s4,s5,average;
Scanner s = new Scanner(System.in);
System.out.println("Enter speed of first racer:");
s4 = s.nextFloat();
System.out.println("Enter speed of fifth racer:");
s5 = s.nextFloat();
average=(s1+s2+s3+s4+s5)/5;
if(s1>average)
System.out.println("First racer is qualify racer:");
else if(s2>average)
System.out.println("Second racer is qualify racer:");
else if(s3>average)
System.out.println("Third racer is qualify racer:");
else if(s4>average)
System.out.println("Fourth racer is qualify racer:");
else if(s5>average)
System.out.println("Fifth racer is qualify racer:");
}}
|
|
|
|
|
|
|
|
|
|
|
21
AJ/Lab Manual -22- Object Oriented Programming
LAB # 3
Use of Operators, Expressions, control Flow
int x = 34;
int y = x++;
3. int x = 20;
int j = 4;
int y = x++ + --j;
4. int m = 14;
int n = --m + m--;
5. int m = 4;
int n = m -- - -- m;
6. int i=10;
while(i>1){
System.out.println(++i);
}
7. int i = 4;
while ( i != 1 ) {
System.out.println( i); i -= 2; }
22
AJ/Lab Manual -23- Object Oriented Programming
return s;
}
16. What is the sum after this executes?
int sum = 0; for (int i = 5; i >= 0; --i) {
for (int j = 1; j < 4; ++j) {
if (j % 2 == 1)
continue;
sum++; }
if (i > 0)
break; }
System.out.println("sum=" + sum);
24
AJ/Lab Manual -25- Object Oriented Programming
Aim: To write a JAVA program to search for an element in a given list of elements using binary search
mechanism.
Program:
import java.util.Scanner;
class binarysearchdemo
{
public static void main(String args[])
{
int n, i, num,first, last, middle;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in);
num = s.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( a[middle] < num )
first = middle + 1;
else if ( a[middle] == num )
{
System.out.println("number found");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
25
AJ/Lab Manual -26- Object Oriented Programming
}
if ( first > last )
System.out.println( " Number is not found");
}}
|
|
|
26
AJ/Lab Manual -27- Object Oriented Programming
Lab 04
Introduction to classes
A class is an extension to the structure data type. Data member should be grouped in private access
specifier and member functions are normally clustered together in public section. Attempting to access
private members from outside the class will cause syntax error.
Q#1: Write a class named Person with attributes name of the person, his/her age, contact number,
address and gender. Also write following functions to input and display his data.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27
AJ/Lab Manual -28- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
|
Q #2: Modify the above class person so that user can get data for more than 50 students. Note: Use the
Lab Manual BIIT Series
28
AJ/Lab Manual -29- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Q #3: Create a class called employee with the following details as variables within it.
1. Name of the employee
2. Age
3. Designation
4. Salary
Write a JAVA program to create array of objects for the class to access these and print the name, age,
designation and salary.
| Write code here.
|
|
|
29
AJ/Lab Manual -30- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30
AJ/Lab Manual -31- Object Oriented Programming
LAB # 05
An Overview of Objects and Classes (cont..)
Class Declaration
Features of Class:
Classes contain data known as members and member functions. As a unit, the
collection of members and member functions is an object. Therefore, these
units of objects make up a class.
2. Age
3. Year of study
4. Semester
Write a JAVA program to create object for the class to access these and print the Name, age, year,
semester and grade according to their percentage of marks scored.
Lab Manual BIIT Series
<50% -- F grade
LAB # 06
Theory: Constructors and Destructors
Q #1: Create a class for counting the number of objects created and
destroyed within various block using constructor and destructors.
|
AJ/Lab Manual -35- Object Oriented Programming
|
AJ/Lab Manual -36- Object Oriented Programming
|
AJ/Lab Manual -37- Object Oriented Programming
|
AJ/Lab Manual -38- Object Oriented Programming
Lab 07
Constructor overloading
Lab Manual BIIT Series
|
|
|
|
AJ/Lab Manual -40- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -41- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -42- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -45- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -47- Object Oriented Programming
|
|
|
|
|
|
|
|
Q#1. Write a class named student to save name, age, gender, section
and city. Also write following member functions to get and display the
data.
1. input() : to get data from the user
2. display () : to print the details of student
Use the concept of static data member and constructor to set the arid
no of each student automatically, starting from 2018-arid-0001.
|
Lab Manual BIIT Series
|
AJ/Lab Manual -50- Object Oriented Programming
|
AJ/Lab Manual -51- Object Oriented Programming
|
AJ/Lab Manual -52- Object Oriented Programming
LAB # 08
Friend Functions and Friend Classes
|
Lab Manual BIIT Series
|
AJ/Lab Manual -54- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -56- Object Oriented Programming
|
Lab Manual BIIT Series
Q#2. Write a program to accept the student detail such as name and 3
different marks by get_data() method and display the name and
average of marks using display() method. Define a friend class for
calculating the average of marks using the method marrk_avg().
|
AJ/Lab Manual -57- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -58- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -59- Object Oriented Programming
LAB # 9
Theory: Inheritance
The derived class inherits all the features from the base class and can
have additional features of its own.
Exercise
Q.1 Write a class called Person that has the attributes name, address,
date of birth and cell phone number. This class should also include
the following methods:
1. A constructor to initialize all the members a default values
2. input () to get data for attributes
3. display () function to print attributes in the following format:
Name: the name of the person
Address: the address of the person
AJ/Lab Manual -61- Object Oriented Programming
Now, write a class called Employee that inherits from the Person
class. This class should have the additional attributes Department
and DateHired. Be sure to include input() and display() methods.
Write two more classes, Commission and Hourly, that both inherit
from Employee.
Now write a main () program that gets employee data from user, like
Name, Address, Date of Birth, Cell Phone Number, Department Date
Hired, Base Salary (if commission) or Rate of Pay (if hourly)
Commission Rate (if commission) or hoursWorked (if hourly). The
program should print a report with the following information:
|
AJ/Lab Manual -64- Object Oriented Programming
|
AJ/Lab Manual -65- Object Oriented Programming
|
AJ/Lab Manual -66- Object Oriented Programming
|
AJ/Lab Manual -67- Object Oriented Programming
LAB # 10
Inheritance (cont..)
Exercise
|
AJ/Lab Manual -71- Object Oriented Programming
|
AJ/Lab Manual -72- Object Oriented Programming
|
AJ/Lab Manual -73- Object Oriented Programming
|
AJ/Lab Manual -74- Object Oriented Programming
|
Lab Manual BIIT Series
LAB # 11
|
AJ/Lab Manual -76- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -77- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -78- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -79- Object Oriented Programming
|
AJ/Lab Manual -81- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -82- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -83- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -84- Object Oriented Programming
|
Lab Manual BIIT Series
LAB # 12
Theory: File Handling
Q.1 Write a program to input 5 cities from user and store them in a
file cities.txt.
Example Execution
|
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -88- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -89- Object Oriented Programming
|
|
|
Example Output
1- Islamabad
2- Rawalpindi Lab Manual BIIT Series
3- Lahore
4- Karachi
5- Multan
AJ/Lab Manual -90- Object Oriented Programming
|
AJ/Lab Manual -91- Object Oriented Programming
|
AJ/Lab Manual -92- Object Oriented Programming
Q.3 Write a program that inputs the addresses of five students from
user and save them in a file students.txt. After writing the data, the
program should read the data from file as a whole line. Finally the
program should print all addresses.
Successfully Saved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
AJ/Lab Manual -93- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -94- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -95- Object Oriented Programming
|
Lab Manual BIIT Series
LAB # 13
Theory: Character I/O
The data in the file can also be written character by character. Similarly the contents of a file can also be
read character by character. The put () function can be used to write single character in a file and the get
() function can be used to read single character from a file. The syntax is:
Obj.put(char)
Obj.get(char)
Q.1: Write a program that inputs five characters from user and stores
Lab Manual BIIT Series
them in a file.
Sample Output:
Enter a character: a
Enter a character: e
Enter a character: i
Enter a character: o
Enter a character: u
Written…………
previous example.
Sample Output:
LAB # 14
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -100- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -101- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
Q.2.Write a program to input records of three students and store it in students.txt file. Each record
contains Roll number, name and marks.
AJ/Lab Manual -102- Object Oriented Programming
Sample Execution:
|
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -103- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
|
|
|
AJ/Lab Manual -104- Object Oriented Programming
|
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series
|
|
|
|
|
Q.3. Write a program that reads the record stored in students.txt file
and display them.
|
AJ/Lab Manual -105- Object Oriented Programming
|
AJ/Lab Manual -106- Object Oriented Programming
|
AJ/Lab Manual -107- Object Oriented Programming
|
AJ/Lab Manual -108- Object Oriented Programming
Q.4. Write a program that creates a file to store name and email of
user using classes and then display the records as well.
|
Lab Manual BIIT Series
|
AJ/Lab Manual -109- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -110- Object Oriented Programming
|
Lab Manual BIIT Series
LAB # 15
The object can also be written and read from a file. The objects
are normally written and read in binary mode. It helps the user to
use as much memory as is needed to store the content of the object.
|
AJ/Lab Manual -112- Object Oriented Programming
|
AJ/Lab Manual -113- Object Oriented Programming
|
AJ/Lab Manual -114- Object Oriented Programming
|
AJ/Lab Manual -115- Object Oriented Programming
Q.2. Write a program that reads10 objects froma file country.txt using
binary I/O that is written in previous example.
|
Lab Manual BIIT Series
|
AJ/Lab Manual -116- Object Oriented Programming
|
AJ/Lab Manual -117- Object Oriented Programming
|
AJ/Lab Manual -118- Object Oriented Programming
|
Lab Manual BIIT Series
|
AJ/Lab Manual -120- Object Oriented Programming
|
AJ/Lab Manual -121- Object Oriented Programming
Q.1. Write a program to input a string value from user and display
length of string without using built in function.
|
Lab Manual BIIT Series
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Lab Manual BIIT Series