Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 30 Revision Lecture 1

Similar presentations


Presentation on theme: "Lecture 30 Revision Lecture 1"— Presentation transcript:

1 Lecture 30 Revision Lecture 1

2 Summary of Previous Lecture
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Interrupting Threads InterruptedException Handling Exceptions Waiting for thread execution awaitTermination() Thread.join() Returning a result from asynchronous threads Callable interface Retrieving the result Future Interface FutureTask implementation

3 Exporting your Assignment
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary The purpose of using Eclipse is to make it standard for markers to import, view your source code and run your simulation. If you have used a different IDE, please create an Eclipse project and copy your files in to setup a project similar to the one above. You can then follow our steps to export

4 Exam Format/Topics Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary 51 Multi-choice Questions. Total marks 60. Questions will be worth either 1 mark or 1.5 marks. Topics : Angela Access Modifiers (public, private, default, package) Overriding/Overloading methods Constructors Class inheritance Nested, Anonymous, Abstract Classes Fields (Instance/Static/Member Class) static keyword final keyword Topics : Craig/Elliot Using Swing Components JFrame, JPanel, JComponent Events/Event Listeners Adapters

5 Exam Format/Topics GUI Thread/Event Dispatch Thread (EDT)
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary GUI Thread/Event Dispatch Thread (EDT) SwingUtility/SwingWorker classes Benefits of Model-View-Controller (MVC) pattern Regex! File IO Classes and methods Binary (De)Serialization of Objects Java Threads (Under the hood) States Runnable() Interface ExecutorService Class / Methods Java Monitor/Monitor Locks Daemon Threads Producer/Consumer Relationship ArrayBlockingQueue Future/Callable interfaces

6 Object Oriented Programming
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Class : A blueprint from which objects are created Explains state (fields) and behavior (methods) Data encapsulation Often models real-world objects Object : Instance of a Class that is created at run-time Has its own state separate from other instances Declared with a type Initialized with a constructor Type Name Constructor Method

7 Object Oriented Programming
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Inheritance : Mechanism for organizing and structuring software Classes can inherit fields and methods from superclass Extends Implements Is-A relationship e.g. Bicycle is a Vehicle Interface : Contract between a class and its functionality If a class implements an interface, it must provide functionality for all the defined methods Ensures all classes implementing the interface will provide common functionality and can be used similarly.

8 Java Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Java : Source code is written in text files ending with the .java extension. These source files are compiled into .class files. Java Virtual Machine (JVM) : .class files contain bytecode that are interpreted by the JVM, which executes the code on the machine. Creates a layer between processor and executing code. Errors: Syntax Errors : Errors are found at compile-time where the compiler will find an error preventing the compilation of class files Runtime Errors : Program was compiled successfully, however an error was thrown during execution Logic Errors : No explicit errors are thrown but has unexpected output or results.

9 Java Static typed language
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Static typed language Variables must be declared before that can be used. Bicycle bike; Variables can then be initialized to receive a value of same type bike = new Bicycle(); final keyword Indicates that a variable’s value cannot be changed once initialized When a list is declared as final, the objects within the list can still change their value. Objects can also be added and removed from the list. However, a new List instance cannot be assigned to the final variable

10 Expressions - Operators
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary

11 Conditionals If, else, else if statements Switch statements For Loops
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary If, else, else if statements Switch statements For Loops Enchanced For Loops While Loops Do While Loops Ternary Operations

12 Methods Static Methods e.g. result = Math.ceil();
Calling local methods e.g. returnVar = methodName(parameters) Invoking methods on objects e.g. object.methodName(parameters); Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary The method definition will contain modifiers (e.g. visibility, static, final etc.), method name, return type and parameters (with types!) Method Call Stack

13 Methods Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Overloading : Declaring methods of the same name with different amounts of parameters. Usually used to perform similar tasks with different types/amounts of arguments. Compiler recognizes this by the method signature, e.g. name/parameters – not return type

14 Arrays Arrays Declaring an array Initializing an array – Fixed size
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Arrays Declaring an array Initializing an array – Fixed size Adding values or objects to an array e.g. courseMarks = new int[]{26,73,55,97}; ArrayIndexOutOfBounds exception Using the logical operators defined earlier Multi-dimensional arrays – representing grids. Cloning Arrays ArrayLists Stores Objects, not primitive values Dynamically sizing Generics – ArrayList<T>

15 Classes Defining a class : Class declaration and class body
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Defining a class : Class declaration and class body Field and Method declarations/logic Instance variables : non-static fields Class variables : static fields Visibility Modifiers Determine the visibility of a class/variable/method/constructor public : Can be accessed from anywhere – no safety protected : Accessed from within the package No modifier/default : Accessed within the class/package private : Accessed only from within the class

16 Inheritance Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Is-A relationship represented by arrows. Subclass inherits non-private methods of the superclass to use. The subclass also inherits the non-private fields of the superclass. Overriding : Change the meaning (override) of the method declared in the superclass. Could either be a completely new implementation or add more functionality after call the super() method. Must override the same method signature. Cannot override final method.

17 Inheritance Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary The subclass can call the method of the superclass even if it doesn’t implement the method itself. We will see “SuperMethod” be printed.

18 Inheritance Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary If the subclass declares the superMethod() signature in its own class body, then it will override the functionality. Now when we create our subclass instance and call superMethod(), we will see the subclass implementation of that method be executed.

19 Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Polymorphism Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour. The program does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding). This means the compiler doesn’t necessarily know the instance type of the object that the method is being called on. The instance can be determined at run time and the same method call will have different behaviour depending on the instance type.

20 Polymorphism Binding Interrupting Threads Waiting for Threads
Callable Interface Future Interface Summary Polymorphism Binding Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary

21 Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Polymorphism Binding Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Although the compiler sees each ‘cs’ variable as type CustomSuper (and hence we can only call CustomSuper methods), the instances we created were of type CustomSub and CustomOtherSub. We therefore see the overriden versions of superMethod(). Prints : “SubMethod”, “OtherSubMethod”

22 Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Abstract Classes Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Similar to Interfaces, Abstract classes cannot be instantiated and are instead used to define part of an implementation leaving the subclasses to implement the logic. Declared with the keyword abstract. Will often contain abstract methods, which are a method signature with no code block defining the implementation of the method. These must be implemented by all subclasses extending the abstract class. An Abstract class can have state. It may declare and initialize instance variables. An interface cannot do this.

23 Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Anonymous Classes Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary An Anonymous class is a local class that is not given a name but is instead declared implicitly by creating a variable of it. Because of this we can only use an anonymous class once, when is is instantiated. These should only be used when you need to use the class only once. We are combining the instantiation and defining of a class in one statement. We have seen many examples of these when developing GUIs. They are often used when creating AWT or Swing based programs. public void sayBye() { HelloWorld i = new HelloWorld() { String name = "world"; public void greet() { System.out.println("Bye " + name); } }; i.greet();

24 Exceptions Java uses Exceptions for error handling.
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Exceptions Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Java uses Exceptions for error handling. These must be caught using a try-catch statement, or thrown to the calling class to handle the exception (Exception Propagation). If the exception is never handled by the developer, it will be thrown to the default exception handler which will terminate the thread and display the error message. try { value = 0; int answer = 100 / value; System.out.println("This will not be printed"); //1 } catch (Exception e) { System.out.println("Error"); //2 value = -1; } System.out.println("Program can continue to run..."); //3

25 Exceptions Common Exceptions we dealt with : NullPointerException
Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Exceptions Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Common Exceptions we dealt with : NullPointerException When using objects to execute tasks ArrayIndexOutOfBoundsException When accessing arrays ClassCastException When casting the class of an instance InterruptedException When dealing with Threads IOException Dealing with Input/Output (like files)

26


Download ppt "Lecture 30 Revision Lecture 1"

Similar presentations


Ads by Google