unit 6: object-oriented programming: objects and classes jin sa

Download Unit 6: Object-oriented Programming: Objects and Classes Jin Sa

Post on 22-Dec-2015




2 download

Embed Size (px)


  • Slide 1
  • Unit 6: Object-oriented Programming: Objects and Classes Jin Sa
  • Slide 2
  • Objectives of this unit To learn how to declare a class and how to create an object of a class. To learn how to send messages to other objects. To understand the difference between instance and static variables and methods. To manipulate objects in arrays. To understand the use of various visibility modifiers. To know how to use the exception mechanism.
  • Slide 3
  • Defining and using classes and objects An object has a unique identity, state, and behaviours. The state of an object consists of a set of data fields with their current values. These data fields are called attributes. The behaviour of an object is defined by a set of methods. Classes are constructs that define objects of the same type.
  • Slide 4
  • Defining classes see SimpleCircle from unit6 section B public class Circle { double radius = 1.0; Circle() { } Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double findArea() { return radius * radius * 3.14159; } Radius is the data field of the Circle class. The data fields are often referred to as attributes. The class also defines three methods: Circle(), Circle(double newRadius) and findArea(). These three methods will determine the behaviour of any circle created from this template.
  • Slide 5
  • Methods The format of a method is type_of_value_returned name_of _method(list of parameters) type_of_value_returned can be void if the method does not return any value void setRadius(double newR){ radius=newR; } List of parameters can be empty if the method does not take any parameter. findArea().
  • Slide 6
  • Constructors Circle() and Circle(doubleRadius) are called the constructors. A constructor of a class is invoked when an object of that class is created. It plays the role of initializing objects. A class may have more than one constructor, more than one way to create an instance. Constructors must have the same name as the class itself. A constructor with no parameters is referred to as a no-arg constructor, e.g. Circle().
  • Slide 7
  • Creating objects new (see TestSimpleCircle) To create an object, we use the new operator. new Circle(); new Circle(2.5);
  • Slide 8
  • Object references and accessing objects via object references To reference an object, we can assign an object to an object reference variable. For example: Circle myCircle = new Circle(); myCircle is an object reference. It is now pointing to an instance of Circle. An objects data and method can be accessed via the object reference, e.g. myCircle.radius myCircle.findArea()
  • Slide 9
  • More about object references c=new Circle();
  • Slide 10
  • More about object references (2)
  • Slide 11
  • Student Activity Complete Unit 6 student activity 6.1
  • Slide 12
  • Visibility modifiers public: The data or method is visible to any class in any package. private: The data or methods can be accessed only by the declaring class. By default: the data or method can be accessed by any class in the same package.
  • Slide 13
  • Visibility modifiers (2)
  • Slide 14
  • get and set methods Following the principle that data should be encapsulated, we should declare data field private. However, private attributes are not accessible from outside its object. To allow clients, i.e. other objects, to access or modify a private data field in a controlled way, we can provide public get method and set method to manipulate the private data field, e.g. public double getRadius(){ return radius; } public void setRadius(double newR) { if (newRj) return i; else return j; } int maxNum(int i, int j, ">
  • Example of method overloading public class MethodOverloading { int maxNum(int i, int j){ if (i>j) return i; else return j; } int maxNum(int i, int j, int k){ int tmp=maxNum(i,j); return maxNum(tmp,k); } MethodOverloading test=new MethodOverloading(); int I = test.maxNum(5,3); int j = test.maxNum(5,3,6);
  • Slide 30
  • Exception handling (may skip) Examples of causes of exceptions Different ways of handling exceptions Not handling the exception at all Handling where it occurs using try-catch Handling exception at a different point
  • Slide 31
  • Examples of causes exceptions Typical examples include: division by zero, array index out of bounds, file does not exist, null pointer and I/O exception. In Java, these, and many other such, unusual situations are defined as exception classes. The programmers can also define their own exception classes to capture new exceptional situations.
  • Slide 32
  • Not handle the exceptions do not handle it, the program terminates with an exception message telling us what exception occurred and where it occurred.
  • Slide 33
  • Example (Student activity 6.6) class SomeExceptions { public int divide(int m){ Scanner in=new Scanner(System.in); int n=in.nextInt(); // get the value of the divisor return m/n; } public class IllustrateException { public static void main(String [] args) { SomeExceptions myProg = new SomeExceptions(); System.out.println(myProg.divide(100)); }
  • Slide 34
  • Handling exception where it occurs: the try-catch statement The try-catch statement identifies a block of statements that may produce one or more exceptions. One or more catch clauses should follow a try block. The catch clauses define how to handle each type of exception that may occur in the try block. Each catch clause is called an exception handler.
  • Slide 35
  • Revised example (student activity 6.7) class SomeException2 { int maxInt=999999999; public int divide(int m){ Scanner in=new Scanner(System.in); int n=in.nextInt(); try{ return m/n; } catch (ArithmeticException e) { System.out.println("An exception has occurred."); System.out.println("A big number is returned as a default."); return maxInt; }
  • Slide 36
  • Handling exception at a different point If an exception is not caught and handled where it occurs, control is immediately returned to the place that invoked the method, e.g. if the divide method does not provide any mechanism to catch and handle the arithmetic exception, the control will be returned to the main program where it calls myProg.divide(100). We can design programs so that the exception is caught and handled at this outter layer using try- catch statement
  • Slide 37
  • Revise example public class IllustrateException3 { public static void main(String [] args) { SomeExceptions myProg = new SomeExceptions(); try { System.out.println(myProg.divide(100)); } catch (ArithmeticException e){ System.out.println("exception happened, caught at outer level."); }
  • Slide 38
  • Student activity Complete unit 6 student activities 6.6 and 6.7 (exception) Multiple choice questions
  • Slide 39
  • An integrate case study Go through the case study Go through the design (note with voice over) Student activity Go through outline and solution
  • Slide 40
  • Summary how to define classes; how to create objects; what is an object reference; how to use visibility modifiers to achieve the right level of encapsulation; how to pass objects as parameters to a method and how to return an object as a result; array of objects; static method and variables; exception handling


View more >