1 / 92

Data Structures, Algorithms, & Applications

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.

jafari
Download Presentation

Data Structures, Algorithms, & Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures, Algorithms, & Applications John Barr

  2. 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.

  3. 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.

  4. Prerequisites • Java • Asymptotic Complexity • Big Oh, Theta, and Omega notations

  5. Handouts, syllabus, text, source codes, assignments, past exams, etc. • My office data. Web Site • www.ithaca.edu/~barr/Student/CS311

  6. Do Problem Set 1 by next week. Assignments • Submission procedures • Assignment guidelines

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

  8. Grades • 25% for problem sets/programs (~10) • 40% for tests (3 tests; see web) • 30% for projects • 5% participation

  9. 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

  10. Java Topics: • Packages • Inheritance • Method Overriding • Exceptions • Generic Methods • Interfaces, Abstract Classes

  11. Java Exception Classes: • EmptyQueueException • MyInputException • UndefinedMethodException

  12. 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.

  13. 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. */

  14. MyInputException package exceptions; public class MyInputException extends RuntimeException { public MyInputException() {super();} public MyInputException(String message) {super(message);} }

  15. MyInputStream Catch exceptions of type IOException thrown by Java’s input methods Throws exceptions of type MyInputException

  16. Generic Methods • Uses an interface class • Is not an interface itself • Can accept sub-classes that implement an interface class.

  17. Generic Methods • Not possible for Java primitive types. • Use with subclasses of type Object • Must write wrapper classes for primitive types.

  18. 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; }

  19. 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; }

  20. 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));} }

  21. 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));} }

  22. 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.

  23. 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)

  24. 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.

  25. 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);

  26. 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(); }

  27. 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)); } }

  28. 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);

  29. 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); }

  30. The Operable Interface /** Marker interface for classes that are * both Computable and Comparable. */ package utilities; public interface Operable extends Computable, Comparable {}

  31. 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(); }

  32. The CloneableObject Interface Cloning vs copying: X = new currancy(3.42); Y = x; y.setValue(6.25); System.out.println(x); >> $6.25 Why?

  33. The CloneableObject Interface X = new currancy(3.42); x 3.42

  34. The CloneableObject Interface X = new currancy(3.42); x y Y = x; 3.42

  35. The CloneableObject Interface X = new currancy(3.42); x y Y = x; y.setValue(6.25); 3.42 6.25

  36. 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

  37. 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();}

  38. Wrapper Classes • Built-in in Java • Redefined in book to use book’s interfaces.

  39. 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;

  40. 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);}

  41. 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);}

  42. Integer Wrapper Class // Computable interface methods /** @return this + x */ public Object add(Object x) { return new MyInteger (value + ((MyInteger) x).value); }

  43. 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; }

  44. Integer Wrapper Class // override Object.equals /** @return true iff this == x */ public boolean equals(Object x) {return value == ((MyInteger) x).value;}

  45. 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.

  46. 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;

  47. 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)

  48. Methods as paramters package misc; import java.lang.reflect.*; import wrappers.*; import utilities.*; public class Array1D { // instance data member Object [ ] a;

  49. 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.

  50. 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.

More Related