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

215 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

<ul><li> Slide 1 </li> <li> Unit 6: Object-oriented Programming: Objects and Classes Jin Sa </li> <li> Slide 2 </li> <li> 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. </li> <li> Slide 3 </li> <li> 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. </li> <li> Slide 4 </li> <li> 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. </li> <li> Slide 5 </li> <li> 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(). </li> <li> Slide 6 </li> <li> 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(). </li> <li> Slide 7 </li> <li> Creating objects new (see TestSimpleCircle) To create an object, we use the new operator. new Circle(); new Circle(2.5); </li> <li> Slide 8 </li> <li> 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() </li> <li> Slide 9 </li> <li> More about object references c=new Circle(); </li> <li> Slide 10 </li> <li> More about object references (2) </li> <li> Slide 11 </li> <li> Student Activity Complete Unit 6 student activity 6.1 </li> <li> Slide 12 </li> <li> 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. </li> <li> Slide 13 </li> <li> Visibility modifiers (2) </li> <li> Slide 14 </li> <li> 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, "&gt; </li> <li> Example of method overloading public class MethodOverloading { int maxNum(int i, int j){ if (i&gt;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); </li> <li> Slide 30 </li> <li> 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 </li> <li> Slide 31 </li> <li> 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. </li> <li> Slide 32 </li> <li> Not handle the exceptions do not handle it, the program terminates with an exception message telling us what exception occurred and where it occurred. </li> <li> Slide 33 </li> <li> 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)); } </li> <li> Slide 34 </li> <li> 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. </li> <li> Slide 35 </li> <li> 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; } </li> <li> Slide 36 </li> <li> 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 </li> <li> Slide 37 </li> <li> 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."); } </li> <li> Slide 38 </li> <li> Student activity Complete unit 6 student activities 6.6 and 6.7 (exception) Multiple choice questions </li> <li> Slide 39 </li> <li> An integrate case study Go through the case study Go through the design (note with voice over) Student activity Go through outline and solution </li> <li> Slide 40 </li> <li> 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 </li> </ul>

Recommended

View more >