Notes on Java Programming IST JAV 2012 S. Frénot

Similar documents
Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Fundamentals of Object Oriented Programming

BBM 102 Introduction to Programming II Spring Exceptions

C16b: Exception Handling

Object Oriented Programming

Atelier Java - J2. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

What are Exceptions?

National University. Faculty of Computer Since and Technology Object Oriented Programming

Object Oriented Programming Exception Handling

Exceptions. COMP 202 Exceptions. Exceptions. Exceptions. An exception is an object that describes an unusual or erroneous situation

BBM 102 Introduction to Programming II Spring 2017

Chapter 10. Exception Handling. Java Actually: A Comprehensive Primer in Programming

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

CSG 100 Data Structures

Java reflection. alberto ferrari university of parma

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

More on Exception Handling

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Introduction to Java. CSC212 Lecture 6 D. Thiebaut, Fall 2014

CSCI 261 Computer Science II

Polymorphism CSCI 201 Principles of Software Development

COMP-202 Unit 9: Exceptions

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

COMP-202. Exceptions. COMP Exceptions, 2011 Jörg Kienzle and others

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Exceptions and Libraries

CS Programming I: Exceptions

EXCEPTIONS. Java Programming

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Tutorial 8 Date: 15/04/2014

JAVA Programming Language Homework I - OO concept

Question Topic: Single Class. Question 1:

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

C18a: Abstract Class and Method

Here is a hierarchy of classes to deal with Input and Output streams.

G51PGP Programming Paradigms. Lecture OO-4 Aggregation

Remedial Java - Excep0ons 3/09/17. (remedial) Java. Jars. Anastasia Bezerianos 1

Exceptions. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Programming by Delegation

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

CMSC 331 Second Midterm Exam

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

More on Exception Handling

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Lecture 19 Programming Exceptions CSE11 Fall 2013

CH. 2 OBJECT-ORIENTED PROGRAMMING

CSC 1214: Object-Oriented Programming

CS Programming I: Exceptions

CS159. Nathan Sprague

Computer Science is...

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

About this sample exam: Learning Goals

( &% class MyClass { }

Java Errors and Exceptions. Because Murphy s Law never fails

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O

CS 11 java track: lecture 3

File I/O and Exceptions

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Std 12 Lesson-10 Exception Handling in Java ( 1

1. Find the output of following java program. class MainClass { public static void main (String arg[])

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Introduction to Java

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

ECE 462 Fall 2011, Third Exam

Software 1 with Java. Initialization. Initialization. Initialization. Pass by Value. Initialization. Recitation No. 11 (Summary)

9. Java Errors and Exceptions

Inheritance (an intuitive description)

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

Review what constitutes a thread Creating threads general Creating threads Java What happens if synchronization is not used? Assignment.

Programming II (CS300)

Assigned Date: August 27, 2014 Due Date: September 7, 2015, 11:59 PM

Unit 5 - Exception Handling & Multithreaded

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Chapter 14. Exception Handling and Event Handling ISBN

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

COMP-202 Unit 9: Exceptions

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

6.Introducing Classes 9. Exceptions

Class, Variable, Constructor, Object, Method Questions

Transcription:

I) Refactoring The next code contains three classes : public abstract class Triangle{ protected int sidea,sideb,sidec; Notes on Java Programming IST JAV 2012 S. Frénot public Triangle(int a, int b, int c) { this.sidea = a; this.sideb = b; this.sidec = c; public String tostring() { return "I am a Triangle of the 1st side "+sidea + ", the 2nd side " +sideb + " and the 3rd side " +sidec; public abstract String getarea(); public class Equilateral_triangle extends Triangle{ public Equilateral_triangle(int side) { super(side,side,side); public String getarea() { double val = (double)(this.sidea * this.sidea * 0.433); return "-->" + val; public String tostring() { return "I am a Equilateral Triangle!";

public class Test{ public static void main(string [] arg){ Equilateral_triangle et1 = new Equilateral_triangle(3); System.out.println(et1); System.out.println("The area is " + et1.getarea()); Refactor these classes such that the Triangle class calculates the area. The getarea() method should not be abstract anymore protected attributes become private ones II) Exception management An exception is a standard high-level programming paradigm that enable exception behavior management. The mechanism is sufficiently powerful that you can handle standard exception, create your own exception, manage your own exceptions. 1) Runtime exceptions A runtime exception is an exception that may appear at runtime on certain condition that may not be treated by the caller site. The caller site is the class method that uses the method or the operation. For instance, the arithmetic division '/' may generate an exception if the denominator is 0. The indice [ ] operator may generate an exception if the index is out of the bounds. public class TestOne { System.out.println("-->"+arg[0]); System.out.println("-->"+(5/2)); System.out.println("-->"+(5/0)); Running this class without parameter will generate the following exception. ~/Documents/cours/IST/ $ java test/testone Exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 at test.testone.main(testone.java:4) That means at line 4 of the code the [ ] accesses an unreachable index. Running the class with one parameter will generate the other exception.

~/Documents/cours/IST $ java test/testone a -->a -->2 Exception in thread "main" java.lang.arithmeticexception: / by zero at test.testone.main(testone.java:6) When an exception occurs, the line in the running method is immediately interrupted and returns to the calling method from the call stack. One may handle exception in surrounding the suspicious code with a try/catch block of code. The try section contains several method calls. Some of them may raise an exception. If this occurs the line in the try block is interrupted and the control flow jumps to the catch block. A finally block may be added. The finally block is always executed whereas an Exception occurred or not. public class TestOne { try { System.out.println("-->"+arg[0]); System.out.println("-->"+(5/2)); System.out.println("-->"+(5/0)); catch (Exception e) { System.out.println("An Exception occured"); finally { System.out.println("Finally is always executed"); System.out.println("The code continues"); Exercise: write a functionally equivalent code that does not use the length array attribute. public class Test2 { public static void main(string [] arg){ for (int i=0; i<arg.length; i++){ System.out.println("->"+arg[i]); System.out.println("Finished");

2) User Exceptions User exception or compile time exceptions are exceptions declared at the method declaration that forces the caller to handle them. Many standard Java classes declare such exceptions. Exercise: Compile this class file and find two ways to manage the compile time exception. public class TestIO { java.io.fileinputstream dis = new java.io.fileinputstream("/tmp/toto"); /* Do not hesitate to have a look at the java.io.fileinputstream class description at the javadoc Oracle web site */ A method may generate many exceptions. public class Triangle{ private static final int MIN_SIDE_A = 10; private static final int MAX_SIDE_A = 100; private int sidea,sideb,sidec; public Triangle(int a, int b, int c) throws TooSmallException { if ( a < Triangle.MIN_SIDE_A ) { throw new TooSmallException( "The Triangle is too small change the a size smaller than" +Triangle.MIN_SIDE_A); if (a > Triangle.MAX_SIDE_A) { throw new TooBigException( "The Triangle is too Big a " +a+" is greater than "+Triangle.MAX_SIDE_A); this.sidea = a; this.sideb = b; this.sidec = c;

public class TooBigException extends Exception { public TooBigException(String val) { super(val); public class TooSmallException extends Exception { public TooSmallException(String val) { super(val); The caller site must handle all exceptions. Since Exception inherits from an Exception parent class, the caller may handle all exception in a generic way or with a specific catch clause for each type of Exception. public class Test{ try { Equilateral_triangle et1 = new Equilateral_triangle(11); catch (TooBigException e) { System.out.println("The triangle is too big"); catch (TooSmallException e) { System.out.println("The triangle is too Small"); // OR try { Equilateral_triangle et1 = new Equilateral_triangle(11); catch (Exception foo) { System.out.println("The triangle is not good"); /* You may have a look at the Exception class to understand the various methods of the foo variable in the catch method */

For the next course, you must write a code that declares and uses a user declared Exception in your inheritance code.