CS 11 java track: lecture 3

Similar documents
Fundamentals of Object Oriented Programming

Full file at Chapter 2 - Inheritance and Exception Handling

2.6 Error, exception and event handling

Exception-Handling Overview

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

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

COMP-202 Unit 9: Exceptions

Programming II (CS300)

CS159. Nathan Sprague

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Data Structures. 02 Exception Handling

CSCI 261 Computer Science II

More on Objects in JAVA TM

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

Lecture 2: Java & Javadoc

COMP-202 Unit 9: Exceptions

Exceptions. CSC207 Winter 2017

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Object-Oriented Design. March 2005 Object Oriented Design 1

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Inheritance and Polymorphism

15CS45 : OBJECT ORIENTED CONCEPTS

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

CS Exam 1 Review Suggestions

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Java Programming Lecture 7

14. Exception Handling

Chapter 10 Classes Continued. Fundamentals of Java

Object-oriented programming in Java (2)

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?

Core Java Syllabus. Pre-requisite / Target Audience: C language skills (Good to Have)

CMSC 331 Second Midterm Exam

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Making New instances of Classes

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

First Name: AITI 2004: Exam 2 July 19, 2004

Selected Java Topics

Exceptions and Libraries

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming. Java-Lecture 11 Polymorphism

More on Exception Handling

BBM 102 Introduction to Programming II Spring Exceptions

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

CS159. Nathan Sprague

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

PROGRAMMING LANGUAGE 2

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

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Programming II (CS300)

Chapter 14. Exception Handling and Event Handling ISBN

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

Object Oriented Java

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

The Essence of OOP using Java, Nested Top-Level Classes. Preface

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Chapter 14 Abstract Classes and Interfaces

CMSC 132: Object-Oriented Programming II

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

More on Exception Handling

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Chapter 14. Exception Handling and Event Handling

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

ECE 122. Engineering Problem Solving with Java

First Name: AITI 2004: Exam 2 July 19, 2004

CS11 Java. Fall Lecture 3

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Correctness and Robustness

CS 251 Intermediate Programming Exceptions

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

ITI Introduction to Computing II

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

BBM 102 Introduction to Programming II Spring 2017

CS 162, Lecture 25: Exam II Review. 30 May 2018

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Object-Oriented Programming (OOP) Fundamental Principles of OOP

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

PESIT Bangalore South Campus

VALLIAMMAI ENGINEERING COLLEGE

Object Oriented Programming

Inheritance and Interfaces

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

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

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

ITI Introduction to Computing II

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

Transcription:

CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces (GUIs)

javadoc (1) three ways to write comments: // this way (to end of line) /* this way (to closing star-slash) */ /** * this way (javadoc style) */

javadoc (2) /** * This is a javadoc comment for a method. Note * the two asterisks after the starting "/". * * @param foo This is a comment for the argument "foo". * @return This describes the return value. */ public int mymethod(double foo) { // code goes here }

javadoc (3) Javadoc comment tags: @param describes an argument @return describes the return value @throws describes an exception that can be thrown Can use the "javadoc" tool to generate HTML documentation from classes this way but we won't (you can try if you want)

Exception handling Purpose of exceptions deal with "exceptional situations" errors (e.g. divide by zero) rare occurrences (e.g. end of file when reading) normal flow of control won't work don't want to clutter the code with lots of tests

Syntax syntax of exceptions try { // some code that might throw an exception } catch (IOException e) { // code to handle IO exceptions } catch (Exception e) { // code to handle all other exceptions } finally { // Do this whether or not an exception // was thrown. }

More about exception syntax can have try and one catch try and several catches try and finally try, catch, finally try, several catches, finally most of the code will be in the try block

try/catch/finally meaning of try/catch/finally: try to execute the code in this block if an exception is thrown, look at the catch blocks if one of them identifies the type of the exception, execute that block; else go to the next block type of exception == same class or a subclass if there is a finally block, execute it after the try block and any catch blocks even if a new exception is thrown

finally finally blocks are rare usually used to "clean up" before exiting a method e.g. close a file that was opened in a try block even if a new exception gets thrown in the try block or in a catch block, finally block still gets executed

throw if you want to generate an exception you must throw it if (something_bad_happens()) { throw new Exception("BAD!"); } exceptions are classes (need new) exception constructors can take arguments usually just pass a string with a message

checked exceptions (1) most exceptions in java are checked exceptions this means that code that can throw an exception must either be in a try/catch block be in a method that declares that it throws that exception

checked exceptions (2) say your method can throw an IOException public void mymethod() { try { // code that may throw IOException } catch (IOException e) { // handle the exception } }

checked exceptions (3) alternatively... public void mymethod() throws IOException { } // code that may throw IOException now don't need the try/catch blocks could just declare that all methods throw Exception (but that's lame)

RuntimeExceptions some exceptions are so unpredictable or can happen in so many places that having to check for them would be unbearably tedious e.g. divide by zero (ArithmeticException) array bounds violations (ArrayIndexOutOfBoundsException) these are RuntimeExceptions don't have to be caught or declared can still catch or declare if you want

catch-all exception handlers can catch ALL exceptions this way: try { // code that can throw exceptions } catch (Exception e) { } // handle all exceptions e.g. System.out.println(e); NOT good practice should catch specific exceptions; use catch-all exceptions only after catching specific exceptions if at all

back to OOP -- inheritance consider: // base class public class Vehicle { private double accel; protected int numberofwheels; public Point2d getposition() { /*... */ } public float getvelocity() { /*... */ } }

inheritance (2) // subclass public class MotorVehicle extends Vehicle { private double fuel; public float getfuelremaining() { /*... */ } public String getlicenseplate() { /*... */ } // override base class method: public float getvelocity() { /*... */ } }

inheritance (3) subclass has an "is-a" relationship to base class (also called "superclass") MotorVehicle "is-a" Vehicle we say it "inherits from" Vehicle instance of MotorVehicle can call getposition(), getvelocity() (superclass methods) gets them "for free" can also call getfuelremaining() or getlicenseplate() (new methods)

inheritance (4) can override superclass methods in subclass e.g. new getvelocity() method in MotorVehicle can also add new fields or new methods private fields and methods not accessible to subclasses protected fields and methods are accessible to subclasses

polymorphism (1) the "big idea" in object-oriented programming basic idea: the appropriate method is chosen based on the object that the method is called on vague enough for you? example...

polymorphism (2) Vehicle v = new Vehicle(); MotorVehicle mv = new MotorVehicle(); mv = v; // DOES NOT COMPILE! // Not all Vehicles are MotorVehicles! v = mv; // OK: all MotorVehicles are Vehicles. // DOES NOT "convert" mv to a Vehicle. System.out.println(v.getVelocity()); // Which getvelocity() method gets called? // ANSWER: the MotorVehicle getvelocity() method. // v is still a MotorVehicle.

abstract classes (1) a class with some methods unimplemented is an abstract class such a class cannot be instantiated can be used to make subclasses that can be instantiated ("concrete" subclasses) methods declared with the keyword abstract aren't defined class also has to have the abstract keyword

abstract classes (2) abstract class Shape { Point2d center; abstract void draw(); } class Circle extends Shape { void draw() { /* draw a Circle */ } } class Rectangle extends Shape { void draw() { /* draw a Rectangle */ } }

abstract classes (3) Shape s = new Shape(); // COMPILE ERROR Shape s = new Rectangle(); // OK s.draw(); // draws a Rectangle

interfaces (1) an abstract class with no fields and only abstract methods can also be called an interface interface Drawable { } public void draw();

interfaces (2) why bother having interfaces? why not just have abstract classes? answer: a class can extend only one class (abstract or not), but can implement any number of interfaces this is how java handles what would require multiple inheritance in e.g. C++ interfaces specify behavior ONLY not implementation

interfaces (3) subtle point: if a class is a subclass of a class which implements an interface Foo, then the subclass also implements interface Foo (you don't have to re-declare it)

default constructors every class has a no-argument constructor if you don't explicitly supply one, one is created for you that does nothing this is the "default constructor"

inheritance and constructors super(arg1, arg2,...); calls the superclass constructor with arguments arg1, arg2, etc. can only be called inside a subclass constructor if called, must be the first statement in the constructor if not called, the default superclass constructor is automatically called used to make initialization easier only need to initialize new fields

graphical user interfaces (GUIs) GUIs are composed of buttons, frames, windows, menus, sliders, etc. usual way of interfacing with interactive programs in java, two main sets of libraries for GUIs: AWT (Abstract Windowing Toolkit) Swing (more advanced and nicer-looking)

GUI inheritance hierarchy long inheritance tree for GUI classes: java.lang.object +--java.awt.component +--java.awt.container +--javax.swing.jcomponent +--javax.swing.abstractbutton +--javax.swing.jbutton

GUI objects first do this: import javax.swing.*; // all of the Swing classes import java.awt.*; // fundamental GUI classes GUI objects: JFrame: a window Container: window, not incl. menus, titlebar, etc. JButton: a button and lots more...

next week arrays listener classes inner classes anonymous inner classes