//Lab 2 solution set /* +-----------------+ +--------------+ | Photographer | +->| Camera | +-----------------+ | +--------------+ | String name | | | int roll | | Camera myCamera |--+ | boolean isBW | | Snapshot pic |--+ +--------------+ +-----------------+ | | +-----------------+ | | Snapshot |<-+ +-----------------+ | String contains | | int width | | int height | +-----------------+ */ class Photographer { String name; Camera myCamera; Snapshot pic; Photographer(String name, Camera myCamera, Snapshot pic) { this.name = name; this.myCamera = myCamera; this.pic = pic; } //new Photographer("Fred", new Camera(23,false), new Snapshot("kitten",3,4)) == false //new Photographer("Wilma", new Camera(22,true), new Snapshot("lion",3,4)) == true //To determine if this Photographer's snapshot is of a lion boolean ofLion() { // ... this.name ... this.myCamera.CameraMethod( ... ) ... this.pic.SnapshotMethod ( ... ) return this.pic.isLion(); } //new Photographer("Fred", new Camera(23,false), new Snapshot("kitten",3,4)).takePicture("Wilma") -> // new Photographer("Fred",new Camera(22,false), new Snapshot("Wilma",3,4)) //To produce a Photographer who has taken a picture of content, using this Photographers camera Photographer takePicture(String content) { //... this.name ... this.myCamera.CameraMethod( ... ) ... this.pic.SnapshotMethod( ... ) ... return new Photographer( this.name, this.myCamera.advance(), this.myCamera.shoot(content) ); } } /* Original camera class Camera { int roll; boolean isBW; Camera(int roll, boolean isBW) { this.roll = roll; this.isBW = isBW; } //new Camera(33,true).advance() -> new Camera(32,true) //To produce a new Camera with one less picture on the roll than this Camera Camera advance() { // this.roll ... this.isBW ... return new Camera(this.roll - 1, this.isBW); } //new Camera(33,true).shoot("lion") -> new Snapshot("lion",3,4) Snapshot shoot( String picOf ) { // ... this.roll ... this.isBW ... picOf return new Snapshot(picOf, 3, 4); } } */ class Snapshot { String contains; int width; int height; Snapshot(String contains, int width, int height) { this.contains = contains; this.width = width; this.height = height; } //new Snapshot("lion",3,4).isLion() == true //new Snapshot("waterfall",3,3).isLion() == false //To determine if this snapshot is of a lion boolean isLion() { //this.contains ... this.width ... this.height ... return this.contains.equalsIgnoreCase("lion"); } } /* +------------------------------------------+ | abstract Camera | +------------------------------------------+ +------------------------------------------+ | abstract Camera advance() | | abstract Snapshot shoot(String contents) | +------------------------------------------+ / \ --- | ---------------------------------------------------------------- | | | +--------------------------+ +--------------------------+ +---------------------------+ | Disposable | | SLR | | Digital | +--------------------------+ +--------------------------+ +---------------------------+ | int roll | +-| Lens curLens | +---------------------------+ +--------------------------+ | | int roll | | Camera advance() | | Camera advance() | | +--------------------------+ | Snapshot shoot( String c) | | Snapshot shoot(String c) | | | Camera advance() | +---------------------------+ +--------------------------+ | | Snapshot shoot(String c) | | +--------------------------+ +------------+ | | Lens |<-----------------+ +------------+ | int width | | int height | +------------+ */ abstract class Camera { //To remove one shot from this camera abstract Camera advance(); //To produce a snapshot of the contents abstract Snapshot shoot(String contents); } class Disposable extends Camera { int roll; Disposable(int roll) { this.roll = roll; } //new Disposable(12).advance() -> new Disposable(11) //To produce a camera with one less shot than this Disposable Camera advance() { // this.roll ... return new Disposable(roll-1); } //new Disposable(12).shoot("Larry") -> new Snapshot("Larry",3,4) //To produce a snapshot of the contents Snapshot shoot(String contents) { // this.roll ... return new Snapshot(contents,3,4); } } class SLR extends Camera { Lens curLens; int roll; SLR(Lens curLens, int roll) { this.curLens = curLens; this.roll = roll; } //new SLR(new Lens(5,10),34).advance() -> new SLR(new Lens(5,10),33) //To produce a camera with one less shot than this SLR Camera advance() { // ... this.curLens.LensMethod( ... ) ... this.roll ... return new SLR(this.curLens, this.roll -1); } //new SLR( new Lens(4,16), 33).shoot("Moe") -> new Snapshot("Moe",4,16) //To produce a snapshot of the contents Snapshot shoot(String contents) { // ... this.curLens.LensMethod( ... ) ... this.roll ... contents return new Snapshot(contents, this.curLens.getX(), this.curLens.getY()); } } class Lens { int width; int height; Lens(int width, int height) { this.width = width; this.height = height; } //to return the x dimension produced by this lens int getX() { //this.width ... this.height ... return this.width; } //to return the y dimension produced by this lens int getY() { //this.width ... this.height ... return this.height; } } class Digital extends Camera { Digital() { } //new Digital().advance() -> new Digital() //To produce a camera with ont less picture than this digital Camera advance() { // ... return new Digital(); } //new Digital().shoot("Curly") -> new Snapshot("Curly",8,10) //To produce a snapshot of the given contents Snapshot shoot(String contents) { // ... return new Snapshot(contents); } }