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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 240 Week 2. Introduction to Project 2 Command Line Syntax java Spell word -- source file hard wired into program Run Demo.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 10 Classes Continued
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS1101: Programming Methodology Aaron Tan.
Inheritance in the Java programming language J. W. Rider.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance and Access Control CS 162 (Summer 2009)
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes
CS 240 Week 2.
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CS240: Advanced Programming Concepts
Overloading and Constructors
Interface.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Packages and Interfaces
Interfaces.
Advanced Java Programming
Java Inheritance.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Review for Midterm 3.
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

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

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.

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

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

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

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 t3.txt – t4.java t4.java t1.txt t2.java – chmod 000 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