CS61B Lecture #8. Last modified: Fri Sep 17 12:18: CS61B: Lecture #8 1

Similar documents
CS61B Lecture #11. Please report problems (missing files, malfunctions of submit, etc.) by , not by the newsgroup, for faster service.

CS61B Lecture #11: Examples: Comparable & Reader

CS61B Lecture #11: Examples: Comparable & Reader

CS61B Lecture #11: Examples: Comparable & Reader. Last modified: Wed Sep 24 12:44: CS61B: Lecture #11 1

Recreation. Last modified: Mon Sep 21 12:46: CS61B: Lecture #11 1

Recreation. Last modified: Sun Sep 16 17:51: CS61B: Lecture #11 1

CS 61B Discussion 5: Inheritance II Fall 2014

CS61B Lecture #2. Public Service Announcements:

CS61B Lecture #13: Packages, Access, Etc.

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

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

CS61B Lecture #12. Public Service Announcement. Miscellaneous Topics: What to do About Errors? throw new SomeException (optional description);

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

CS61B Lecture #13: Packages, Access, Etc.

Recreation. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Full file at

CSE 303 Midterm Exam

Sample First Midterm Exam The Answers

CS61B Lecture #5: Arrays and Objects

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

Exam Duration: 2hrs and 30min Software Design

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Public-Service Announcement

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

CS61B Lecture #9: Interfaces and Abstract Classes

Introduction to Programming Using Java (98-388)

Question 1. (2 points) What is the difference between a stream and a file?

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Name CIS 201 Midterm II: Chapters 1-8

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

CS61B, Fall 2007 Final Examination P. N. Hilfinger

Do not turn to the next page until the start of the exam.

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

Type Hierarchy. Lecture 6: OOP, autumn 2003

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

CSE 143 Au03 Midterm 2 Page 1 of 7

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Error Handling. public int divide(double a, double b) { if (b==0) return -1; // error double result = a/b; return 0; // success }

Public-Service Announcement

Programming Languages and Techniques (CIS120)

CS61B Lecture #19. Last modified: Mon Oct 13 12:02: CS61B: Lecture #19 1

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

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

COE318 Lecture Notes Week 10 (Nov 7, 2011)

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

CS61B Lecture #6: Arrays

Interview Questions of C++

CSE413 Midterm. Question Max Points Total 100

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

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Programming Languages and Techniques (CIS120)

PIC 20A Number, Autoboxing, and Unboxing

Lecture 4: Exceptions. I/O

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

COS 126 General Computer Science Spring Written Exam 1

VIRTUAL FUNCTIONS Chapter 10

Announcements. Last modified: Fri Sep 8 00:59: CS61B: Lecture #7 1

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Lecture 4: Extending Classes. Concept

COMP322 - Introduction to C++

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

DO NOT. package java.lang; public final class String implements Serializable, Comparable, CharSequence {

6.092 Introduction to Software Engineering in Java January (IAP) 2009

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

PIC 20A Streams and I/O

Dynamic arrays / C Strings

Public-Service Announcement

Do not turn to the next page until the start of the exam.

Programming Languages and Techniques (CIS120)

20-CS Programming Languages Fall Final Exam! Answer all questions Be sure to put your name on the paper in the space provided!

Jonathan Aldrich Charlie Garrod

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

CS61B, Summer 2002 Exam #1 Barath Raghavan

Design Patterns: State, Bridge, Visitor

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Algorithms & Datastructures Laboratory Exercise Sheet 1

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Last name:... First name:... Department (if not D-INFK):...

CS61B Lecture #8: Object-Oriented Mechanisms

CSE 11 Midterm Fall 2012

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

CPSC 2380 Data Structures and Algorithms

CS 116 Week 8 Page 1

CS 3 Introduction to Software Engineering. 5: Iterators

Getting started with Java

Welcome (back) to CS1007!

Today: Sorting algorithms: why? Insertion Sort. Inversions. CS61B Lecture #26. Last modified: Wed Oct 24 13:43: CS61B: Lecture #26 1

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Announcements. 1. Forms to return today after class:

Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You ll learn much less if you just look at

Java and OOP. Part 5 More

CS304 Object Oriented Programming Final Term

Transcription:

CS61B Lecture #8 Midterm tentatively scheduled for the evening of 19 October(Tuesday). Format: 2 hour, open-book. We will accommodate students with conflicts as needed; please arrange an alternative time with us the week before the exam (11 15 Oct.). Project #1 (coming soon) will be due on 13 Oct. (Wednesday night). Some advice: The Blue Reader contains information on the tools you are using (debugger, Emacs, make, javac compiler, etc.). Be curious about the software you re using and the software we give you. For example, what is our test framework doing and how? (How could you find this out?) Last modified: Fri Sep 17 12:18:33 2004 CS61B: Lecture #8 1

Example: Comparable Java library provides an interface to describe Objects that have a natural order on them, such as String Comparable, BigInteger and BigDecimal: public interface Comparable { // Java 1.4 version for now /** Returns value <0, == 0, or > 0 depending on whether * THIS is <, ==, or > OBJ. Throws ClassCastException * if OBJ is not comparable to THIS. */ int compareto (Object obj); Might use in a general-purpose max function: /** The largest value in array A, or null if A empty. */ public static Object max (Comparable[] A) { if (A.length == 0) return null; Comparable result = A[0]; for (int i = 1; i < A.length; i += 1) if (result.compareto (A[i]) < 0) result = A[i]; return result; Nowmax(S)willreturnmaximumvalueinSifSisanarrayofStrings, or any other kind of Object that implements Comparable. Last modified: Fri Sep 17 12:18:33 2004 CS61B: Lecture #8 2

Example: Readers Java class java.io.reader abstracts sources of characters. Here, we present a revisionist version (not the real thing): public interface Reader { // Real java.io.reader is abstract class /** Release this stream: further reads are illegal */ void close (); /** Read as many characters as possible, up to LEN, * into BUF[OFF], BUF[OFF+1],..., and return the * number read, or -1 if at end-of-stream. */ int read (char[] buf, int off, int len); /** Short for read (BUF, 0, BUF.length). */ int read (char[] buf); /** Read and return single character, or -1 at end-of-stream. */ int read (); Can t write new Reader(); it s abstract. So what good is it? Last modified: Fri Sep 17 12:18:33 2004 CS61B: Lecture #8 3

Generic Partial Implementation According to their specifications, some of Reader s methods are related. Can express this with a partial implementation, which leaves key methods unimplemented and provides default bodies for others. Result still abstract: can t use new on it. /** A partial implementation of Reader. Complete * implementations MUST override close and read(,,). * They MAY override the other read methods for speed. */ public abstract class AbstractReader implements Reader { public abstract void close (); public abstract int read (char[] buf, int off, int len); public int read (char[] buf) { return read(buf,0,buf.length); public int read () { return (read (buf1) == -1)? -1 : buf1[0]; private char[] buf1 = new char[1]; Last modified: Fri Sep 17 12:18:33 2004 CS61B: Lecture #8 4

Implementation of Reader: StringReader The class StringReader reads characters from a String: public class StringReader extends AbstractReader { private String str; private int k; /** A Reader delivering the characters in STR. */ public StringReader (String str) { this.str = str; k = 0; public void close () { str = null; public int read (char[] buf, int off, int len) { if (k == str.length ()) return -1; len = Math.min (len, str.length () - k); str.getchars (k, k+len, buf, off); k += len; return len; Last modified: Fri Sep 17 12:18:33 2004 CS61B: Lecture #8 5

Using Reader Consider this method, which counts words: /** The total number of words in R, where a "word" is * a maximal sequence of non-whitespace characters. */ int wc (Reader r) { int c0, count; c0 = ; cnt = 0; while (true) { int c = r.read (); if (c == -1) return count; if (Character.isWhitespace ((char) c0) &&! Character.isWhitespace ((char) c)) count += 1; c0 = c; This method works for any Reader: // Number of words in the String sometext: wc (new StringReader (sometext)) // Number of words in standard input. wc (new InputStreamReader (System.in)) // Number of words in file named filename: wc (new FileReader (filename)) other implementations of Reader Last modified: Fri Sep 17 12:18:33 2004 CS61B: Lecture #8 6

How It Fits Together Client Interface Concrete Class Abstract Template Reader StringReader AbstractReader read(b,o,l) read(b,o,l) overrides read(b,o,l) wc method read() calls read(b) read() which is really read(b) read() calls inherited from calls inherited from read(b) read() implements extends implements Last modified: Fri Sep 17 12:18:33 2004 CS61B: Lecture #8 7

Lessons TheReader interface class served as a specification for a whole set of readers. Ideally, most client methods that deal with Readers, like wc, will specify type Reader for the formal parameters, not a specific kind of Reader, thus assuming as little as possible. AndonlywhenaclientcreatesanewReaderwillitgetspecificabout what subtype of Reader it needs. That way, client s methods are as widely applicable as possible. Finally, AbstractReader is a tool for implementors of non-abstract Reader classes, and not used by clients. Alas, Java library is not pure. E.g., AbstractReader is really just called Reader and there is no interface. In this example, we saw what they should have done! The Comparable interface allows definition of functions that depend only on a limited subset of the properties (methods) of their arguments (such as must have a compareto method ). Last modified: Fri Sep 17 12:18:33 2004 CS61B: Lecture #8 8