Today. Homework. Lecture Notes CPSC 224 (Spring 2012) Quiz 5. interfaces. exceptions. best practices. start on swing (if time) hw3 due

Similar documents
2018/2/5 话费券企业客户接入文档 语雀

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

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

COE318 Lecture Notes Week 10 (Nov 7, 2011)

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

class objects instances Fields Constructors Methods static

C16b: Exception Handling

Exceptions. Examples of code which shows the syntax and all that

Introduction to Java. Handout-3a. cs402 - Spring

CS159. Nathan Sprague

Java Errors and Exceptions. Because Murphy s Law never fails

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Written by John Bell for CS 342, Spring 2018

BBM 102 Introduction to Programming II Spring Exceptions

CS 3 Introduction to Software Engineering. 3: Exceptions

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

Programming II (CS300)

Lecture Topics. Administrivia

Exception Handling Generics. Amit Gupta

Introduction to Reflective Java

Certified Core Java Developer VS-1036

Class, Variable, Constructor, Object, Method Questions

15CS45 : OBJECT ORIENTED CONCEPTS

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

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Introduction to Objects. James Brucker

Programming II (CS300)

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

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

Exceptions. What exceptional things might our programs run in to?

Java Programming Unit 7. Error Handling. Excep8ons.

Location: Planet Laser Interview Skills Workshop

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Object Oriented Software Design

Object Oriented Software Design

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Checked and Unchecked Exceptions in Java

Errors and Exceptions

Administrivia. CPSC Winter 2008 Term 1. Department of Computer Science Undergraduate Events

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Selected Questions from by Nageshwara Rao

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Encapsulation. Mason Vail Boise State University Computer Science

16-Dec-10. Consider the following method:

Exceptions and Design

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Assumptions. History

CSCI 261 Computer Science II

Writing your own Exceptions. How to extend Exception

Chapter 13 Exception Handling

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

CMSC 132: Object-Oriented Programming II

Compaq Interview Questions And Answers

C. E. McDowell August 25, Baskin Center for. University of California, Santa Cruz. Santa Cruz, CA USA. abstract

CS 231 Data Structures and Algorithms, Fall 2016

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

Chapter 12 Exception Handling

Object-Oriented Programming (Java)

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Fundamentals of Object Oriented Programming

Introduction to Java

Programming Languages and Techniques (CIS120)

2.6 Error, exception and event handling

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

Highlights of Last Week

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

BBM 102 Introduction to Programming II Spring 2017

INSTRUCTIONS TO CANDIDATES

ITI Introduction to Computing II

ITI Introduction to Computing II

CS Introduction to Data Structures Tuesday, February 2, 2016

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

Selected Java Topics

Java for Interfaces and Networks (DT3010, HT10)

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

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

CA341 - Comparative Programming Languages

CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Self-test Java Programming

Principles of Computer Science

13: Error Handling with Exceptions. The basic philosophy of Java is that "badly formed code will not be run."

Reminder from last time

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

Chapter 2: Java OOP I

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

Object-oriented Programming. Object-oriented Programming

Exception Handling. --After creating exception object,method handover that object to the JVM.

School of Informatics, University of Edinburgh

CS61B Lecture #5: Arrays and Objects

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

this keyword (1). this with constructor (2). cisc3120 design and implementation of software applications I spring 2015 lecture # I.

Exceptions - Example. Exceptions - Example

Transcription:

Today Quiz 5 interfaces exceptions best practices start on swing (if time) Homework hw3 due hw4 out (next thurs) S. Bowers 1 of 8

Implicit vs. Explicit Parameters Methods define their explicit parameters public void credit(double amt) the explicit parameter is amt When a method is called on an object it is passed as an implicit parameter Passed as a reference to the object the method is called on ( calling object ) Account a = new Account(100); a.credit(50); // like calling "credit(a, 50)" the implicit parameter here is the reference a Use the this keyword to access the implicit parameter public void credit(double amt) { this.balance += amt; there are times when this is really needed (more later) S. Bowers 2 of 8

Java Interfaces In Java, a class can subclass (extend) at most one other class Different than C++, which allows multiple inheritance Java provides interfaces as a workaround An interface is like a fully abstract class None of the methods are implemented Every class implementing an interface must implement all the methods Or else be declared as an abstract class public interface Account { public double getbalance(); public interface CreditAccount extends Account { public void credit(double amt); public interface DebitAccount extends Account { public void debit(double amt); public class TimeDeposit implements CreditAccount { public double getbalance() { S. Bowers 3 of 8

public class Checking implements CreditAccount, DebitAccount { public double getbalance() { public void credit(double amt) { public void debit(double amt) { Technically, all methods in a public interface are public But good practice to put public anyway Interfaces can be used as types! Account a = new TimeDeposit(300); System.out.println(a.getBalance()); Q: Why would we want to do this? allows for decoupling our code from any particular implementation allows for general-purpose methods (only require certain operations) Interfaces used throughout Java API, e.g., List is an interface Warning: Don t get carried away with interfaces! S. Bowers 4 of 8

More on Exceptions We can define our own methods to throw exceptions the throws keyword is part of the signature the throw keyword causing an exception to be thrown public void debit(double amt) throws Exception { if(balance - amt < 0) throw new Exception("Insufficient funds to debit amount."); balance -= amt; try { Account a = new Account(100); a.debit(200); catch(exception e) { e.printstacktrace(); Running this prints: java.lang.exception: Insufficient funds to debit amount. at Account.debit(Test.java:21) at Test.main(Test.java:6) S. Bowers 5 of 8

If your method throws an exception use the @throws tag: /** * * @param amt the amount to debit * @throws Exception If insufficient funds available */ Methods can throw multiple types of exceptions public void f() throws Type1Exception, Type2Exception, { Convention is to create your own exception classes: public class InsufficentFundsException extends Exception { public InsufficentFundsException(String msg) { super(msg); public void debit(double amt) throws InsufficientFundsException { usually this is all you do sometimes might add methods to pass additional information S. Bowers 6 of 8

Instead of catching exceptions, you can also punt // in checking public void debit(double amt) throws InsufficientFundsException { super.debit(amt); super.debit(5); // the fee // another example public static void main(string[] args) throws Exception { useful in some cases but your apps should catch the exceptions (e.g., in main) S. Bowers 7 of 8

Garbage Collection When you create a new object in Java The JVM allocates memory space on the heap All Java objects live in the heap When an object isn t needed, the JVM frees up the space for you This is done via automatic garbage collection The garbage collector (JVM) looks for unreachable objects Example: 1. Book b = new Book(); 2. Book c = new Book(); 3. Bood d = c; // d references same object as c 4. c = new Book(); // c now references a new object 5. d = c; // now there is a non-referenced object! 6. d = null; // d is now a "null" reference 7. d.gettitle(); // this generates a NullPointerException 8. b = null; // old b object can now be garb. collected Arrays of object references Q: What happens to the heap after this statement? Book[] books = new Book[3]; Each element in the books array is a null reference Q: What happens if we call: books[1].gettitle() We get a NullPointerException! S. Bowers 8 of 8