110 likes | 230 Views
Algorithms. Week 1 Lab. Hobbes. Does everyone have an account on hobbes? Can we log on?. Java. Has everyone had experience programming in Java? Programming assignments in this class will be in Java. Inheritance. Class A { int x; int y; }. Class B extends A { int z; }.
E N D
Algorithms Week 1 Lab
Hobbes • Does everyone have an account on hobbes? • Can we log on?
Java • Has everyone had experience programming in Java? • Programming assignments in this class will be in Java.
Inheritance Class A { int x; int y; } Class B extends A { int z; } main() { A aInstance; B bInstance; aInstance.x = 2; bInstance.z = 3; bInstance.y = 4; aInstance.z = 6; }
Java Inheritance Stream InputStream OutputStream . . . FileInputStream . . .
Java Interfaces • Multiple inheritance • Like a contract or an agreement • Classes agree to fill all the requirements of the interface. • Classes that do so can share in the interface’s name.
Java Interfaces • Abstract method declarations • public abstract String getName() • Just has a return type and a parameters list. • To implement this part of the interface: • A class must provide a method getName • With no parameters • That returns a String
Our Test Environment • All your programming assignments will use this test environment. • You interact with it by implementing interfaces. • Our environment source code: • SortingAlgorithm sAlg; • Your class file: • public class MySortingAlgorithm implements SortingAlgorithm { ... } • If your class implements SortingAlgorithm properly, this will work properly. • If you implement it improperly, your class won’t compile.
Java Interfaces • Example: Algorithm interface • http://www.seas.gwu.edu/~simhaweb/cs151/hw/ex1/Algorithm.html • This is a superclass for all the interfaces we will use. • Any assignment you turn in needs to have these two methods. • String getName() • void setPropertyExtractor(int algID, PropertyExtractor prop)
Java Interfaces • SortingAlgorithm interface • Methods from Algorithm • String getName() • void setPropertyExtractor(int algID, PropertyExtractor prop) • New methods • int[] createSortIndex(int[] data) • void sortInPlace(int[] data) • int[] createSortIndex(Comparable[] data) • void sortInPlace(Comparable[] data) • You need to have something for each of these methods. • If we tell you not to implement some part of the interface, you still need a stub for that method!! • {} (no body) or {return void;} or something.
Notes • Prof. Simha: • http://www.seas.gwu.edu/~simhaweb/cs151/ • Prof. Youssef: • http://www.seas.gwu.edu/~ayoussef/cs212/