+ All Categories

gdfgdfg

Date post: 22-May-2015
Category:
Upload: nawaz-ali
View: 1,963 times
Download: 0 times
Share this document with a friend
Popular Tags:
46
Object Oriented Solutions (Objects & Classes) Instructor: Dr. Tabassam Nawaz Class Timings: 3:00p.m. 6:00p.m. WED
Transcript
Page 1: gdfgdfg

Object Oriented Solutions (Objects & Classes)

Instructor: Dr. Tabassam Nawaz

Class Timings: 3:00p.m. 6:00p.m. WED

Page 2: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

2 / 46

Classes & Objects

• CLASS– At core of JAVA– Logical construct– Defines shape and nature of an object– Forms the basis for OOP– Defines a new data type– Template of an object

Page 3: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

3 / 46

Classes & Objects

• OBJECT:– An instance of a class– Physical reality– Occupies space in memory

Page 4: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

4 / 46

The General Form of a Class• class classname { type instance-variable1; type instance-variable2; //… type instance-variableN; type methodname1(parameter-list) { // body of method } type methodname2(parameter-list) { // body of method } //… type methodnameN(parameter-list) { // body of method }}

Page 5: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

5 / 46

The General Form of a Class

• Data or variable defined within the class are called Instance Variable.

• The code is contained within methods.

• Both are Members of the class.

• Each object contains its own copy of variables.

Page 6: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

6 / 46

A Simple Class

• class Box {

double width;

double height;

double depth;

}// Box is a new data type.

Page 7: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

7 / 46

Creating an Object

• Box mybox = new Box(); // create a Box object called mybox

// mybox will be an instance of Box

// has physical reality

• mybox.width = 100;// dot(.) operator to access instance variables as

well as methods within an object.

Page 8: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

8 / 46

A program that uses the Box class.

/* A program that uses the Box class. Call this file BoxDemo.java */class Box { double width; double height; double depth;}// This class declares an object of type Box.class BoxDemo { public static void main(String args[]) { Box mybox = new Box(); double vol;

Page 9: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

9 / 46

A program that uses the Box class.(contd…)

// assign values to mybox's instance variables mybox.width = 10; mybox.height = 20; mybox.depth = 15;

// compute volume of box vol = mybox.width * mybox.height * mybox.depth;

System.out.println("Volume is " + vol); }}

Page 10: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

10 / 46

Output

• Call the file BoxDemo.java

• Two .class files have been created

• Both classes can be in their own files. • Volume is 3000.0 //output of program

Page 11: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

11 / 46

Two Box Objects// This program declares two Box objects.class Box { double width; double height; double depth;} class BoxDemo2 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15;

Page 12: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

12 / 46

Two Box Objects (contd…)/* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // compute volume of first box vol = mybox1.width * mybox1.height * mybox1.depth; System.out.println("Volume is " + vol); // compute volume of second box vol = mybox2.width * mybox2.height * mybox2.depth; System.out.println("Volume is " + vol); }}

Page 13: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

13 / 46

Output

• Volume is 3000.0Volume is 162.0 //output of program

• Mybox1’s data is completely separate from the data contained in mybox2.

Page 14: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

14 / 46

Declaring Objects

• Two-step process:– Must declare a variable of class type. // This variable does not define an object but can refer to an object.

– Must acquire an actual, physical copy of the object and assign it to that variable.

// using new operator

• new operator dynamically allocates memory for an object and returns a reference to it. // reference (stored in the variable) = address in memory of the object.

Page 15: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

15 / 46

Declaring Objects

• Box mybox = new Box();// combines the two steps.

• Box mybox; // declare reference to object.

mybox = new Box();// allocate a Box object.

Page 16: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

16 / 46

Output

• After declaring reference to mybox, it contains the value null ( does not yet point to an actual object).

• mybox simply holds the memory address of the actual Box object.

Page 17: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

17 / 46

Declaring Objects

• Box mybox;

• mybox = new Box();

STATEMENT

mybox

mybox

Box object

EFFECT

null

width

height

depth

Page 18: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

18 / 46

new Operator

• Dynamically allocates memory for an object.• General form

class-var = new classname();

// class-var is a variable of class type being created. The classname is the name of the class that is being instantiated. () for constructor. If there is no explicit constructor is specified, Java will automatically define a default constructor.

Page 19: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

19 / 46

new Operator

• Java’s simple data types are not implemented as objects but as normal variables in the interest of efficiency.

• Object versions of simple data types are also available.

Page 20: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

20 / 46

new Operator

• new allocates memory for an object during runtime as our program can create as many or as few objects as it needs.

• Memory is finite.

• For insufficient memory, a runtime exception will occur.

Page 21: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

21 / 46

Assigning Object Reference Variables

• Box b1 = new Box();Box b2 = b1;

b1

b2

• The assignment of b1 to b2 did not allocate any memory or copy any part of the original object.

width

height

depth

Page 22: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

22 / 46

Assigning Object Reference Variables

• Any changes made to the object through b2 will affect the object to which b1 is referring.

• A subsequent assignment to b1 will simply unhook b1 from the original object without affecting the object or b2.

• Box b1 = new Box();

Box b2 = b1;

//…

b1 = null; //b2 still points to the original object.

Page 23: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

23 / 46

Introducing Methods

• General formtype name (parameter-list){

// body of method}

type= returned type (may be void)name = name of methodParameter-list = sequence of types and identifier

pairs separated by commas.• return value; // return statement must be

included if the return type is other than void.

Page 24: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

24 / 46

Adding a Method to the Box Class

• Methods define the interface to most classes.

• This allows the class implementer to hide the specific layout of internal data structure behind cleaner method abstractions.

Page 25: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

25 / 46

Program includes a method inside the Box Class

// This program includes a method inside the box class.class Box { double width; double height; double depth; // display volume of a box void volume() { System.out.print("Volume is "); System.out.println(width * height * depth); }} class BoxDemo3 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box();

Page 26: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

26 / 46

Program includes a method inside the Box Class (contd…)

// assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // display volume of first box mybox1.volume(); // display volume of second box mybox2.volume(); }}

Page 27: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

27 / 46

This program includes a method inside the Box Class

• Each time volume() is invoked, it displays the volume for the specified box.

• When an instance variable is accessed by code that is not part of the class in which that instance variable is defined, it must be done through an object, by use of the dot operator. However, when an instance variable is accessed by code that is part of the same class as the instance variable, that variable can be referred to directly. The same thing applies to methods.

Page 28: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

28 / 46

Returning a Value// Now, volume() returns the volume of a box.class Box { double width; double height; double depth; // compute and return volume double volume() { return width * height * depth; }} class BoxDemo4 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol;

Page 29: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

29 / 46

Returning a Value (contd…) // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); }}

Page 30: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

30 / 46

Returning a Value

• The type of data returned by a method must be compatible with the return type specified by the method.

• The variable receiving the value returned by a method must also be compatible with the return type specified.

• Actually no need for vol variable:System.out.println("Volume is " + mybox1.volume());

Page 31: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

31 / 46

Adding a Method that takes Parameters

• int square(){

return 10 * 10;

}

• int square(int i){

return i * i;

}

Page 32: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

32 / 46

Parameter Vs Argument

• A parameter is a variable defined by a method that receives a value when the method is called.

• An argument is a value that is passed to a method when it is invoked.

Page 33: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

33 / 46

program uses a parameterized method

// This program uses a parameterized method.class Box { double width; double height; double depth; // compute and return volume double volume() { return width * height * depth; } // sets dimensions of box void setDim(double w, double h, double d) { width = w; height = h; depth = d; }}

Page 34: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

34 / 46

program uses a parameterized method (contd…)

class BoxDemo5 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // initialize each box mybox1.setDim(10, 20, 15); mybox2.setDim(3, 6, 9); // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); }}

Page 35: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

35 / 46

Constructors

• Tedious to initialize all the variables in a class each time an instance is created.

• Automatic initialization is performed through the use of a constructor.

• A constructor initializes an object immediately upon creation.

• Same name as class name

• No return type not even void.

Page 36: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

36 / 46

Constructors/* Here, Box uses a constructor to initialize the dimensions of a box. */class Box { double width; double height; double depth; // This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; } // compute and return volume double volume() { return width * height * depth; }}

Page 37: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

37 / 46

Constructors (contd…) class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); }}

Page 38: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

38 / 46

Output

• Constructing Box

Constructing Box

Volume is 1000.0

Volume is 1000.0

Page 39: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

39 / 46

Default Constructor

• When we do not explicitly define a constructor for a class, then Java creates a default constructor for the class.

• The default constructor automatically initializes all instance variables to zero.

• Once we define our own constructor, the default constructor is no longer used.

Page 40: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

40 / 46

Parameterized Constructors

• Previously, all boxes have the same dimensions.

• A way to construct Box objects of various dimensions.

• Adding parameters is much more useful.

Page 41: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

41 / 46

Parameterized Constructors/* Here, Box uses a parameterized constructor to initialize the dimensions of a box. */class Box { double width; double height; double depth; // This is the constructor for Box. Box(double w, double h, double d) { width = w; height = h; depth = d; } // compute and return volume double volume() { return width * height * depth; }}

Page 42: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

42 / 46

Parameterized Constructors (contd…)

class BoxDemo7 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(3, 6, 9); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); }}

Page 43: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

43 / 46

Output

Volume is 3000.0

Volume is 162.0

Page 44: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

44 / 46

Lab Session

• Create a class called Date. • Class Date includes three pieces of information as instance

variables--- a month (type int), a day (type int) and a year (type int).

• Your class should have a constructor that initializes the three instance variables and assumes that the values provided are correct.

• Provide a set and a get method for each instance variable. • Provide a method named displayDate that display the month,

day and year separated by forward slahes (/).• Write a test application named DateTest that demonstrates

class Date’s capabilities.

Page 45: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

45 / 46

Assignment # 3 (Q1)• Create a class called Invoice that a hardware store might use to

represent an invoice for an item sold at the store. • An Invoice should include four pieces of information as instance

variables--- a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (type double).

• Your class should have a constructor that initializes the four instance variables.

• Provide a set and a get method for each instance variable. • In addition, provide a method named getInvoiceAmount that

calculates the invoice amount (i.e. multiplies the quantity by the price per item), then returns the amount as double value.

• If the quantity is not positive, it should be set to zero. • If the price per item is not positive, it should be set to 0.0. • Write a test application named InvoiceTest that demonstrates class

Invoice’s capabilities.

Page 46: gdfgdfg

Object Oriented Solution (Spring 2009) UET Taxila

46 / 46

Assignment # 3 (Q2)• Create a class called Employee. • Class Employee includes three pieces of information as instance

variables--- a first name (type String), a last name (type String) and a monthly salary (type double).

• Your class should have a constructor that initializes the three instance variables.

• Provide a set and a get method for each instance variable. • If the monthly salary is not positive, it should be set to 0.0. • Write a test application named EmployeeTest that demonstrates

class Employee’s capabilities.• Create Two Employee’s objects and display each object’s yearly

salary. • Give each Employee a 10% raise and display each Empolyee’s

yearly salary again.