/*-------------------------------------------------------------------------*/ /* +---------------+ | ImageFile | +---------------+ | String name | | int width | | int height | | String kind | +---------------+ */ //to represent an image file class ImageFile{ String name; int width; int height; String kind; ImageFile(String name, int width, int height, String kind){ this.name = name; this.width = width; this.height = height; this.kind = kind; } /* TEMPLATE: ... this.name ... ... this.name = ??? ... this.width ... ... this.width = ??? ... this.height ... ... this.height = ??? ... this.kind ... ... this.kind = ??? */ // is this imagefile the same as the given one? boolean same(ImageFile that){ return this.name.equals(that.name) && this.width == that.width && this.height == that.height && this.kind.equals(that.kind); } // design the method crop... void crop(int w, int h){ String s = "Do nothing."; } // design the method changeName ... } /*-------------------------------------------------------------------------*/ interface ISelect { /* Return true if this Object of the type T should be selected */ boolean select(ImageFile o); } /*-------------------------------------------------------------------------*/ /* Select image files smaller than 40000 */ class SmallImageFile implements ISelect { /* Return true if the size of the given ImageFile is smaller than 40000 */ boolean select(ImageFile o) { return o.height * o.width < 40000; } } /*-------------------------------------------------------------------------*/ //to represent a list of objects. interface AList{ // does this list contan the given object? boolean contains(ImageFile that); } //to represent an empty list of objects class MTList implements AList{ MTList(){} //does this empty list contain that object boolean contains(ImageFile that){ return false; } } //to represent a nonempty list of objects class ConsList implements AList{ ImageFile first; AList rest; ConsList(ImageFile first, AList rest){ this.first=first; this.rest=rest; } //does this non-emty list contain that object boolean contains(ImageFile that){ if (this.first.same(that)){ return true; } else { return this.rest.contains(that); } } } /*-------------------------------------------------------------------------*/ class Examples { // sample data to be used for all tests ImageFile pic1 = new ImageFile("dog", 300, 200, "jpg"); ImageFile pic2 = new ImageFile("cat", 200, 200, "png"); ImageFile pic3 = new ImageFile("bird", 250, 200, "jpg"); ImageFile pic4 = new ImageFile("horse", 300, 300, "giff"); ImageFile pic5 = new ImageFile("goat", 100, 200, "giff"); ImageFile pic6 = new ImageFile("cow", 150, 200, "jpg"); ImageFile pic7 = new ImageFile("snake", 200, 300, "jpg"); //empty list AList mt= new MTList (); // picture list -- all pictures AList piclistall = new ConsList (pic1, new ConsList (pic2, new ConsList (pic3, new ConsList (pic4, new ConsList (pic5, new ConsList (pic6, new ConsList (pic7, mt))))))); // picture list - short names (less than 4 characters) AList piclistshortnames = new ConsList (pic1, new ConsList (pic2, new ConsList (pic6, mt))); // picture list - small size (< 40000) AList piclistsmall = new ConsList (pic5, new ConsList (pic6, mt)); // picture list - small size (< 40000) AList piclistsmall2 = new ConsList (pic5, new ConsList (pic6, mt)); // picture list -- large pictures AList piclistlarge = new ConsList (pic1, new ConsList (pic2, new ConsList (pic3,mt))); }