160 likes | 246 Views
CS 240 Week 3. List’Em. java Grep [-r] directoryName fileSelectionPattern substringSelectionPattern Run Demo java LineCount [-r] directoryName fileSelectionPattern Run Demo. What is the Same?.
E N D
List’Em • java Grep [-r] directoryNamefileSelectionPatternsubstringSelectionPattern • Run Demo • java LineCount [-r] directoryNamefileSelectionPattern • Run Demo
What is the Same? • Errors are the same: directoryName is not a directory, directoryName is a directory but not readable, a selected file in a direcitory is not readable, command line is not of proper syntax • If the –r option is present the program will recursively traverse folders starting from the one with the directoryName • There is some form of initialization required at the beginning of the program • There is some form of initialization required when processing each file • When the program processes a file, it opens the file and reads each line one at a time and processes the line • When the program finishes processing a file it may generate additional output • Each program generates a total of some kind (though the output syntax is different)
Inheritance • Syntax • Keyword: extends • Fields • Use of public, private, protected, and package scope • Constructors • Use of super() • Must be first line • Defaults • Default constructor • What if there is not constructor • Final • Classes • Methods • Variables – can only be initialized once • “blank final” variables are initialized in constructor • Abstract classes • Keyword abstract • Method with no body • Similar to .h files
Inheritance • Methods • Methods are virtual • public, private, protected, package scope • Use of super – can be at any line • Overriding • Must be same signature (see boolean equals(Object o))
Interfaces • Answer to multiple inheritance • Interaction contract • Can have variables of the interface type • Value in variables must be instance of implementing class • Keyword “implements” • Like .h file in concept (conceptually similar to abstract class) • Contains • Constants (static and final) • Method signatures
Inheritance/Interface Example • Person.java: The root class (a super class) • Faculty.java: A subclass of Person.java • Student.java: A subclass of Person.java • GradStudent.java: A subclass of Student.java • Main.java
The Java Pattern Class • java.util.regex.Pattern • Regular expression syntax • Each character stands for itself (except \) • Metacharacters • Normal: [,\,^,$,.,|,?*+,(,) • In square brackets: [,\,^, • - is also special when indicating a range • Special characters (\\, \t, \n, \r, \f, \a, \e, \cx) • Classes: [abc], [^abc], [a-zA-Z], and more • Predefined classes: ., \d, \D, \s, \S, \w, \W • \p{Alpha} , \p{Upper}, \p{Punct} • Boundary matchers: ^, $
Using Java Regular Expressions • Pattern p = Pattern.compile(regularExpression) • Notice use of static method • Use of \\ • “\\p{Alpha}+” • Matcher m = p.matcher(“”) • m.reset(String to apply matcher to) • The input string is usually a line from an input file • booleanm.find () //finds all matches to the regular expression • See example • String m.group() //returns the substring matched by the matcher • Example Code • Play With Patterns
Class Object • Every class inherits directly or indirectly from the class Object • Methods • hashCode() • Equals • Overriding • instanceof checking • null checking • type conversion • toString()
Template Method Pattern • 2 or more classes, C1 … Cn,with common fields and algorithms but some aspects are different • Create a single super class containing common fields, constructors, and algorithms • Methods may call other methods that are overridden in the sub-classes • For C1 … Cncreate a class that extends the superclass. • Add any additional fields • Move all of the differences in this class • It most cases the differences go into overridden constructors and methods.
The File Class • File name and path name differences in Windows, Mac, and Linux • A “File” hides these differences” • The constructor: File(String fileName) • absolute paths names • relative path names • Methods • isDirectory() • canRead() • listFiles() – if this “file” is a directory • isFile() • Used as input parameter to constructor of FileInputStream
Example Code for the File Class • FileType.java • sampleDirectory //interogatted in FileType.main • t3.txt • t4.java • t1.txt • t2.java • chmod000 t2.java and run program again
Assertions • Design by Contract • pre-condition • post-condition • Syntax: • assert boolean-expression; • Can be used on any line • Proof of correctness • Frequently used to check pre-conditions and post-conditions • Different from exceptions • java –ea command line option needed • Assertion Example
One-time Singletons • new T(…).m() • new T().run() • Example Code