Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240 Week 3. List’Em java Grep [-r] directoryName fileSelectionPattern substringSelectionPattern Run Demo java LineCount [-r] directoryName fileSelectionPattern.

Similar presentations


Presentation on theme: "CS 240 Week 3. List’Em java Grep [-r] directoryName fileSelectionPattern substringSelectionPattern Run Demo java LineCount [-r] directoryName fileSelectionPattern."— Presentation transcript:

1 CS 240 Week 3

2 List’Em java Grep [-r] directoryName fileSelectionPattern substringSelectionPattern Run Demo java LineCount [-r] directoryName fileSelectionPattern 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? GrepLineCount Keeps track of total lines matched in all qualifying files and prints the value at the end. Keeps track of total lines in all files matched and prints the value at the end. When processing a file, if it finds at least one line that contains a substring matching the substringSelectionPattern, it prints the file name and each line that matches the substringSelectionPattern. When processing a file it keeps track of the total number of lines matched, printing the result when it reaches the end of the file that contains at least 1 matched line. When processing a file it keeps track of the number of lines in the file, printing the result, along with the file name, when reaching the end of the file. Command line syntax is different Output Syntax is different.Output syntax 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) Person.java – Faculty.java: A subclass of Person.java Faculty.java – Student.java: A subclass of Person.java Student.java GradStudent.java: A subclass of Student.java GradStudent.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 – boolean m.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, C 1 … C n,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 C 1 … C n create 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 t3.txt – t4.java t4.java t1.txt t2.java – chmod 000 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


Download ppt "CS 240 Week 3. List’Em java Grep [-r] directoryName fileSelectionPattern substringSelectionPattern Run Demo java LineCount [-r] directoryName fileSelectionPattern."

Similar presentations


Ads by Google