Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions