COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
COMP 110: Introduction to Programming Tyler Johnson January 26, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 12, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 28, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014.
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Object Oriented Programming with Java
1 Cathay Life Insurance Ltd. (Vietnam) 27/11/20091.
Recitation 4. 2-D arrays. Exceptions. Animal[] v= new Animal[3]; 2 declaration of array v v null Create array of 3 elements a6 Animal[] null Assign.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Topics Introduction Types of Errors Exceptions Exception Handling
30 min Scratch July min intro to Scratch A Quick-and-Dirty approach Leaving lots of exploration for the future. (5 hour lesson plan available)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Flexible Scheduling of Software with Logical Execution Time Constraints* Stefan Resmerita and Patricia Derler University of Salzburg, Austria *UC Berkeley,
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
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.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Exception Handling Recitation – 10/(23,24)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
The Java Programming Language
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
COMP Inheritance Basics Yi Hong June 09, 2015.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance and Polymorphism
COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Lecture 17: Polymorphism (Part II)
COMP 110 More about inheritance
CS 302 Week 11 Jim Williams, PhD.
Exception Handling Chapter 9 Edited by JJ.
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Lecture 18: Polymorphism (Part II)
Chapter 11 Inheritance and Polymorphism Part 2
Subtype Substitution Principle
Presentation transcript:

COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring Announcements Program 5 Milestone 1 due Wednesday by 5pm

COMP 110: Spring Questions?

COMP 110: Spring Today in COMP 110 Brief Review Finish Inheritance Basic Exception Handling Programming Demo

COMP 110: Spring Review: Overriding Methods Person has a jump method, so all subclasses have a jump method Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

COMP 110: Spring Review: Overriding Methods Each subclass has its own jump functionality public class Person { public void jump() { System.out.println("Whee!"); } public class Athlete extends Person { public void jump() { System.out.println("I jump really well!"); }

COMP 110: Spring Review: Type Compatibilities ExtremeAthlete is an Athlete XGamesSkater is a Person Person is not necessarily a Skydiver Person p = new ExtremeAthlete(); Legal Athlete a = new Athlete(); Legal XGamesSkater xgs = new Person(); Illegal

COMP 110: Spring Polymorphism many forms Enables the substitution of one object for another as long as the objects have the same interface 8

COMP 110: Spring Dynamic Binding public static void jump3Times(Person p) { p.jump(); } public static void main(String[] args) { XGamesSkater xgs = new XGamesSkater(); Athlete ath = new Athlete(); jump3Times(xgs); jump3Times(ath); }

COMP 110: Spring Inheritance Some final things on inheritance Implementing the equals method

COMP 110: Spring The Class Object The Java class Object provides methods that are inherited by every class For example equals, toString These methods should be overridden with methods appropriate for the classes you create

COMP 110: Spring The equals Method Every class has a default.equals() method Inherited from the class Object Returns whether two objects of the class are equal in some sense Does not necessarily do what you want You decide what it means for two objects of a class you create to be considered equal by overriding the equals method Perhaps books are equal if the names and page numbers are equal Perhaps only if the names are equal Put this logic inside.equals() method

COMP 110: Spring The equals Method Object has an equals method Subclasses should override it public boolean equals(Object obj) { return (this == obj); } What does this method do? Returns whether this has the same address as obj This is the default behavior for subclasses

COMP 110: Spring The equals Method First try: public boolean equals(Student std) { return (this.id == std.id); } This is overloading, not overriding We want to be able to test if two Objects are equal Student - id: int + getID(): int + setID(int newID): void

COMP 110: Spring The equals Method Second try public boolean equals(Object obj) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } What does this method do? Typecasts the incoming Object to a Student Returns whether this has the same id as otherStudent

COMP 110: Spring The equals Method public boolean equals(Object obj) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } Why do we need to typecast? Object does not have an id, obj.id would not compile Whats the problem with this method? What if the object passed in is not actually a Student? The typecast will fail and we will get a runtime error

COMP 110: Spring The instanceof Operator We can test whether an object is of a certain class type: if(obj instanceof Student) { System.out.println("obj is an instance of the class Student"); } Syntax: object instanceof Class_Name Use this operator in the equals method

COMP 110: Spring The equals Method Third try public boolean equals(Object obj) { if ((obj != null) && (obj instanceof Student)) { Student otherStudent = (Student)obj; return (this.id == otherStudent.id); } return false; } null is a special constant that can be assigned to a variable of a class type – means that the variable does not refer to anything right now

COMP 110: Spring Basic Exception Handling Section 9.1 in text

COMP 110: Spring Error Handling Recall from Program 4 Parse a string of the form operand1 operator operand2 For example " " We assumed the input was valid What if its not?

COMP 110: Spring Error Handling Example of invalid input "g " Your programs would happily parse away and attempt to call Double.parseDouble("g23.55"); Result? Your program crashes with a NumberFormatException

COMP 110: Spring Exceptions An exception is an object that signals the occurrence of an unusual (exceptional) event during program execution Exception handling is a way of detecting and dealing with these unusual cases in principled manner i.e. without a run-time error or program crash

COMP 110: Spring Example Handling divide by zero exceptions BasketballScores int score = keyboard.nextInt(); int scoreSum = 0; int numGames = 0; while(score >= 0) { scoreSum += score; numGames++; score = keyboard.nextInt(); } double average = scoreSum/numGames; Possible ArithmeticException: / by zero!

COMP 110: Spring Example We could do this int score = keyboard.nextInt(); int scoreSum = 0; int numGames = 0; while(score >= 0) { scoreSum += score; numGames++; score = keyboard.nextInt(); } double average = 0; if(numGames > 0) average = scoreSum/numGames;

COMP 110: Spring Example Using Exception Handling (try/catch blocks) int score = keyboard.nextInt(); int scoreSum = 0; int numGames = 0; while(score >= 0) { scoreSum += score; numGames++; score = keyboard.nextInt(); } double average = 0; try { average = scoreSum/numGames; } catch(ArithmeticException e) { System.out.println(e.getMessage()); System.out.println("Cannot compute average for 0 games"); }

COMP 110: Spring Example When numGames != 0 int score = keyboard.nextInt(); int scoreSum = 0; int numGames = 0; while(score >= 0) { scoreSum += score; numGames++; score = keyboard.nextInt(); } double average = 0; try { average = scoreSum/numGames; } catch(ArithmeticException e) { System.out.println(e.getMessage()); System.out.println("Cannot compute average for 0 games"); }

COMP 110: Spring Example When numGames == 0 int score = keyboard.nextInt(); int scoreSum = 0; int numGames = 0; while(score >= 0) { scoreSum += score; numGames++; score = keyboard.nextInt(); } double average = 0; try { average = scoreSum/numGames; } catch(ArithmeticException e) { System.out.println(e.getMessage()); System.out.println("Cannot compute average for 0 games"); }

COMP 110: Spring The try Block A try block contains the basic algorithm for when everything goes smoothly Try blocks will possibly throw an exception Syntax try { Code_To_Try } Example try { average = scoreSum/numGames; }

COMP 110: Spring The catch Block The catch block is used to deal with any exceptions that may occur This is your error handling code Syntax catch(Exception_Class_Name Catch_Block_Parameter) { Process_Exception_Of_Type_Exception_Class_Name } Possibly_Other_Catch_Blocks Example catch(ArithmeticException e) { System.out.println(e.getMessage()); System.out.println("Cannot compute average for 0 games"); }

COMP 110: Spring Throwing Exceptions You can also throw your own exceptions try { int score = keyboard.nextInt(); int scoreSum = 0; int numGames = 0; while(score >= 0) { scoreSum += score; numGames++; score = keyboard.nextInt(); } if(numGames <= 0) throw new Exception("Exception: num games is less than 1"); double average = scoreSum/numGames; } catch(Exception e) { //catches any kind of exception System.out.println(e.getMessage()); }

COMP 110: Spring Throwing Exceptions Syntax throw new Exception_Class_Name(Possibly_Some_Arguments); Example throw new Exception( "Exception: num games is less than 1"); or Exception exceptObject = new Exception("Illegal character."); throw exceptObject;

COMP 110: Spring Exception Objects An exception is an object All exceptions inherit the method getMessage() from the class Exception Example catch(ArithmeticException e) { System.out.println(e.getMessage()); System.out.println("Cannot compute average for 0 games"); }

COMP 110: Spring Java Exception Handling A try block contains code that may throw an exception When an exception is thrown, execution of the try block ends immediately Depending on the type of exception, the appropriate catch block is chosen and executed If no exception is thrown, all catch blocks are ignored

COMP 110: Spring Programming Demo Adding Exception Handling to Program 4 If we call Double.parseDouble() with invalid input such as "g23.55", the method will throw a NumberFormatException Catch this exception and signal to the user there was a problem with the input

COMP 110: Spring Programming Demo Programming

COMP 110: Spring Wednesday Basic File I/O