Computer Science 313 – Advanced Programming Topics.

Slides:



Advertisements
Similar presentations
Reusable Classes.  Motivation: Write less code!
Advertisements

Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
Computer Science 313 – Advanced Programming Topics.
CSC 212 – Data Structures Lecture 10: More Inheritance & Exceptions.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
CSC 212 – Data Structures Lecture 13: Linked Lists.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Java File Structure.  File class which is defined by java.io does not operate on streams  deals directly with files and the file system  File class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Computer Science and Engineering College of Engineering The Ohio State University Lot More Inheritance and Intro to Design Patterns Lecture 12.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
1 CSC 221: Computer Programming I Spring 2008 ArrayLists and arrays  example: letter frequencies  autoboxing/unboxing  ArrayLists vs. arrays  example:
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
CS2110: SW Development Methods Inheritance in OO and in Java Part 1: Introduction Readings: A few pages in Ch. 2 of MSD text introduce this Section 3.3.
CPSC 217 T03 Week I Part #1: Unix and HELLO WORLD Hubert (Sathaporn) Hu.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance Dwight Deugo Nesa Matic
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Name of Presentation Red Hat Presenter Mobicents SIP Presence Service: XDM Server Creating XCAP Application Usages Eduardo Martins.
CSC 212 – Data Structures Lecture 37: Course Review.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Computer Science Projects Internal Assessment. Mastery Item Claimed Justification Where Listed Random Access File – Searching Lines P. 53 Random.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Data Structures and Java CS /14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:
M1G Introduction to Programming 2 5. Completing the program.
IBM TSpaces Lab 2 Customizing tuples and fields. Summary Blocking commands Tuple Expiration Extending Tuples (The SubclassableTuple) Reading/writing user.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
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,
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Computer Science 313 – Advanced Programming Topics.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Computer Science 313 – Advanced Programming Topics.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
Geoff Holmes Week 5 problems Handling Throwing Catching Multiple exceptions Finally clause Examples Exception Handling.
Chapter 5 Hierarchies IS-A associations superclasses subclasses
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS2102: Lecture on Abstract Classes and Inheritance
Abstract Classes.
Week 6 Object-Oriented Programming (2): Polymorphism
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Exceptions 10-May-19.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

Computer Science 313 – Advanced Programming Topics

 Writing a Java-based file manager  Starts at drive, but drill-down into directories  Considers only directories & files (for now)  Named required for all directory entries  Print out the names of directories & files  Print out files sizes as we traverse directories  Allow user to create & delete files  Remain true to our basic nature

 Writing a Java-based file manager  Starts at drive, but drill-down into directories  Considers only directories & files (for now)  Named required for all directory entries  Print out the names of directories & files  Print out files sizes as we traverse directories  Allow user to create & delete files  Remain true to our basic nature

Composite Pattern Classes  Create abstract superclass for the pattern  This class normally used by client code  Common methods & fields defined in superclass  Should also declare any abstract methods needed  Other classes subclass of abstract superclass  Decorator-like parallel hierarchies not required  Subinterfaces not required & almost never used  Could create hierarchies as needed  But consider composition vs. inheritence

Composite Code Inside public abstract class OSEntry { public String name; public abstract String toString(); public void createFile(String fileName) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } public void createDir(String dirName) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } }

Directories Contain…?  Currently, each directory has lots of files  Will need a field to store these references  Could use an array or some type of List  Question is: what type should List hold?  List could simply store references to File  Unable to hold new types of entries we create  Class is forever closed to any type of extension  Containers must refer to abstract superclass

Woot! Pimped-Out Recursion!

Container Code public class Directory extends OSEntry { public List contents; public void createFile(String fName) { String newFile = new File(getName(), fName); contents.add(newFile); } public void createDir(String dName) { String dir = new Directory(getName(), dName); contents.add(dir); }

File Methods…  Files should not be able to create files & dirs  Methods declared previously, so that is good  Even better, already throw documented exception  Useful trick when option may/may not be possible  Should be careful about too many exceptions  Should be able to get size of a file  Most classes should not provide this method  Instead, we will not declare this in the superclass  Clients must have a File before calling method

File Code public class File extends OSEntry { public java.util.File fileInOS; public String toString() { return getName(); } public long getSize() { return fileInOS.getLength(); } }

Client Code OSEntry entry; System.out.println(entry.toString()); if (entry instanceof File) { System.out.println(“Size is :” + ((File)entry).getSize()); } else { System.out.println(“No size, bozo!”); }

For Next Class  Lab available on the web  Lab will be due 2 weeks from Friday  No class this Friday  Some percentage of class with me in Hartford, CT  Use time wisely – work on lab, study for the test #2  Test #2 in class on Monday  Can include any and all material since last test  Patterns & optimizations fair game to ask about  Open-note, open-book, open-template, but closed slide