Many strategies have been developed for this, but they often produce surprising results.

Similar documents
Array. Prepared By - Rifat Shahriyar

Introduction to Programming Using Java (98-388)

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

CS 231 Data Structures and Algorithms, Fall 2016

1 Shyam sir JAVA Notes

Chapter 2: Using Data

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Full file at

Pace University. Fundamental Concepts of CS121 1

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Java Primer 1: Types, Classes and Operators

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

2. The object-oriented paradigm

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

ECE 122 Engineering Problem Solving with Java

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Operators and Expressions

Chapter 2: Using Data

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Object-oriented programming in...

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

CS260 Intro to Java & Android 03.Java Language Basics

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

2. The object-oriented paradigm!

HST 952. Computing for Biomedical Scientists Lecture 8

Lecture Set 4: More About Methods and More About Operators

Java Language Features

Chapter 4 Defining Classes I

Lecture Set 4: More About Methods and More About Operators

Declarations and Access Control SCJP tips

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Praktische Softwaretechnologie

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

COP 3330 Final Exam Review

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Java Bytecode (binary file)

Java Overview An introduction to the Java Programming Language

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Simple Java Reference

Lecture 1: Object Oriented Programming. Muhammad Hafeez Javed

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CSC Java Programming, Fall Java Data Types and Control Constructs

Java. Representing Data. Representing data. Primitive data types

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

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

Sri Vidya College of Engineering & Technology Question Bank

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

CS111: PROGRAMMING LANGUAGE II

Programming overview

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Design Patterns: State, Bridge, Visitor

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

PIC 20A Number, Autoboxing, and Unboxing

Objectives. In this chapter, you will:

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Arrays Classes & Methods, Inheritance

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

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Object Oriented Programming Exception Handling

Inheritance and Interfaces

CE221 Programming in C++ Part 1 Introduction

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Java+- Language Reference Manual

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

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

Java Object Oriented Design. CSC207 Fall 2014

The Java Series. Java Essentials Advanced Language Constructs. Java Essentials II. Advanced Language Constructs Slide 1

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

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

Chapter 6 Introduction to Defining Classes

Class, Variable, Constructor, Object, Method Questions

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS121/IS223. Object Reference Variables. Dr Olly Gotel

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CSE 142 Su 04 Computer Programming 1 - Java. Objects

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Program Fundamentals

School of Informatics, University of Edinburgh

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

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Brief Summary of Java

Language Reference Manual simplicity

CSC 1214: Object-Oriented Programming

Chapter 02: Using Data

tostring() Method tostring() Method -Example tostring() Method -Example StringBufferClass StringBuffer class - Constructors

Zheng-Liang Lu Java Programming 45 / 79

Full file at

Decaf Language Reference Manual

Programming Language Basics

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Transcription:

Interfaces C++ is among those languages offering multiple inheritance: a class may inherit from more than one direct superclass. This adds flexibility, but it also brings problems. A class may inherit two or more methods of a given name along different paths. Which method should take precedence? Outline for Lecture 14 I. Interfaces II. Modularization. III. File streams IV. Characters & Strings V. Arrays VI. Collection types Many strategies have been developed for this, but they often produce surprising results. Multiple inheritance imposes considerable implementation difficulties on a language, and may make it take longer to call a method. In view of these difficulties, Java adopted a different approach: A class is allowed to inherit methods from more than one direct superclass, but all the methods inherited must be abstract, with the exception of the methods inherited from a single class. This solves the problems with multiple inheritance: Since only a single method is inherited, there can t be any conflict about which method predominates. Since the inheritance graph is a tree, implementation is comparatively simple and fast. The entirely-abstract classes inherited from are called interfaces. Let s take a look at an interface. interface Drivable { Lecture 14 Object-Oriented Languages and Systems 1

boolean startengine(); void stopengine(); float accelerate(float acc); boolean turn(direction dir); The keyword interface replaces the word class in the class definition, and all methods must be abstract. Note that these methods, being abstract, have no body defined here. Instead, they end with a semicolon. They could be defined to be abstract, but that s not necessary. Since interfaces name capabilities, it s often a good idea to name them after those abilities. Any class that implements all the methods can then declare that it implements the interface. class Automobile implements Drivable { boolean startengine() { if (nottoocold) enginerunning = true; void stopengine() { enginerunning = false; etc. Other classes, like lawnmower, could also implement Drivable. An interface can also serve as a type specifier. You can write procedures that take objects of the interface type as parameters: Drivable myvehicle; But the main purpose of an interface is to impose requirements on the class(es) that want to implement it. Example: The java.util.enumeration interface. 2002 Edward F. Gehringer CSC/ECE 517 Lecture Notes, Fall 2002 2

This interface can be used by any kind of set to provide serial access to its elements. An object that implements the Enumeration interface must provide two methods, nextelement(), which returns an Object type so it can be used with any kind of collection, and hasmoreelements(). Any kind of object can implement the Enumeration interface and then use it to provide access to its elements. This means we can write general looping code, to which we later plug in the appropriate object types. Enumeration e = while (e.hasmoreelement()) { String s = (String) e.nextelement(); System.out.println(s); Modularization of Java programs In Visualworks\Smalltalk, related classes are combined into the same category. This is the only structuring feature available in Smalltalk. Java has two different mechanisms for grouping classes. Multiple classes may be defined in the same file. In this case, however, only one of these classes may be public the one whose name matches the name of the file. Classes from multiple files may be grouped into a package. In this case, an identical package statement is placed at the beginning of each file. package csc517.project.gui; public class Window { Lecture 14 Object-Oriented Languages and Systems 3

package csc517.project.gui; public class DialogBox extends Window { Package names. The components of package names always correspond to the components at the end of the pathname where they are stored. The beginning of the pathname is taken from the CLASSPATH environment variable. So, the full pathname could be /ncsu/efg/csc517/project/gui/window.java Protection of variables and methods: So far, we have seen three of Java s visibility levels, public,, and. Java has another visibility level: default, i.e., declared without a visibility specifier int i; Here is a table that summarizes the visibility: Situation public protected Default private Inherited by subclass in same package? Inherited by subclass in different package? Accessible to other classes in same package? Accessible to classes in different packages? Yes Yes Yes No Yes Yes No No Yes Yes Yes No No No No 2002 Edward F. Gehringer CSC/ECE 517 Lecture Notes, Fall 2002 4

Note that it is possible to have public access methods, but private instance variables. This allows the implementor to change the implementation of the class without affecting clients of the class, as long as the signature of the methods doesn t change. Also note the difference between inherited and accessible. Subclasses inherit protected fields, but they can t access them in instances of the superclass. The same applies to protected fields when the subclass is not in the same package as the superclass. File streams A stream is a sequence of data. The stream that flows from your keyboard to your program is the standard input stream. The stream that flows from your program to the display is the standard output stream. To read data, you create an input stream that connects an input file to your program. In order to use input (or output) streams, you need to inform Java that you want to use classes from the input-output package: import java.io.fileinputstream; import java.io.fileoutputstream; or simply import java.io.*; Then, you create an instance of FileInputStream, and assign it to a particular file. FileInputStream inputfile = new FileInputStream("/ncsu/efg/input.data"); Lecture 14 Object-Oriented Languages and Systems 5

We can use inputfile.read() to read a single byte. However, it is usually more convenient to read complete numbers and strings. To do this, one converts a FileInputStream into an instance of StreamTokenizer. StreamTokenizer tokens = new StreamTokenizer(inputFile); Tokenizers treat whitespace as delimiters. Thus, a file containing the following characters is viewed as a stream of seven numbers: 1.0 2 3.1415926536-42 5.15e6 678 0 The nexttoken method moves the tokenizer from one token to the next. Each time it moves to a token, it assigns the token it moves past to its instance variable nval. What are the values that nval takes on on successive calls to nexttoken? The nval variable is of type double, so if you want to change it to any other value, you need to cast it: (int) tokens.nval When the tokenizer reaches the end of the stream, nexttoken returns a special value, which is the same as the value in the tokenizer s TT_EOF instance variable. Thus, to test for end-of-file, you can use while (tokens.nexttoken()!= TT_EOF) { i = (int) tokens.nval Before the end of the file is reached, nexttoken returns TT_NUMBER whenever it reads a number (and it assigns the value of the number to nval), and 2002 Edward F. Gehringer CSC/ECE 517 Lecture Notes, Fall 2002 6

TT_WORD whenever it reads a string (and it assigns the value of the string to sval). Output streams are created in a similar fashion: FileOutputStream outputfile = new FileOutputStream("output.data"); You can write a byte at a time to the output stream with the write(byte[] b) method. However, it is usually more convenient to use an instance of PrintStream. PrintStream ouput = new PrintStream(outputFile); You can write to PrintStreams using the print and println methods. After you have finished reading from or writing to a file, you should close it: inputfile.close(); outputfile.close(); Characters and strings We have seen that tokenizers can be used to read data from input files. The sval instance variable receives a string that is scanned by the tokenizer. Tokenizers can also be used to read quoted strings and to treat endof-line as significant. The length of a string can be determined with the length() message: String s = "Hello, world!"; System.out.println(s.length); Strings are constants. You can t change a string, although you can concatenate two to produce a longer string. Lecture 14 Object-Oriented Languages and Systems 7

A single character can be fetched from a string with the charat method. s.charat[0] returns Java strings differ from C++ strings. In C++, strings are arrays of characters. They are terminated by null characters. Java strings are instances of the String class. They are not terminated by null characters. (Actually, due to bounds-checking, it is impossible to access the character after the end of a string.) Characters in Java are surrounded by single quotes, e.g., 'E', and are declared like char c = 'E'; The standard default of a character is the null character, denoted by '\u000' Arrays In Java, an array is declared by giving the type and the variable name: int scores[]; Note that the declaration doesn t tell how large the array is. The size is determined when the array is created, with the new operator: scores = new int [classsize]; One may combine these two statements: int scores[] = new int [classsize]; The first element in each array has subscript 0. Thus, to create an array of month lengths, where the length of the ith month is stored in the ith element, we must declare it like this: static int month_days[] = new int [13]; 2002 Edward F. Gehringer CSC/ECE 517 Lecture Notes, Fall 2002 8

Instead of explicitly giving the size of the array, one may use an array initializer, which implicitly specifies it: static int month_days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31; Array elements are accessed just as in C or C++: System.out.println(month_days[2]); Each array has an instance variable called length: month_days.length It is also possible to declare arrays of class types: Circle circles [] = new Circle [4]; One may create a new object to put into an array: circles[3] = new Circle(5.5); And we can access the fields and methods of array elements: circles[3].radius circles[3].area() Overwriting an array element can cause an instance to be garbage collected: circles[3] = new Circle(1.2); Now, if the old value of circle[3] hasn t been stored anywhere else, the circle at 5.5 may be garbage-collected. Implementation of arrays Arrays of primitive types are laid out with 4 bytes for the length instance variable. Enough bytes in each element to hold an item of the type (in the case of int, 4 bytes). Arrays of class types are laid out with Lecture 14 Object-Oriented Languages and Systems 9

4 bytes for the length instance variable. Enough bytes in each element to hold the address of the corresponding array element: length instance var. address of 1st elt. address of 2nd elt. address of last elt. Because addresses of class instances are stored in arrays, it is possible to put subclass elements in the same array as elements of their parent class. In essence, arrays contain pointers, but in Java as in Smalltalk the programmer never has to manipulate pointers explicitly. Multidimensional arrays Multidimensional arrays are represented as arrays of arrays: int twodarray[][] = new int [256][16]; Since multidimensional arrays are allocated as arrays of arrays, they do not necessarily have to be rectangular. You may create an array with the last several dimensions unspecified, and later create rows that have different lengths: int triangular[][] = new int[4][]; triangular[0] = new int[1]; triangular[1] = new int[2]; triangular[2] = new int[3]; triangular[3] = new int[4]; These can also be created with nested initializers: int triangular[][] = {{0,{0,1,{0,1,2,{0,1,2,3; When an array reference is out of bounds, the ArrayIndexOutOfBounds exception is thrown. Arrays and methods 2002 Edward F. Gehringer CSC/ECE 517 Lecture Notes, Fall 2002 10

When an array is a parameter or a return value, empty brackets are placed after the type specification: public Shape[] translate(shape shapes[]) { In a called method, it is even possible to create an array, and return it from the method: class CreateDemo { public static void main(string args[]) { int [] box; box = create(4, 2, 17); System.out.println(box[2]); protected static int[] create(int subscript, int new_value) { int [] out_array; out_array = new int [length]; out_array[subscript] =new_value; return out_array; length, int What does this program print? Now let s look at the argument to procedure main( ) It is an array of Strings. This array contains the command-line arguments. This program displays all of the command-line arguments: public class Demonstrate { public static void main(string argv[]) { int counter; int max = argv.length; for (counter = 0; counter < max; ++counter) { System.out.println(argv[counter]); Lecture 14 Object-Oriented Languages and Systems 11

Collection classes in Java Java has standard collection classes, which often have something in common with their Smalltalk counterparts. They are found in java.util.*. Vectors: A vector is similar to a Smalltalk OrderedCollection. Like arrays, vectors are declared and then created: Vector v = new Vector(); Vectors can contain an element of class Object. This means that they can contain an instance of any class at all. To add elements to the rear of a vector, use the addelement method. v.addelement(e); An element can be inserted in a particular place using the insertelementat method. Arguments are an instance, and an integer index, saying where the instance is to be added. Thus, v.insertelementat(e, 0) inserts element e at the head of the vector. The converse, v.removeelementat is useful for deleting elements. v.removeelementat(0) There are also v.firstelement() and v.lastelement(). What methods can be used to represent a queue? What methods can you use to represent a stack? 2002 Edward F. Gehringer CSC/ECE 517 Lecture Notes, Fall 2002 12

Elements of primitive data types are not class instances, and they cannot be stored in vectors. There is a good reason for this. What is it? However, Java provides wrapper classes to allow primitive values to be converted back and forth to class instances. For ints, the wrapper is called Integer. An Integer instance is created the same way as any other instance: Integer i = new Integer(n); Here is how to store integers in a vector and read them back again: import java.util.*; class VectorDemo { public static void main(string args[]) { Vector v = new Vector(); v.addelement(new Integer(1)); v.addelement(new Integer(2)); Integer i = (Integer) v.lastelement(); System.out.println(i); What else do you notice about this program that we haven t seen before? Lecture 14 Object-Oriented Languages and Systems 13

Why is this necessary here? Well, we said that a vector contains instances of what type? So, when elements are fetched from vectors, an instance of what type is fetched? We need to convert this object into an instance of the class it was before it was put in the vector. Unlike in C and C++, casts in Java are legal only between instances in the same inheritance hierarchy, and are type-checked at compile time and run time. A cast can be used to narrow the type of a reference (make it something that s less general). That s what we needed to do with the Integer cast in the program above. Hashtables A hashtable is similar to a Smalltalk Dictionary. 1 Hashtable dates = new Hashtable(); dates.put("christmas", new Date("25 Dec 1996")); dates.put("independence", new Date("4 July 1997")); The first argument to put is the The second argument is the To get a value out, the get method is used: Date d = (Date) dates.get("christmas"); A null value is returned if no value is found for the specified key. Also, remove removes an element. 1 Actually, there is a class Dictionary in Java. It is an abstract superclass of Hashtable. 2002 Edward F. Gehringer CSC/ECE 517 Lecture Notes, Fall 2002 14

Summary containskey tells whether a particular key is found in the dictionary. Interfaces provide many of the benefits of multiple inheritance, while avoiding most of the costs. Packages and multiple compilation units make it possible to organize a large program. Characters and strings are similar to, but not identical to, those in C++. File streams are used for input and output. Utility classes such as tokenizers and print streams facilitate their use. The exception-handling mechanism of Java relies upon throwing exceptions from one place in the code to be dealt with by an exception-handler elsewhere. Arrays in Java are zero-based and created on the heap. Java has a set of collection classes, which differ in detail from Smalltalk s. Lecture 14 Object-Oriented Languages and Systems 15