DOC PREVIEW
USC CSCI 455x - Code Handout

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Code Handout to accompany final exam CSCI 455 [S16 Bono] 1 (C++) Node type and ListType (this is the only part of the code handout with C++ code): struct Node { int data; Node * next; Node(int item); Node(int item, Node * n); }; typedef Node * ListType; (Java) Selected methods of Point class: Note: point objects are mutable, and they are not Comparable (i.e., they do not implement Comparable interface). Point p = new Point(x, y) create a point with coordinates (x, y) Point p = new Point(p2) create a point with the same coordinates as p2 p.getX() p.getY() returns x and y coordinates of p, respectively. p.translate(dx, dy) Changes x and y values of p by dx and dy, respectively. I.e., if p had coordinates (x, y), its new value is a point with coordinates (x+dx, y+dy) p.setLocation(x, y) Moves point to location (x,y). p.hashCode() A hash function for Point . (Overridden from Object.) p.equals(p2) Returns true iff p and p2 have the same x and y coordinates. (Overridden from Object.) (Java) Reminder of syntax to implement an interface (using Comparator<Type> interface) class StringLengthComparator implements Comparator<String> { public int compare(String s1, String s2) { return s2.length() – s1.length(); } } (Java) Collections class (selected methods) The Collections class contains static methods that operate on collections. Note: ArrayList and LinkedList both implement the List interface used below. static void sort(List<ElmtType> list) Sorts the list into ascending order according to the natural ordering of its elements (i.e., using compareTo). static void sort(List<ElmtType> list, Comparator<ElementType> c) Sorts the list according to the order specified by the comparator. [continued other side]Code Handout to accompany final exam CSCI 455 [S16 Bono] 2 (Java) Map<KeyType, ValueType> Interface (selected methods) The classes that implement this interface are: TreeMap and HashMap. Selected methods: ValueType put(key, value) Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced by the specified value. Returns the previous value associated with specified key, or null if there was no mapping for key. ValueType get(key) Returns the value to which this map maps the specified key or null if the map contains no mapping for this key. boolean containsKey(key) Returns true iff the map contains a mapping for the specified key. ValueType remove(key) Removes the mapping for this key from this map if it is present, otherwise returns null. int size() Number of key-value mappings in this map. boolean isEmpty() Returns true if this map contains no key-value mappings. Set<Map.Entry<KeyType,ValueType>> entrySet() Returns a set view of the entries contained in this map. (Java) Map.Entry<KeyType, ValueType> Interface KeyType getKey() Return the key of the entry ValueType getValue() Return the value of the entry void setValue(newVal) Replace the current value with newVal (Java) Collection<ElmtType> Interface (selected methods) Some classes that implement this interface are: ArrayList, LinkedList, TreeSet, and HashSet. boolean contains(elmt) Returns true iff elmt is in this collection int size() Returns number of elements in this collection boolean add(elmt) Ensures that elmt is in this collection. Returns true iff this collection changed as a result of this call boolean remove(elmt) Removes an instance of elmt from this collection. Returns true iff this collection changed as a result of this call boolean isEmpty() Returns true iff this collection contains no elements. Iterator<ElmtType> iterator() Returns an iterator over the elements in this collection. (Java) Iterator<ElmtType> Interface Some classes that implement this interface are: Scanner, ListIterator: boolean hasNext() Returns true iff the iteration has more elements. ElmtType next() Returns the next element in the iteration. Each successive call returns a different element in the underlying collection. For Scanner the ElmtType is always String. void remove() Removes from the underlying collection the last element returned by the iterator. (Scanner does not implement this optional


View Full Document

USC CSCI 455x - Code Handout

Download Code Handout
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Code Handout and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Code Handout 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?