















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lecture from Object Oriented Programming and Data Structures course with following key points: Array List and Hash Set, Generic Programming, Simplifies Programming, Strings, Vector, Interface Collection, Parsing Arithmetic Expressions
Typology: Slides
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















ge·ner·ic adjective \jə̇ ˈnerik, - rēk
relating or applied to or descriptive of all members of a genus, species, class, or group: common to or characteristic of a whole group or class: typifying or subsuming: not specific or individual. From Wikipedia: generic programming: a style of computer programming in which algorithms are written in terms of to-be- specified-later types that are then instantiated when needed for specific types provided as parameters. In Java: Without generics, every Vector object contains a list of elements of class Object. Clumsy With generics, we can have a Vector of Strings, a Vector of Integers, a Vector of Genes. Simplifies programming, guards against some errors
ge·ner·ic adjective \jə̇ ˈnerik, - rēk
relating or applied to or descriptive of all members of a genus, species, class, or group: common to or characteristic of a whole group or class: typifying or subsuming: not specific or individual. From Wikipedia: generic programming: a style of computer programming in which algorithms are written in terms of to-be- specified-later types that are then instantiated when needed for specific types provided as parameters. In Java: Without generics, every Vector object contains a list of elements of class Object. Clumsy With generics, we can have a Vector of Strings, a Vector of Integers, a Vector of Genes. Simplifies programming, guards against some errors
HashSet s= new HashSet(); HashSet@y 2 Hashset Object Fields that contain a setof objects {o 0 , o 1 , …, o size()- 1
HashSet() add(Object) contains(Object) size() remove(Object) … s HashSet@y 2 HashSet An object of class HashSet contains a growable/shrinkable set of elements (of class Object). You can get the size of the set, add an object to the set, remove an object, etc. More methods exist! Look at them! Don’t ask what “hash” means. Just know that a Hash Set object maintains a set
HashSet s= new HashSet(); … code to store values in the set … for (Object e : s) { System.out.println(c); } HashSet@y 2 HashSet Object Fields that contain a setof objects {o 0 , o 1 , …, o size()- 1
HashSet() add(Object) contains(Object) size() remove(Object) … s HashSet@y 2 HashSet A loop whose body is executed once with e being each element of the set. Don’t know order in which set elements processed Use same sort of loop to process elements of an ArrayList in the order in which they are in the ArrayList.
API specs: ArrayList declared like this: public class ArrayList
API specs: Vector declared like this: public class Vector
Interface Collection: abstract methods for dealing with a group of objects (e.g. sets, lists) Abstract class AbstractCollection: overrides some abstract methods with methods to make it easier to fully implement Collection AbstractList, AbstractQueue, AbstractSet, AbstractDeque overrides some abstract methods of AbstractCollection with real methods to make it easier to fully implement lists, queues, set, and deques Next slide contains classes that you should become familiar with and use. Spend time looking at their specifications. There are also other useful Collection classes
ArrayList extends AbstractList: An object is a growable/shrinkable list of values implemented in an array Arrays: Has lots of static methods for dealing with arrays —searching, sorting, copying, etc. HashSet extends AbstractSet: An object maintains a growable/shrinkable set of values using a technique called hashing. We will learn about hashing later. LinkedList extends AbstractSequentialList: An object maintains a list as a doubly linked list Stack extends Vector: An object maintains LIFO (last-in- first-out) stack of objects Vector extends AbstractList: An object is a growable/shrinkable list of values implemented in an array. An old class from early Java
AbstractList AbstractCollection Object List Collection Iterable List Collection Iterable Collection Iterable Iterable Not discussed today Interface List: abstract methods for dealing with a list of objects (o 0 , …, on- 1 ). Examples: arrays, Vectors Abstract class AbstractList: overrides some abstract methods with real methods to make it easier to fully implement List Homework: Look at API specifications and build diagram giving format of HashSet
We show you a real grammar for arithmetic expressions with integer operands; operations +, - , *, /; and parentheses ( ). It gives precedence to multiplicative operations. We write a recursive descent parser for the grammar and have it generate instructions for a stack machine (explained later). You learn about infix, postfix, and prefix expressions. Introduced in lecture briefly, to show use of grammars and recursion. Done more thoroughly and carefully here. Historical note: Gries wrote the first text on compiler writing, in
(5 + 6) * (4 – 3) Infix 5 6 + 4 3 - * Postfix 5 + 6 * 3 Infix 5 6 3 * + Postfix Math convention: * has precedence over +. This convention removes need for many parentheses Task: Write a parser for conventional arithmetic expressions whose operands are ints.
A
3 + 2
A
Initialized to a String that contains an arithmetic expression. Delivers the tokens in the String, one at a time
Expression : 3445 *( 20 + 16 ) Tokens: 3445
( 20
16 ) All parsers use a scanner, so they do not have to deal with the input character by character and do not have to deal with whitespace