Lab 2
Lab 2
Statement Purpose:
Objective of this lab is to understand the Object-Oriented paradigm.
Activity Outcomes:
The student will be able to understand the Object-oriented paradigm.
The student will be able to understand difference between class and object.
Instructor Note:
The students should brainstorm about the scenarios given in activities; in order to model them in terms of
Objects.
1) Stage J (Journey)
Introduction
The world around us is made up of objects, such as people, automobiles, buildings, streets, and so forth. Each
of these objects has the ability to perform certain actions, and each of these actions has some effect on some
of the other objects in the world. OOP is a programming methodology that views a program as similarly
consisting of objects that interact with each other by means of actions. Object-oriented programming has its
own specialized terminology. The objects are called, appropriately enough, objects. The actions that an object
can take are called methods. Objects of the same kind are said to have the same type or, more often, are said
to be in the same class.
For example, in an airport simulation program, all the simulated airplanes might belong to the same class,
probably called the Airplane class. All objects within a class have the same methods. Thus, in a simulation
program, all airplanes have the same methods (or possible actions), such as taking off, flying to a specific
location, landing, and so forth. However, all simulated airplanes are not identical. They can have different
characteristics, which are indicated in the program by associating different data (that is, some different
information) with each particular airplane object. For example, the data associated with an airplane object
might be two numbers for its speed and altitude.
Things that are called procedures, methods, functions, or subprograms in other languages are all called methods
in Java. In Java, all methods (and for that matter, any programming constructs whatsoever) are part of a class.
2) Stage a1 (apply)
Lab Activities:
Activity 1:
Consider the concept of a CourseResult. The CourseResult should have data members like the student name,
course name and grade obtained in that course.
This concept can be represented in a class as follows:
Solution:
Public class CourseResult
{
Public String studentname;
Public String coursename;
Public String grade;
public void display() }
{ }
System.out.println("Student Name is: " + studentname + "Course Name is: " +
coursename + "Grade is: " + grade);
Public class CourseResultRun
{
public static void main(String[]args)
{
CourseResult c1=new CourseResult ();
c1.studentName= "Ali";
c1.courseName= "OOP";
c1.grade= "A";
c1.display();
CourseResult c2=new CourseResult ();
c2.studentName= "Saba";
c2.courseName= "ICP";
c2.grade= "A+";
c2.display();
}
}
Note that both objects; c1 and c2 have three data members, but each object has different values for their
data members.
Activity 2:
The example below represents a Date class. As date is composed of three attributes, namely month, year and
day; so, the class contains three Data Members. Now every date object will have these three attributes, but
each object can have different values for these three
Solution:
public class Date
{
public String month;
public int day;
public int year; //a four digit number.
public void displayDate()
{
Activity 1:
A Student is an object in a university management System. Analyze the concept and identify the data
members that a Student class should have. Also analyze the behavior of student in a university management
System and identify the methods that should be included in Student class.
Activity 2:
Time is an intangible concept. Analyze the concept and identify the data members and methods that should
be included in Time class.
.
4) Stage a2 (assess)
Assignment 1:
Car is an object that helps us in transportation. Analyze the concept and identify the data members and
methods that should be included in Car class.
Assignment 2:
Rectangle is an object that represents a specific shape. Analyze the concept and identify the data members
and methods that should be included in Rectangle class.
Lab Tasks:
1. Create class circle class with radius as data member. Create setParameter, display and a
calculateCircumference and calculateArea methods.
2. Create a class Rectangle with length and width as data members. Create setParameter, display,
calculateArea, checkSquare methods.
3. Create a class Account class with balance, yearofOpening and CNIC as data member. Create
setParameter, display, withdraw, deposit and checkvalidCNIC methods.
4. Write a class Marks with three data members to store three marks. Create setParameter, display,
sum (to calculate and return the sum), percentage, checkFailure methods.
5. Create a class Point class with x and y as data members. Create setParameter, display and move
and checkOrigin methods.
6. Create a Book class with author and chapterNames [5] as data members. Create setParameter,
display, checkIfAuthorNameStartsWithA and searchChapter methods.
7. Create a Student Class with name, Gpa, subjects [5] and email as data members. Create
setParameter, display, searchSubject, checkProbStatus and validateEmail methods.