Question: A programmer has written some java code to help manage a collection of photographs. So far, two classes have been written: a class Photo and

A programmer has written some java code to help manage a collection of photographs. So far, two classes have been written: a class Photo and a class PhotoAlbum. Parts of both classes are shown below (with comments omitted)

public class Photo

{

public String dateTaken;

public String title;

public Photo ( String date, String picTitle)

{

dateTaken = date;

title = picTitle;

}

}

import java.util.ArrayList;

public class PhotoAlbum

{

public String name;

public ArrayList photos;

public PhotoAlbum (String albumName)

{

name = albumName;

photos = new ArrayList<> ();

}

public void printDetails (Photo myPhoto)

{

System.out.println (myPhoto.title + " taken on " + myPhoto.dateTaken);

}

public void printMatching (String findText)

{

for (Photo p: photos)

{

if (p.title.contains (findText)) {

System.out.println(p.title + " taken on " + p.dateTaken);

}

}

}

}

Answer the following questions with reference to the code shown above.

a) The code above declares public visibility for the fields of both classes.

i) explain why this is problematic, from the point of view of good object oriented design.

ii) Make any necessary modifications to the code to resolve this problem, including any accessor methods.

b) explain why the methods printDetails and printMatching still do not satisfy object oriented design principles. Suggest a refactoring solution which will resolve these issues.

c) The programmer wishes to store a number of tags with each photo. For example, a picture of a cat might be tagged with "kitty", "cute", "tabby", and so on.Tags are represented as strings. There is no limit to the number of tags that could be applied . A newly created Photo object will initially have no tags.

i) Modify the Photo class to add a new field to store the tags for a photo, and any changes necessary to the class constructor.

ii) Write two methods to deal with the tags: addTag will take a string parameter and add it to the tags for the photo; getTags will return all tags for a photo.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!