1 / 154

M874 Weekend School

M874 Weekend School. An Introduction to Java Programming. Presentation 1 , Objects Presentation 2 , Inheritance Presentation 3 , Exceptions Presentation 4 , Abstraction Presentation 5 , Threads. Presentation 6 , HCI Presentation 7 , Packages Presentation 8 , Applets

brook
Download Presentation

M874 Weekend School

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. M874 Weekend School An Introduction to Java Programming M874 Java

  2. Presentation 1, Objects Presentation 2, Inheritance Presentation 3, Exceptions Presentation 4, Abstraction Presentation 5, Threads Presentation 6, HCI Presentation 7, Packages Presentation 8, Applets Presentation 9, Network facilities Contents M874 Java

  3. Presentation 1Aims • Describe what an object is • Outline how objects communicate • Introduce the idea of a class • Provide explanations of the vocabulary terms method, argument, class and object M874 Java

  4. Objects • Objects occur in everyday life. • Typical objects in an air-traffic control system are planes, radars and slots. • An object is associated with data (state) • Objects can send messages to each other. • One of the first activities in a project that uses Java is identifying objects. M874 Java

  5. Objects Objects communicate via messages Plane Radar I have moved my position M874 Java

  6. Objects and Messages The general form of a message destinationObject.selector(arguments) Object message is sent to Name of message Any arguments M874 Java

  7. Examples of Messages Note that messages can have no arguments printer.start() queued(newProcess) canvas.drawline(x1,x2,y1,y2) M874 Java

  8. Java Errors 1 • Forgetting that there is a destination object. • Missing the arguments. • If the message has no arguments then omitting the brackets. M874 Java

  9. Classes • The class is the key idea in an OO language. • Classes are like biscuit cutters: they can be used to create objects. • Classes define the data that makes up an object. • They also define the messages sent to an object and the processing that occurs. M874 Java

  10. Class Skeleton Note capitalisation State can follow methods class Example{ definition of state … definition of methods } Brackets match M874 Java

  11. An Example of a Class class Simple{ int x; add(int k){ x+=k; } set(int k){ x = k } } Class with a single piece of data (instance variable) and two chunks of code known as methods M874 Java

  12. The main Method • This is a method inserted into the only public class in a file. • It carries out execution of a program and acts like a driver program. • It must be placed in the class:it is a method. • It is of the form public static void main. M874 Java

  13. What Happens Java finds that e is an Example, looks for set and then copies 8 into k which is then copied into x Example e; … e.set(8); … e.add(12); Java finds that e is an Example, looks for add and then copies 12 into k which is then added to x. M874 Java

  14. Constructors • Constructors are special methods which are used to create space for objects. • They have a special syntax. • They are used in conjunction with the new facility. • They can be associated with any number of arguments ranging from zero. M874 Java

  15. Example of the Use of a Constructor Example e = new Example(); … Example f = new Example(23); What happens when the new facility is used? M874 Java

  16. How it Works • If you declare no constructors then the Java system will initialise all your scalar instance variables to some default, for example an int will be 0.It will also initialise all object instance variables to null • This is poor programming so you need to define all your constructors. • Roughly same syntax as methods. M874 Java

  17. Examples of Constructors Example(){ x = 0; } Example(int startValue){ x = startValue; } First constructor is equivalent to default Second constructor initialises instance variable x to argument startValue M874 Java

  18. What Happens Example(12); Java system looks for a class Example, then looks for a single argument constructor, if it finds it then it carries out the code defined by the constructor M874 Java

  19. Java Errors 2 • Wrong number and type of arguments in method call. • Omitting a constructor. • Misspelling or lower case/upper case problems. M874 Java

  20. The Use of this • The keyword this has two functions in Java. • It is used within constructors to enable maintainable code to be written. • It is used in methods to refer to the current instance variables. M874 Java

  21. this in Constructors class Example{ int u; Example(int val){ u = val; } Example(){ this(0); } } Use the constructor defined in this class which has a single int argument M874 Java

  22. this in Methods class Example{ int k; setValue(int k){ this.k = k; } … } Set the instance variable in this class to the value of the argument of the method M874 Java

  23. public, private etc. • There are a number of visibility modifiers in Java. • You will mainly come across public and private. • public means everything is allowed access. • private means only entities within a class are allowed access. M874 Java

  24. Compiling Java Classes • A file can contain any number of Java classes. However, only one can be public • If you have a number of public classes then place them in separate files • Good design dictates that instance variables are private and methods are normally public. M874 Java

  25. Java Errors 3 • Forgetting that one class in a file must be public. • Forgetting to name the source file the same as the class name. M874 Java

  26. Methods • Just like subroutines. • Pass objects as references and scalar types as values. • Prefaced with visibility modifiers. • A number of return types including scalar, classes and void. M874 Java

  27. Example public int exCalc(int a, String b){ return someValue; } This is the method exCalc it returns an int value and has two arguments an int and a String M874 Java

  28. Presentation 2Aims • Describe the rationale behind OO languages. • Show how an OO system consist of class hierarchies. • Describe the main reuse facility within Java: inheritance. • Describe the use of class (static methods). M874 Java

  29. The Notion of Hierarchy Plane Passenger Plane CargoPlane FighterPlane B747 B757 AirbA320 M874 Java

  30. Hierarchy 1 • Classes in a computer system can be arranged in a hierarchy. • As you go down the hierarchy the classes become more and more specialised: Plane, PassengerPlane, B757. • As you go down the hierarchy more and more facilities are added. M874 Java

  31. Hierarchy 2 • For example, in a Personnel system you might have a section of hierarchy: Employee - SalariedEmployee - ComissionPaidSalariedEmployee. • In this example more and more methods and instance variables are added as you proceed down the hierarchy. The way that this is achieved is via inheritance. M874 Java

  32. Inheritance 1 • Is the most important concept in OO languages. • Implemented mainly by the extends facility. • Various rules are used to determine what methods and instance variables are inherited. M874 Java

  33. Inheritance 2 • If a class A inherits from a class B, then the methods and instance variables of A become associated with objects defined by A. • For example, if PassengerPlane inherits from Plane then an instance variable describing the size of the plane would be inherited from Plane. M874 Java

  34. Inheritance in Java class A{ instance variables of A a1, a2, a3 methods of A m1, m2, m3 } class B extends A{ instance variables of B b1, b2 methods of B n1, n2 } Any object described by B will have five instance variables and can respond to the five messages m1, m2, m3, n1, n2 M874 Java

  35. Reuse Inheritance is the way that reuse is implemented in any OO language. You will need to be completely happy with this notion to use such a language. It will crop up time and time again, for example, to create a window in Java you will need to inherit from a built-in class Frame. M874 Java

  36. The two Uses of Inheritance • There are two uses of inheritance in Java. • The first use is where functionality is added to an existing class, for example a WeeklyPaidEmployee class might inherit from an Employee class. • The second use is where functionality is changed. This is an example of overriding. M874 Java

  37. Overriding class A{ instance variables methods m1, m2, m3 } class B extends A{ instance variables methods m2,m4,m5 } When an m2 message is sent to a B object the m2 method within c is executed M874 Java

  38. Superclass and Subclass Superclass extends super Subclass this M874 Java

  39. Conventions for Naming • this refers to this class. • super refers to the superclass. • both this and super allow you to refer to the instance variables and methods in a class and its immediate super class. M874 Java

  40. Using super in Constructors • You may remember that we can use the keyword this within constructors. • In a similar fashion we can use super in constructors. • When it is used it calls on the constructor in the superclass. M874 Java

  41. Example of super class A{ int instVar; A(int b){ instVar = b; } … } class B extends A{ int anotherVar; B(int c, int d){ super(d); anotherVar = c; } … } initialises the instance variable inherited from A with the value d M874 Java

  42. Static Methods These are methods whose messages are not sent to objects but to classes. A static method is prefaced with the keyword static. You can also have static variables. These are class globals which are not replicated in each object defined by a class. In Java static methods are usually used to implement general purpose functions such as sin, cos and string conversion functions. M874 Java

  43. Example of Static Methods The static method valueOf found in java.lang public static String valueOf(int v) Places in val the string value of the integer 23 String val = String.valueOf(23); Note the class name M874 Java

  44. Java Errors 3 • Forgetting the class name when calling a static method. • Forgetting to use super within a constructor. • Initialising instance variables in their definitions rather than in the constructors. M874 Java

  45. Presentation 3Aims • To describe how Java handles errors. • To show how exception objects can be used for monitoring errors. • To describe the use of the throws and throw keyword. • To describe the use of the try-catch facility. M874 Java

  46. Errors Errors can occur any time within a Java program • A program runs out of memory. • An invalid URL is accessed. • A user types in a floating point number • when an integer is expected. • A program which expects a file containing data • is connected up to an empty file. • An array overflows. M874 Java

  47. Exceptions • Are a way of handling errors which cannot be anticipated such as mistyped data being provided by the user. • Based on the idea of an exception object. • The keyword throw creates an exception object which indicates that a problem has occurred. M874 Java

  48. Creating an Exception If something has gone wrong then create a new exception object and throw it. IllegalArgument- Exception is a build in class in Java. if(error condition){ throw new IllegalArgumentException(); } M874 Java

  49. Handling Exceptions • When an exception is created control is passed to error-handling code. • This error handling code is introduced by means of the try-catch facility. • A number of exceptions can be handled in the same body of code. M874 Java

  50. Using try-catch try{ // Code which potentially causes two exceptions // IllegalArgumentException and NumberFormat // Exception } catch(IllegalArgumentException i) {Code for processing the IllegalArgumentException} catch(NumberFormatException n) {Code for processing the NumberFormatException} M874 Java

More Related