JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.

Slides:



Advertisements
Similar presentations
Yoshi
Advertisements

Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Written by: Dr. JJ Shepherd
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Final Review.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Java Course Outline Kumar Harshit, USW. Course Description Teaches students to program using the Java programming language with the help of the Netbeans.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
CS1101: Programming Methodology Aaron Tan.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Preventing and Correcting Errors
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
ILM Proprietary and Confidential -
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
What/how do we care about a program? Robustness Correctness Efficiency (speed, space) 11/2/20151IT 179  Software Testing  Error Handling  Efficiency.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error= ; double x0.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Error Handling Tonga Institute of Higher Education.
CIS 200 Test 01 Review. Built-In Types Properties  Exposed “Variables” or accessible values of an object  Can have access controlled via scope modifiers.
Written by: Dr. JJ Shepherd
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
COP 3330 Notes 3/7. Today’s Topics Exceptions Abstract Classes.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Java Exceptions a quick review….
Chapter 10 – Exception Handling
MIT AITI 2003 Lecture14 Exceptions
Creating and Modifying Text part 2
Testing and Exceptions
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
Fundamental Error Handling
Java – Inheritance.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Java Basics Exception Handling.
CMSC 202 Exceptions.
Presentation transcript:

JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION

What we can learn today?  Review of OO programming  Exception concept  Class concept  hashtable  Basic event handling  Basic jswing uses

OO programming  OO programming includes encapsulation, inheritance and polymorphism.  We will talk more about polymorphism.  Java call the methods based on the type of the class it is most of the time in compile time  But, with the use of inheritance, we can use the superclass type and call the methods from the class inherited from it.  This is called polymorphism.  Which the calling of method is resolved in run time, but not in compile time (dynamic!)

Example  e.g There are three classes, like employee, hourly empoloyee and contract employee  Which now the last two inherit employee class  Employee class do not have “”payment()” method  But the other two have!  When we type  Employee temp = new ConEmployee()  We cannot call temp.payment()! As JAVA resolve it by type  But if the super class employee also has this payment() method, then we can call temp.payment()!^^

Exception Handling  Exception is not something program errors.  It maybe a matter of runtime unpredictable hardware problems, or usage or program.  In JAVA, exception are finely classified into  We need to handle  And we don't need to handle type  We would demonstrate in two examples

The type we must handle  Public static void main(String args[]){  Try{  Thread.sleep(100)  }  Catch (Exception e)  {System.out.println(“caught”);  }  In the above example, we must do something for the Thread.sleep(100) method as it throws the exception and it is the type we must handle. First type!.

Something we can ignore  Public static void main(String args[]){  Int a[] = {1,2,3,4,5};  A[6] = 9;  }  It compiles well!  But it throws ArrayIndexOutOfBoundsException in runtime.  BUT WE CAN STILL NOT CATCH IT!

Two ways we can deal with exception  First, code it to deal with it:  Try{  Statement may throw exception  }  Catch (Exception e){}  Finally{}  The catch block is used to catch certain type exception and code the problem. (As Exception is the SUPERCLASS of all exception, so the statements above catch all exception.)  The finally block must execute before any return and after the try and catch blocks.

 Even you type return in try block, the statements in finally block is executed!  After the try block, finally or catch blocks must be executed!

Second method  We don't handle the exception, but we throw it away.  Like  void a() throws Exception{  Thread.sleep(100);  }  Public static void main(String args[])  {Test temp = new Test(); try {  temp.a()}catch (Exception){//we catch it}  }  We catch it in other block as the exception is threw in a() method.

Class concept  All the classes in JAVA are inherited from the Object class.  And thus all classes have all the methods in Object (if they are stating as right modifier in Object!)  Eight types are not classes  Stated in last lesson  But they can be class also  Integer temp = new Integer(6);  The storage are not the same, will not explained here^^

Hashtable  Each element is a pair of  We just the key to find the element we want!  In java, hashtable is given in java.utils.Hashtable  We will use two methods in our examples  get(key)  And put(key, object)  Example shown

Basic event handling  IN java Jswing, we can handle the action, like mouse action, window closing......with the Jswing elements to write interactive program  See the demonstration

WE would now teach the Jswing  Game.java is demonstrated.

The end