0% found this document useful (0 votes)
96 views6 pages

Lab 2

The document discusses object-oriented programming concepts like classes, objects, methods, and data members. It provides examples of different classes like CourseResult, Date, CarPart that have data members and methods. It also lists some activities and assignments for students to practice creating classes for concepts like Student, Time, Car, Rectangle. Finally, it provides tasks to create classes for Circle, Rectangle, Account, Marks, Point, Book, and Student with specified data members and methods.

Uploaded by

GHULAM MOHIUDDIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views6 pages

Lab 2

The document discusses object-oriented programming concepts like classes, objects, methods, and data members. It provides examples of different classes like CourseResult, Date, CarPart that have data members and methods. It also lists some activities and assignments for students to practice creating classes for concepts like Student, Time, Car, Rectangle. Finally, it provides tasks to create classes for Circle, Rectangle, Account, Marks, Point, Book, and Student with specified data members and methods.

Uploaded by

GHULAM MOHIUDDIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB # 02

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()
{

System.out.println(month + " -" + day + "-" +year);


}
}
public class DateDemo
{
public static void main(String[] args)
{
Date date1, date2;
date1 = new Date();
date1.month = "December";
date1.day = 31;
date1.year = 2012;
System.out.println("date1:");
date1.display();
date2 = new Date();
date2.month = "July";
date2.day = 4;
date2.year = 1776;
System.out.println("date2:");
date2.display();
}
}
Activity 3:
Consider the concept of a Car Part. After analyzing this concept, we may consider that it can be
described by three data members: modelNumber, partNumber and cost.
The methods should facilitate the user to assign values to these data members and show the values for
each object.
This concept can be represented in a class as follows:
Solution:
importjavax.swing.JOptionPane;
Public class CarPart
{
private String modelNumber;
private String partNumber;
private String cost;
public void setparameter(String x, String y, String z)
{
modelNumber=x;
partNumber=y;
cost=z;
}
public static void display()
{
System.out.println("Model Number: " +modelNumber + "Part Number: " +partNumber + "Cost: "
+ cost);
}
}
Public class CarPartRunner
{
public static void main(String[]args)
{
CarPart car1=new CarPart ();
String x=JOptionPane.showInputDialog("What is Model Number?" );
String y=JOptionPane.showInputDialog("What is Part Number?" );
String z=JOptionPane.showInputDialog("What is Cost?" );
car1.setparameter(x,y,z);
car1.display();
}
}
3) Stage v (verify)

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy