gdfgdfg
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
599
On Slideshare
599
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Object Oriented Solutions (Objects & Classes) Instructor: Dr. Tabassam Nawaz Class Timings: 3:00p.m. 6:00p.m. WED
  • 2. 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 Object Oriented Solution (Spring 2009) UET Taxila 2 / 46
  • 3. Classes & Objects • OBJECT: – An instance of a class – Physical reality – Occupies space in memory Object Oriented Solution (Spring 2009) UET Taxila 3 / 46
  • 4. 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 } } Object Oriented Solution (Spring 2009) UET Taxila 4 / 46
  • 5. 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. Object Oriented Solution (Spring 2009) UET Taxila 5 / 46
  • 6. A Simple Class • class Box { double width; double height; double depth; } // Box is a new data type. Object Oriented Solution (Spring 2009) UET Taxila 6 / 46
  • 7. 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. Object Oriented Solution (Spring 2009) UET Taxila 7 / 46
  • 8. 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; Object Oriented Solution (Spring 2009) UET Taxila 8 / 46
  • 9. 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); } } Object Oriented Solution (Spring 2009) UET Taxila 9 / 46
  • 10. 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 Object Oriented Solution (Spring 2009) UET Taxila 10 / 46
  • 11. 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; Object Oriented Solution (Spring 2009) UET Taxila 11 / 46
  • 12. 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); } } Object Oriented Solution (Spring 2009) UET Taxila 12 / 46
  • 13. Output • Volume is 3000.0 Volume is 162.0 //output of program • Mybox1’s data is completely separate from the data contained in mybox2. Object Oriented Solution (Spring 2009) UET Taxila 13 / 46
  • 14. 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. Object Oriented Solution (Spring 2009) UET Taxila 14 / 46
  • 15. Declaring Objects • Box mybox = new Box(); // combines the two steps. • Box mybox; // declare reference to object. mybox = new Box(); // allocate a Box object. Object Oriented Solution (Spring 2009) UET Taxila 15 / 46
  • 16. 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. Object Oriented Solution (Spring 2009) UET Taxila 16 / 46
  • 17. Declaring Objects • Box mybox; null mybox width • mybox = new Box(); height mybox depth Box object EFFECT STATEMENT Object Oriented Solution (Spring 2009) UET Taxila 17 / 46
  • 18. 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. Object Oriented Solution (Spring 2009) UET Taxila 18 / 46
  • 19. 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. Object Oriented Solution (Spring 2009) UET Taxila 19 / 46
  • 20. 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. Object Oriented Solution (Spring 2009) UET Taxila 20 / 46
  • 21. Assigning Object Reference Variables • Box b1 = new Box(); Box b2 = b1; width b1 height depth b2 • The assignment of b1 to b2 did not allocate any memory or copy any part of the original object. Object Oriented Solution (Spring 2009) UET Taxila 21 / 46
  • 22. 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. Object Oriented Solution (Spring 2009) UET Taxila 22 / 46
  • 23. Introducing Methods • General form type name (parameter-list){ // body of method } type= returned type (may be void) name = name of method Parameter-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. Object Oriented Solution (Spring 2009) UET Taxila 23 / 46
  • 24. 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. Object Oriented Solution (Spring 2009) UET Taxila 24 / 46
  • 25. 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(); Object Oriented Solution (Spring 2009) UET Taxila 25 / 46
  • 26. 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(); } } Object Oriented Solution (Spring 2009) UET Taxila 26 / 46
  • 27. 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. Object Oriented Solution (Spring 2009) UET Taxila 27 / 46
  • 28. 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; Object Oriented Solution (Spring 2009) UET Taxila 28 / 46
  • 29. 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); } } Object Oriented Solution (Spring 2009) UET Taxila 29 / 46
  • 30. 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()); Object Oriented Solution (Spring 2009) UET Taxila 30 / 46
  • 31. Adding a Method that takes Parameters • int square(){ return 10 * 10; } • int square(int i){ return i * i; } Object Oriented Solution (Spring 2009) UET Taxila 31 / 46
  • 32. 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. Object Oriented Solution (Spring 2009) UET Taxila 32 / 46
  • 33. 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; } } Object Oriented Solution (Spring 2009) UET Taxila 33 / 46
  • 34. 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); } } Object Oriented Solution (Spring 2009) UET Taxila 34 / 46
  • 35. 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. Object Oriented Solution (Spring 2009) UET Taxila 35 / 46
  • 36. 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; } } Object Oriented Solution (Spring 2009) UET Taxila 36 / 46
  • 37. 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); } } Object Oriented Solution (Spring 2009) UET Taxila 37 / 46
  • 38. Output • Constructing Box Constructing Box Volume is 1000.0 Volume is 1000.0 Object Oriented Solution (Spring 2009) UET Taxila 38 / 46
  • 39. 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. Object Oriented Solution (Spring 2009) UET Taxila 39 / 46
  • 40. Parameterized Constructors • Previously, all boxes have the same dimensions. • A way to construct Box objects of various dimensions. • Adding parameters is much more useful. Object Oriented Solution (Spring 2009) UET Taxila 40 / 46
  • 41. 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; } } Object Oriented Solution (Spring 2009) UET Taxila 41 / 46
  • 42. 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); } } Object Oriented Solution (Spring 2009) UET Taxila 42 / 46
  • 43. Output Volume is 3000.0 Volume is 162.0 Object Oriented Solution (Spring 2009) UET Taxila 43 / 46
  • 44. 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. Object Oriented Solution (Spring 2009) UET Taxila 44 / 46
  • 45. 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. Object Oriented Solution (Spring 2009) UET Taxila 45 / 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. Object Oriented Solution (Spring 2009) UET Taxila 46 / 46