1 / 16

CS 240 Week 3

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

peggy
Download Presentation

CS 240 Week 3

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. CS 240Week 3

  2. List’Em • java Grep [-r] directoryNamefileSelectionPatternsubstringSelectionPattern • Run Demo • java LineCount [-r] directoryNamefileSelectionPattern • Run Demo

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

  4. What is Different?

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

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

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

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

  9. 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: ^, $

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

  11. Class Object • Every class inherits directly or indirectly from the class Object • Methods • hashCode() • Equals • Overriding • instanceof checking • null checking • type conversion • toString()

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

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

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

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

  16. One-time Singletons • new T(…).m() • new T().run() • Example Code

More Related