DOC PREVIEW
USC CSCI 455x - Code Handout

This preview shows page 1 out of 2 pages.

Save
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

Unformatted text preview:

Code Handout to accompany final exam Digits class interface from problem 1 CSCI 455 S13 Bono public class Digits create Digits version of num PRE num 0 public Digits int num gets digit i of the number such that digit 1 is the most significant digit and digit numDigits is the least significant digit PRE 1 i numDigits public int getDigit int i the number of digits in the number public int numDigits returns the integer as a whole public int getInt ArrayList ElmtType class selected methods ArrayList Integer arrList new ArrayList Integer create empty arraylist that can hold ints arrList add 3 add a value to the end of the arrList int num arrList get i returns element in in arrList int howMany arrList size number of elements in arrList boolean empty arrList isEmpty true iff arrList has no elements 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 Comparator Type interface An object that can compare two objects of type Type Has one method defined by the interface int compare Type object1 Type object2 Must return a negative number if object1 should come before object2 0 if object1 and object2 are equal or a positive number if object1 should come after object2 Comparable Type interface An object that can be compared to another object of the same type Has one method defined by the interface int compareTo Type other a compareTo b must return a negative number if a should come before b 0 if a and b are equal and a positive number otherwise Some Java classes that are Comparable String Integer Double The Java class Point is not Comparable More other side 1 Code Handout to accompany final exam CSCI 455 S13 Bono Point class selected methods The Point class is a subclass of the abstract class Point2D Note the x and y coordinates are type int in all of the following new Point x y Constructs point object with given x and y values new Point p2 Constructs point object that has the same value as point p2 p translate dx dy Changes x and y values of p by dx and dy respectively I e if p had coordinates x y it s new value is a point with coordinates x dx y dy p getX Returns the x coordinate of p p getY Returns the y coordinate of p Point2D class selected methods Abstract superclass of Point class p distance p2 Returns the distance a double from Point2D p to a specified Point2D p distance double px double py Returns the distance a double from Point2D p to a specified point p distanceSq p2 Returns the square of the distance a double from Point2D p to a specified Point2D p distanceSq double px double py Returns the square of the distance a double from Point2D p to a specified point C Node type and ListType this is the only part of the code handout with C code struct Node int data Node next Node data 0 next NULL Node int d data d next NULL Node int d Node n data d next n typedef Node ListType More other side 2


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 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?