Object Oriented Programming Lab Manual 02

Object Oriented Programming Lab Manual 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. 

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: modelNumberpartNumber 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)

Home Activities:

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.

Comments

Popular posts from this blog

Object Oriented Programming Lab Manual 1