JAVA BASICS II. Example: FIFO

Similar documents
References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assertions. Assertions - Example

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Assertions, pre/postconditions

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

15CS45 : OBJECT ORIENTED CONCEPTS

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

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

3. Design by Contract

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

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

Subclassing for ADTs Implementation

CS 11 java track: lecture 3

Chapter 4 Defining Classes I

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

Correctness and Robustness

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

a correct statement? You need to know what the statement is supposed to do.

1 Shyam sir JAVA Notes

17. Handling Runtime Problems

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

Introduction to Computing II (ITI 1121) Final Examination

CSE 331 Midterm Exam Sample Solution 2/13/12

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Programming II (CS300)

Comp 249 Programming Methodology Chapter 9 Exception Handling

Introduction to Programming Using Java (98-388)

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

CS/ENGRD 2110 SPRING 2018

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

Adding Contracts to C#

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Assertions & Design-by-Contract using JML Erik Poll University of Nijmegen

1.00 Introduction to Computers and Engineering Problem Solving. Final Examination - May 19, 2004

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

More on Exception Handling

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

Programming II (CS300)

Full file at Chapter 2 - Inheritance and Exception Handling

Linked Lists. Chapter 12.3 in Savitch

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

Objects, Subclassing, Subtyping, and Inheritance

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

More on Objects in JAVA TM

Concurrent Computing CSCI 201 Principles of Software Development

Definition of DJ (Diminished Java)

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the

Written by John Bell for CS 342, Spring 2018

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

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

Core Java Interview Questions and Answers.

Self-test Java Programming

Designing Robust Classes

Object Oriented Programming

CS18000: Programming I

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

Exception-Handling Overview

CSE 143 SAMPLE MIDTERM

School of Informatics, University of Edinburgh

Conformance. Object-Oriented Programming Spring 2015

The Contract Pattern. Design by contract

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?

CS260 Intro to Java & Android 03.Java Language Basics

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

c) And last but not least, there are javadoc comments. See Weiss.

UC Santa Barbara. CS189A - Capstone. Christopher Kruegel Department of Computer Science UC Santa Barbara

Array Based Lists. Collections

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

CS 3 Introduction to Software Engineering. 3: Exceptions

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

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

COMP-202 Unit 9: Exceptions

Chapter 10 Classes Continued. Fundamentals of Java

Typecasts and Dynamic Dispatch. Dynamic dispatch

More on Exception Handling

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

School of Informatics, University of Edinburgh

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

Synchronization SPL/2010 SPL/20 1

Object Oriented Programming Exception Handling

CSE 331 Summer 2016 Final Exam. Please wait to turn the page until everyone is told to begin.

Prelim 1. Solution. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer

Java Threads and intrinsic locks

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

Class, Variable, Constructor, Object, Method Questions

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

Universe Type System for Eiffel. Annetta Schaad

Classes, interfaces, & documentation. Review of basic building blocks

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Transcription:

JAVA BASICS II Example: FIFO To show how simple data structures are built without pointers, we ll build a doubly-linked list ListItem class has some user data first refers to that ListItem object at the front of the queue last refers to the object at the end of the queue, i.e. most recently added 2 public class ListItem { // In file ListItem.java public Object x; // N.B. a heterogeneous queue public ListItem previous; public ListItem next; // Constructor operation takes initial value public ListItem(String val) { // this refers to current object this.x = val; this.previous = this.next = null; public boolean equals(listitem c) { // two ListItems are equal if their string values // are equal and they point to the same objects return ( x.equals(c.x) && (previous == c.previous) && (next == c.next) ); public void printitem() { System.out.println(x); import java.applet.*; // overview of fifo.java public class fifo extends Applet { private int count = 0; public ListItem first = null; // first is the next item to be removed public ListItem last = null; // last is the item most recently added // Called to initialize and test the applet. More detail on next page. public void init() { System.out.println("isEmpty returns "+isempty()); putqueue("node 1"); getqueue().printitem(); // See if the queue is empty public boolean isempty() { // Add an item to the queue public void putqueue(string value) { // Get the first item off the front of the queue public ListItem getqueue() {

// Called to initialize and test the applet. public void init() { System.out.println("isEmpty returns "+isempty()); putqueue("node 1"); System.out.println("First node is "); first.printitem(); System.out.println("Last node is "); last.printitem(); // See if the queue is empty public boolean isempty() { return (count == 0); // Add an item to the queue public void putqueue(string value) { putqueue("node 2"); System.out.println("First node is "); first.printitem(); System.out.println("Last node is "); last.printitem(); getqueue().printitem(); System.out.println("First node is "); first.printitem(); System.out.println("Last node is "); last.printitem(); getqueue().printitem(); System.out.println("isEmpty returns "+isempty()); ListItem newitem = new ListItem(value); if ( isempty() ) { // Special case of empty queue first = last = newitem; else { // next is the next item in the queue // previous is the item (if any) that was in the // queue right ahead of this (current) item last.next = newitem; newitem.previous = last; last = newitem; count++; // Get the first item off the front of the queue public ListItem getqueue() { ListItem firstitem = first; // Make sure the queue isn't empty if (isempty() ) { System.out.println("Called getqueue on an empty queue"); else { this.first = firstitem.next; // Did we just remove the only item in the queue? if (first == null) { last = null; else { first.previous = null; count--; return firstitem; Programming by Contract A paradigm first introduced by Bertrand Meyer, the creator of the OO programming language Eiffel. Eiffel has built-in support for programming by contract, but most of the concepts can be used in any language. Idea: create a contract between the software developer (supplier) and software user (consumer) Methods should start with a precondition that must be satisfied by the consumer of the routine. And end with postconditions which the supplier guarantees to be true (if and only if the preconditions were met). Each class has an invariant which must be satisfied after any changes to the object represented by the class, I.e., the invariant guarantees the object is in a valid state. Benefits: a good way to document requirements that can also be checked by the program. Saves lots of debugging. 8

Programming by Contract Note that the integer variable count, and first and last (both of type ListItem, are redundant in that first and last are null iff count == 0 first == last, but both not null iff count == 1 otherwise first!= last iff count > 1 Java has no assert macro, but we can test and throw an exception. // See if the queue is empty // Check consistency of count, first and last // Note that exceptions are first-class objects class CorruptFifoException extends Exception; public boolean isempty() { if (count == 0) { if (first == null && last == null) { return (true); else { throw new CorruptFifoException( first and last should be null ); else { // count!= 0 9 Single Inheritance, but Classes and Interfaces A class may extend only one class, but it may implement many others A subclass inherits the variables and methods of its superclass(es), but may override them Overrides the methods defined in the class(es) it implements, as in upcoming thread example The methods of an abstract class are implemented elsewhere A final class cannot be extended Instances of a synchronizableclass can be arguments of a synchronize block Which means that access to critical sections is restricted 11 12

Interfaces Java does not allow multiple inheritance because it introduces problems as well as benefits. Fortunately, Java allows you to impose requirements on a class from multiple class-like interfaces. An interface is like an abstract class in that it can hold abstract method definitions that force other classes to implement ordinary methods. But it is also different: An interface does NOT have instance variables (but it can have constants) All methods in an interface are abstract (they each have a name, parameters, and a return type, but no implementation) All methods in an interface are automatically public. 13 Classes vs. Interfaces A class definition that implements an interface must define all the methods specified in that interface. In this respect, an interface is like an abstract class. An interface differs from an abstract class, however, in several respects: An interface only imposes definition requirements; interfaces do not supply definitions. A class extends exactly one superclass; a class can implement an unlimited number of interfaces. Thus, the purpose of the interface is strictly to impose requirements via its abstract methods; there are no method implementations: 14 Interfaces Interfaces provide no mechanism for enforcing method specifications, other than method signatures you are free to deposit descriptive comments in an interface, however. Interfaces are excellent places for descriptive comments for two reasons: Interfaces, unlike class definitions, are free of clutter from implementing code. Programmers look to interfaces for method and class documentation. Interfaces The interface mechanism is an enormously important aid to good programming practice. Interfaces allow you to shift to the Java compiler a requirement-managing responsibility that otherwise would engage your own, human attention. Interfaces encourage you to document your classes by acting, by convention, as documentation centers. 15 16

Interfaces Example java.lang defines a Comparable interface as: public interface Comparable {int compareto(object other); // no implementation If you want an interface to impose requirements on a particular class, don t extend it; instead implement it: public class someclassname implements I1, I2 { public class Movie3 extends Attraction implements Comparable { public int compareto (Object othermovie) { Movie3 other = (Movie3) othermovie; if (rating()< other. rating()) return -1; else if (rating() > other. rating()) return 1; If an error does occur, that error is said to be exceptional behavior that throws an exception. Whenever an expression has the potential to throw an exception, you can embed that expression in a try catch statement, in which you specify explicitly what Java is to do when an exception actually is thrown. are objects in their own right They can be generated, caught and handled under program control Examples: IOException, ArithmeticException, etc. else return 0; 17 18 try/catch/finally Associates a set of statements with one or more exceptions and some handling code try { Thread.sleep(200); catch(interruptedexceptione){ System.out.println(e); finally { System.out.println( Wakeup ); Java will throw an exception when unusual conditions arise during execution of programs, e.g., E.g., Attempt to divide an integer by zero To handle the exception, use the following: try {statement with potential to throw exception catch (exception-class-name parameter) {exception-handling-code To catch I/O exceptions, use: FileNotFoundException or IOException class. 19 20

Suppose, for example, that you want to open a file for reading using a FileInputStream instance. You can acknowledge that the attempt may throw an exception by embedding the reading expressions in a block following the try keyword. Java stops executing statements in the try block as soon as an exception is thrown: try { < An attempt to attach a stream to a file occurs here You specify what to do in the event that the exception is an instance of the IOException class by writing the keyword catch, followed by a parameter typed by IOException, surrounded by parentheses, followed by another block: catch (IOException e) { 21 22 To shut a program down, use System.exit(0); To have a block of statements executed after a try (whether or not an exception was thrown) use: finally { clean-up statements You can create (and throw) your own exceptions, e.g., public class StrangeNewException extends Exception { throw (new StrangeNewException () ) catch ( StrangeNewException e) { Alternative method to handle exceptions: public static void f(params) throws Exceptionclass { 23