CSE 11 February 6, 2003. © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not.

Slides:



Advertisements
Similar presentations
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Advertisements

Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
Written by: Dr. JJ Shepherd
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
HST 952 Computing for Biomedical Scientists Lecture 7.
1 Week 11 l Basic Exception Handling »the mechanics of exceptions l Defining and Using Exceptions »some "simple" cases l Reality Check »guidelines for.
Exception Handling Chapter 8. Outline Basic Exception Handling Defining Exception Classes Using Exception Classes.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Exception Handling By: Thomas Fasciano. What is an Exception?  An error condition that occurs during the execution of a Java Program.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch Announcements l Project 6 now out. »Milestone due Oct. 24th »Final project.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
CSE 11 HW 2, 3 and 4 Posted in public and on web Deadlines HW 2 turnin: Wed Jan 22 interview: Saturday January 25.class files in public directory for HW.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Exception Handling Recitation – 10/(23,24)/2008 CS 180 Department of Computer Science, Purdue University.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Abstract and Nested Classes
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.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Walt Savitch University of California, San Diego
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Does not implement clone() method! public class Bar { … public Object clone() { … } Does not implement Cloneable interface!
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Advanced Programming Rabie A. Ramadan vpro/ Lecture 4.
Java Type System and Object Model Horstmann ch , 7.7.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Creating a GUI Class An example of class design using inheritance and interfaces.
Chapter 8-Exception Handling/ Robust Programming.
Written by: Dr. JJ Shepherd
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
Lecturer: Dr. AJ Bieszczad Chapter 8 COMP 150: Introduction to Object-Oriented Programming 8-1 l Basic Exception Handling »the mechanics of exceptions.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Written by: Dr. JJ Shepherd
Chapter 8 Classes and Objects
Computer Science II Exam 1 Review.
Announcements/Reminders
Exception Handling Chapter 9.
Exception Handling Chapter 8 Basic Exception Handling
Exceptions & exception handling
Exceptions & exception handling
slides created by Ethan Apter
Exception Handling Chapter 9 Edited by JJ.
slides created by Ethan Apter
slides created by Ethan Apter and Marty Stepp
Exception Handling Chapter 8 Basic Exception Handling
CMSC 202 Exceptions.
Basic Exception Handling
Presentation transcript:

CSE 11 February 6, 2003

© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any other purpose without the written permission of Walter Savitch.

Midterm Statisatics Mean: 73.5/95 = 78% Median: 79/95 = 84% Remember No Scaling

The Class Object In Java, every class is a descendent of the predefined class Object

public class Species { private String name; private int population; private double growthRate; … MEANS

MEANS: public class Species extends Object { private String name; private int population; private double growthRate; …

Methods in the Class Object toString equals clone

toString public class Species { ….. public String toString() { return ( name + “\nPopulation = “ + population +”\nGrowth rate = “ + growthRate); }

If you add toString Species s = new Species(); …… System.out.println(s.toString()); //Not need writeOutput

equals Method public boolean equals(Object other)

public boolean equals(Object otherObject) { if (otherObject == null) return false; else if (!(otherObject instanceof Student)) return false; else { Student otherStudent = (Student)otherObject; return (this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber)); } }

Cloning (Appendix 9) Java demands a couple nuisance details. Must return a value of type Object. Class must implement the Cloneable interface, for example public class Species implements Cloneable Must account for CloneNotSupportedException.

Must return a value of type Object public class Species //will need something here { public Object clone() { … }

Class must implement the Cloneable interface For example public class Species implements Cloneable This has the syntax of implementing an interface but is not really being used like an interface (whatever that is). Just a way of saying “Yes, I really mean to do cloning.”

Class must implement the Cloneable interface public class Species implements Cloneable { public Object clone() { … }

Must account for CloneNotSupportedException Just a magic formula until we cover exceptions in Chapter 8.

CloneNotSupportedException public Object clone() { //This gives you the a simple bit map copy try { return super.clone(); } catch(CloneNotSupportedException e) {//This should never happen return null; //to keep compiler happy }

public class Species implements Cloneable {//Should work if all instance variables are //of primitive type or of type String public Object clone() { //This gives you the a simple bit map copy try { return super.clone(); } catch(CloneNotSupportedException e) {//This should never happen return null; //to keep compiler happy }

If Class has Class Instance Variables (other than String) public class Neighbor implements Cloneable { private String name; private int numberOfChildren; private PetRecord pet; //A real class public Object clone() { …..

public Object clone() { try { Neighbor copy = (Neighbor)super.clone(); copy.pet = (PetRecord)pet.clone(); return copy; } catch(CloneNotSupportedException e) { return null; } }

Warning Java Compiler may not enforce all the cloning rules.

Alternative to Cloning Copy Constructor = Constructor with one parameter of the class type that makes a “deep copy.” Really is cloning, but you avoid the baggage of the Java cloning stuff. Down side, may have trouble interacting with software that depends on your having a clone method

public class Species { private String name; private int population; private double growthRate; public Species(Species original) { name = new String(original.name); population = original.population; growthRate = original.growthRate; } |

Exception Handling Way to separate handling of “exceptional cases” from the “ordinary” cases. First example of event driven programming. Throwing an exception is a trigger event Handling the exception is the action produced in reaction to this trigger event.

public class ExceptionDemo { public static void main(String[] a) { int donutCount, milkCount; double donutsPerGlass;

try { System.out.println(“N of donuts:"); donutCount = SavitchIn.readLineInt( ); System.out.println("number of milk:"); milkCount = SavitchIn.readLineInt( ); if (milkCount < 1) throw new Exception("No Milk!"); donutsPerGlass = donutCount/(double)milkCount; System.out.println("You have " + donutsPerGlass + " donuts for each glass of milk."); }

try { … if (milkCount < 1) throw new Exception("No Milk!"); … } catch(Exception e) { System.out.println(e.getMessage( )); System.out.println("Go buy some milk."); } System.out.println("End of program.");

try { … if (Something) throw new Exception(“Bla Bla"); … } catch(Exception e) { Handle Exception Action }

How Commonly Used: try { … someMethod(…);//Might throw exception … } catch(Exception e) { Handle Exception Action }

How Commonly Used: public void someMethod(…) throws Exception { if (something) handle things else//handling depends on context throw new Exception(“Bla Bla”); }

How Commonly Used: public int readInt()throws Exception { String s = SavitchIn.readLine(); if (s is well formed) return Integer.parseInt(in); else//handling depends on context throw new Exception(“Syntax error”); }

Throws Clause public int readInt()throws Exception Warns user of method that method might throw an exception Enforced by compiler More details later

Exception Classes throw new Exception(…); new Exception(…) is an invocation of a constructor for the class Exception Equivalent to Exception e = new Exception(…); throw e;

throw Operator throw new Exception(…); Equivalent to Exception e = new Exception(…); throw e; throw e is a trigger event, a signal to wake up the catch-exception portion of your code.

More Exception Classes CloneNotSupportedException NumberFormatException NullPointerException You can define new exception classes

Defining Exception Classes Must be a derived class of an existing exception class, typically a derived class of Exception. Typically all it has and all it needs for methods are constructors and inherited methods.

public class DivideByZeroException extends Exception { public DivideByZeroException( ) { super("Dividing by Zero!"); } public DivideByZeroException(String message) { super(message); }

getMessage() Method Inherited Also inherited a private instance variable of type String. getMessage() is an accessor method that returns the string in this private instance variable.

try { … if (milkCount < 1) throw new Exception("No Milk!"); … } catch(Exception e) { System.out.println(e.getMessage( )); System.out.println("Go buy some milk."); } System.out.println("End of program.");