Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 313 – Advanced Programming Topics.

Similar presentations


Presentation on theme: "Computer Science 313 – Advanced Programming Topics."— Presentation transcript:

1 Computer Science 313 – Advanced Programming Topics

2  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

3  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

4 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

5 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(); } }

6 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

7 Woot! Pimped-Out Recursion!

8 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); }

9 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

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

11 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!”); }

12 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


Download ppt "Computer Science 313 – Advanced Programming Topics."

Similar presentations


Ads by Google