930 likes | 1.05k Vues
Data Structures, Algorithms, & Applications. John Barr. What The Course Is About. Data structures is concerned with the representation and manipulation of data. All programs manipulate data. So, all programs represent data in some way. Data manipulation requires an algorithm.
E N D
What The Course Is About • Data structures is concerned with the representation and manipulation of data. • All programs manipulate data. • So, all programs represent data in some way. • Data manipulation requires an algorithm.
The study of data structures and algorithms is fundamental to Computer Science. What The Course Is About • Algorithm design methods needed to develop programs that do the data manipulation.
Prerequisites • Java • Asymptotic Complexity • Big Oh, Theta, and Omega notations
Handouts, syllabus, text, source codes, assignments, past exams, etc. • My office data. Web Site • www.ithaca.edu/~barr/Student/CS311
Do Problem Set 1 by next week. Assignments • Submission procedures • Assignment guidelines
Source Codes • Read download and use instructions. • Must have Java 1.2 or higher. • See ProgramIndex.htm, AllNames.html and other html files produced by Javadoc for Java codes. • I will use the java 2 JDK. You can develop in anything, but it must run in the JDK.
Grades • 25% for problem sets/programs (~10) • 40% for tests (3 tests; see web) • 30% for projects • 5% participation
Organization of Text • Three parts • Part I … Chapters 1-4, Background • Part 2 … Chapters 5-17, Data Structures • Part 3 … Chapters 18-22, Algorithms • Each chapter … concepts + applications
Java Topics: • Packages • Inheritance • Method Overriding • Exceptions • Generic Methods • Interfaces, Abstract Classes
Java Exception Classes: • EmptyQueueException • MyInputException • UndefinedMethodException
MyInputException Thrown by methods of class MyInputStream, defined in the book. Java’s standard input methods throw exceptions that are subclasses of IOException. Thus must be declared in the throws clause of all methods that use std input. MyInputException is a subclass of RunTimeException. Do not have to catch these exceptions.
MyInputException /** This exception is thrown when an input * exception occurs. It is used by MyInputStream * to convert checked exceptions such as * FileNotFoundException and EOFException * into unchecked runtime exceptions. */
MyInputException package exceptions; public class MyInputException extends RuntimeException { public MyInputException() {super();} public MyInputException(String message) {super(message);} }
MyInputStream Catch exceptions of type IOException thrown by Java’s input methods Throws exceptions of type MyInputException
Generic Methods • Uses an interface class • Is not an interface itself • Can accept sub-classes that implement an interface class.
Generic Methods • Not possible for Java primitive types. • Use with subclasses of type Object • Must write wrapper classes for primitive types.
Generic Methods /** Swap the integers a[i] and a[j]. */ public static void swap(int [] a, int i, int j) { // Don't bother to check that indexes i and j // are in bounds. Java will do this and throw // an ArrayIndexOutOfBoundsException if i or // j is out of bounds. int temp = a[i]; a[i] = a[j]; a[j] = temp; }
Generic Methods /** Generic method to swap the object * references a[i] and a[j]. */ public static void swap(Object [] a, int i, int j) { Object temp = a[i]; a[i] = a[j]; a[j] = temp; }
Generic Methods package misc; public class Abc { public static int abc(int a, int b, int c) {return a+b+b*c+(a+b-c)/(a+b)+4;} public static void main(String [] args) {System.out.println(abc(2,3,4));} }
Generic Methods package misc; public class Abc { public static float abc(float a, float b, float c) {return a+b+b*c+(a+b-c)/(a+b)+4;} public static void main(String [] args) {System.out.println(abc(2,3,4));} }
Generic Methods Problem with making a generic abc method: • Not all objects can + or * or / • Need to restrict the objects that the generic method accepts.
Interfaces • A list of 0 or more static final data members (constants) • And 0 or more method headers with no implementation. • A class can implement 0 or more interfaces, but can only extend one class (or one abstract class)
Interfaces • Can define methods that have formal parameters whose data type is an interface • Cannot create instances of an interface • Instances of a class that implements an interface may be cast into the data type of the interface.
The Computable Interface package utilities; /** Interface to be implemented by all classes * that permit the standard arithmetic operations. */ public interface Computable { /** @return this + x */ public Object add(Object x); /** @return this - x */ public Object subtract(Object x); /** @return this * x */ public Object multiply(Object x);
The Computable Interface (cont) /** @return quotient of this / x */ public Object divide(Object x); /** @return remainder of this / x */ public Object mod(Object x); /** @return this incremented by x */ public Object increment(Object x); /** @return this decremented by x */ public Object decrement(Object x); /** @return the additive zero element */ public Object zero(); /** @return the multiplicative identity element */ public Object identity(); }
Generic Method abc package misc; import wrappers.*; import utilities.*; public class GenericAbc { public static Computable abc(Computable a, Computable b, Computable c) { Computable t = (Computable) a.add(b.multiply(c)); return (Computable) t.add(b.divide(c)); } public static void main(String [] args) { MyInteger x, y, z; x = new MyInteger(2); y = new MyInteger(3); z = new MyInteger(4); System.out.println(abc(x, y, z)); } }
The MyComparable Interface package utilities; /** Interface to be implemented by all classes that permit comparison between their objects. Comparable has only one method. */ public interface MyComparable extends Comparable { /** @return -1 if this < x, * 0 if this == x, * 1 if this > x */ public int compareTo(Object x); /** @return true iff this < x */ public boolean lessThan(Object x); /** @return true iff this <= x */ public boolean lessThanOrEqual(Object x);
The MyComparable Interface /** @return true iff this > x */ public boolean greaterThan(Object x); /** @return true iff this >= x */ public boolean greaterThanOrEqual(Object x); /** @return true iff this == x */ public boolean equals(Object x); }
The Operable Interface /** Marker interface for classes that are * both Computable and Comparable. */ package utilities; public interface Operable extends Computable, Comparable {}
The Zero Interface package utilities; /** Interface to be implemented by all classes * that implement provide a zero value and test for zero. */ public interface Zero { public Object zero(); public boolean equalsZero(); }
The CloneableObject Interface Cloning vs copying: X = new currancy(3.42); Y = x; y.setValue(6.25); System.out.println(x); >> $6.25 Why?
The CloneableObject Interface X = new currancy(3.42); x 3.42
The CloneableObject Interface X = new currancy(3.42); x y Y = x; 3.42
The CloneableObject Interface X = new currancy(3.42); x y Y = x; y.setValue(6.25); 3.42 6.25
The CloneableObject Interface X = new currancy(3.42); Y = x.clone(); y.setValue(6.25); System.out.println(x); x x y x y 3.42 3.42 3.42 3.42 6.25
The CloneableObject Interface package utilities; /** Interface to be implemented by all classes * that implement the method clone. */ public interface CloneableObject extends Cloneable {public Object clone();}
Wrapper Classes • Built-in in Java • Redefined in book to use book’s interfaces.
Integer Wrapper Class /** wrapper class for int */ package wrappers; import utilities.*; import exceptions.*; public class MyInteger implements Operable, Zero, CloneableObject { // value of the integer private int value;
Integer Wrapper Class // constructor methods /** MyInteger initialized to theValue */ public MyInteger(int theValue) {value = theValue;} /** MyInteger initialized to 0 */ public MyInteger() {this(0);} /** MyInteger initialized to s */ public MyInteger(String s) throws NumberFormatException {value = Integer.parseInt(s);}
Integer Wrapper Class(partial) /** input from the given input stream */ public static MyInteger input(MyInputStream stream) { System.out.println("Enter an integer value"); return new MyInteger(stream.readInteger()); } /** make a clone */ public Object clone() {return new MyInteger(value);}
Integer Wrapper Class // Computable interface methods /** @return this + x */ public Object add(Object x) { return new MyInteger (value + ((MyInteger) x).value); }
Integer Wrapper Class // Comparable interface method /** @return -1 if this < x, * 0 if this == x, * 1 if this > x */ public int compareTo(Object x) { int y = ((MyInteger) x).value; if (value < y) return -1; if (value == y) return 0; return 1; }
Integer Wrapper Class // override Object.equals /** @return true iff this == x */ public boolean equals(Object x) {return value == ((MyInteger) x).value;}
Data Types and Methodsas paramters Example. Class whose instances are 1-D arrays. Method InputArray inputs n items, creates array of size n and stores elements into array. Problem: data type of elements is unknown until run time.
Data Types and Methodsas paramters Solution: make the data type a parameter, theClass. Data type of theClass is Class (defined in java.lang) Obtain a type by: Class typeOfInt = int.class; Class typeofMyInteger = MyInteger.class;
Data Types and Methodsas paramters Other information: • Where to get data from (stnd input, file, etc) • How to input a type (e.g., to input an element of type Currency must read a sign, a dollar value, a cents value) Solution: • Pass in input stream as parameter • Require each data type implement a public static method input(MyInputStream stream)
Methods as paramters package misc; import java.lang.reflect.*; import wrappers.*; import utilities.*; public class Array1D { // instance data member Object [ ] a;
Methods as paramters To get a reference to the input method of the desired class use Java.lang.Class.getMethod(String name, Class parameterTypes) Returns an object of type Method (defined in java.lang.reflect) Name: name of method we are looking for parameterTypes: signature of method Name.
Methods as paramters To invoke the returned method, must use the class function Method.invoke(instance, actual parameters) Use “null” for first parameter if method invoking is static. Second parameter must be an array of parameters. getMethod and invoke throw exceptions that are not of a type that is a subclass of runtimeException, thus must catch or declare a throws.