Sale!

CS1026a: Assignment 4 solved

Original price was: $35.00.Current price is: $30.00. $25.50

Category:

Description

5/5 - (2 votes)

Task:

Many different kinds of applications exist that help people organize their photos. These
applications often allow a person to organize them into albums or catalogs, add captions, dates, etc. In
this assignment, you will write a simple version of such an application.

Your “photo organizer” will
read a file that contains information about a collection of photos, including a caption and a date, and
will produce new “labeled photos” with the photo’s caption below it, and the photo information printed
as a watermark over the bottom right corner of the picture (a sample of a “labeled photo” is shown
below). Then you will create an “album” (a directory) in which the new photos are sorted by date.

Part A:

First, you will write a new class called LabeledPhoto that will model the information needed about an
individual photo in order to create the labeled photo. The attributes of an object of this class will be a
unique identifier for the photo, the category that the photo belongs to, the date that the photo was
taken, a “caption”, and the name of the file with the photo (a .jpg file). The class will contain the
methods described below, one of which is to generate the labeled photo.

Functional Specifications for Part A

1. Write a new class LabeledPhoto that will be stored in the file LabeledPhoto.java.
2. It will have the following attributes (instance variables):
• id, of type String
• category, of type String
• year, of type int
• month, of type int
• day, of type int
• caption, of type String
• photoFile, of type String, which is the name of the file that holds the photo
• photoPic, Reference to a photo object decribed below
3. The class LabeledPhoto will have a constructor that takes six parameters; the constructor will
have the header:

public LabeledPhoto(String theId, int theDay, int theMonth, int
theYear, String theCategory, String theCapion, String
thePhotoFile)
The class will have an instance variable photoPic, which will be of type Picture and which
will be created within your constructor; this will be the “labeled picture” that you will create.
To add text to a picture, you will need to add two methods, specified in Asn4PictureMethods.java
on the website, to your Picture class.

/** This method will draw a the given text, using the font size specified
* in a bold font at the yPos given and centered in the image
* @param text : the text to write
* @param fontSize : the size of font to use
* @param yPos : the vertical position of the
* baseline of the first character
*/
public void drawCenteredString(String text, int yPos, int fontSize)

/** This method will draw the given text, using the font size specified
* in italics in the bottom right corner of the image
* @param text : the text to write
* @param fontSize: the size of font to use
*/

public void drawInfoWatermark(String text, int fontSize)
The following code segment is an example of how one would call the drawString() method:
Picture picObj = new Picture(500,500);
String testCap = (“This is a test”);
String testInfo = (“This is a test”);
int fontsize = 30;
picObj.drawCenteredString(testCap, 100, fontsize);
fontsize = 12;

picObj.drawInfoWatermark(testInfo, fontsize);
In your constructor you will be able to read in the picture using the name of the file stored in
photoFile as you did in previous assignments and then using the new drawing methods, you
will be able to create a new Picture containing the original picture with the caption below the
image in a blank space, and the category and date on the original photo in the bottom right-hand
corner.

The labeled photo will be a Picture object, which needs to be a bit taller than the
original to make room for the caption, with the original copied into it.
4. The public methods for the class will be:
• An accessor (getter) method for each of the attributes, that returns the value of the attributes,
including the attribute photoPic. For the image, you should make a copy of the image and
return that, so the object state remains private.

• Two modifier methods, setCaption and setCategory which will modify the caption
/category instance variables respectively, as well as update the photoPic to include the new
caption/category instead of the old one.
• A method toString that returns a string containing the information about the photo, in the
format: caption(identifier, category, filename, date)
Note that “date” should contain the day, month and year; the format is of your choice.
• A method isOlder that returns true if the image it is invoked on was taken on an earlier date
than an image passed in as a parameter.

• You may, of course, add other methods to the class that you might find useful.
5. One nice way to test a class that you have written yourself is to add a main method to the class,
after all the other methods. This allows you to test your new class without writing a separate test
program (as we did in Assignments 2 and 3).
This main method will have the usual header, and will contain statements that try out the methods
of the LabeledPhoto class, including:

• Create a LabeledPhoto object with sample parameters.
• Invoke all the getter methods on it and print the return values (except photoPic).
• Invoke the getter method to get the labeled photo and display it.
• Call the toString()method and print the return value.
• Create a second LabeledPhoto object with sample parameters.
• Call the toString()method on the second photo and print the return value.
• Use the isOlder method to test if the first image is older than the second and print the result
• Use the isOlder method to test if the second image is older than the first and print the result

Part B:

You will write another new class called PhotoCollection that models a collection of photos. The
information for all the photos will be read in from a text file, with one line of information per photo.
Each line of the file will be of the form
identifier, day, month, year, category, caption, filename
An example of a line in the provided test file myPhotoCollections.txt is:
cat02 4 5 2011 Pets How_embarrassing granny_cat.jpg
The list of photos will be kept in one array of LabeledPhoto object references.
The PhotoCollection class will contain the methods described below.

Functional Specifications for Part B

1. You are to write a new class PhotoCollection that will be stored in the file
PhotoCollection.java. It will start as follows:
import java.util.*;
import java.io.*;
public class PhotoCollection {
2. It will have the following attributes (instance variables) declared by
private LabeledPhoto[] photoArray;
private int numPhoto = 0;
The array photoArray will hold the LabeledPhoto objects created from each line of the
photo information file. The variable numPhoto will be the number of photos, i.e. the number of
lines in the file of photo information that we start with. (Note: your instance variable declarations
should be exactly as above.)

3. The class will have a constructor that takes as it parameter the name of the file of photo
information. The constructor will have the header
public PhotoCollection(String fileName)
The constructor will need to initialize the instance variables from the contents of the file. The
algorithm for the constructor is:
• Read the lines of the file whose filename is the parameter into an array of String. (This array
will be a local variable in the constuctor.) Get the number of photos from the size of the file.
• Create the array object photoArray that was declared as an attribute. Its size will be the
number of photos. (Note that at this step, you are just creating an array object referenced by the
instance variable photoArray; you are not yet creating the individual LabeledPhoto
objects in the array, that’s the next step.)

• For each line that was read in from the file:
o Get the photo information (identifier, day, month, year, category, caption, filename )
from the line, retrieved as tokens from the line. (Hint: use the StringTokenizer
class as in Lab 10.).
o You can use Integer.parseInt( anyStringRef ), to convert text to an int
where needed (like for the date values).
o Create a LabeledPhoto object using the photo information, and store it as the next
element of PhotoArray.

4. The public methods for the class will be:
• A method listPhotoInformation() that displays the photo list on the screen as text, one
photo per line; you can make use of the toString method that you wrote for your class
LabeledPhoto. The header for this method will be
public void listPhotoInformation()
• A method showPhotos() that shows all the labeled photos for the class on the screen. (The
user can then move them around on the screen.) The header for the method will be
public void showPhotos()

• A method showPhotos(String theCategory) that shows all the labeled photos for the
class on the screen as above, but only those that are in the category matching the parameter.
The header for the method will be
public void showPhotos(String theCategory)
• A method storePhotos() that stores all the labeled photos as .jpg files in a directory whose
name is passed as a parameter. The header for the method will be
public void storePhotos(String directory)
This method will write each Picture object in photoArray to a file in the directory. The
filename for each Picture object will be the unique identifier for that photo concatenated with
“_” (a single underscore) and concatenated with the photo’s category. From the example above, if
the information in the file was

cat02 4 5 2011 Pets How_embarrassing granny_cat.jpg
Then the name of labeled photo would be “cat02_Pets.jpg”. So, if the directory passed in is
myPhotos, then this photo would be written to myPhotos/cat02_Pets.jpg.
You will need to add the following code segment right at the beginning of this method; it creates a
new directory if the parameter directory does not yet exist. It also adds a “slash” at the end of the
directory name if the user has omitted it:
char end = directory.charAt(directory.length() – 1);
if (end != ‘/’ || end != ‘\\’)
directory = directory + ‘/’;
File directoryFile = new File(directory);
if (!directoryFile.exists())
directoryFile.mkdirs();

• A method sortPhotosByDate() that can sort photoArray by dates (decending, ie.
newest to oldest). The header for this method will be
public void sortPhotosByDate()
There are many sorting algorithms; two of the most straightforward ones are the bubble sort
and insertion sort. You can find examples of these on the web. (Hint: Remember you wrote
the isOlder method to compare photo dates)
5. You will be using the methods of PhotoCollection in Part C, which will also serve as a test of
your code.

Part C:

You will now write the application program (the class that contains the main method) that will allow
the user to create an “album” (well, really just a directory) with labeled photos sorted by dates. The
class will be called CreatePhotoCollections and will be stored in the file
CreatePhotoCollections.java. The algorithm for the main method is:
• Get the filename of the photo information file from the user.

• Get the name of the directory for storing the class labeled photos from the user.
• Display the list of photos on the screen as text.
• Sort the list by date
• Display the list of photos on the screen as text.
• Display (show) the labeled photos on the screen.
• Display (show) the “Pet” category photos on the screen
• Write the labeled photos as .jpg files to the directory.

Testing:

An example file which you can use for testing is myPhotoCollection.txt, available on the
AssignmentCS1026a 4 section of the Assignment Details web page along with a collection of photos as .jpg
files; these photos are in the zipped file myPhotos.zip. You may create your own test file with your
own photos in order to test your application; if you would like your TA to see your test data, make sure
you submit your version of the photo info file and your .jpg files as well!
Non-functional Specifications:
• Include a comment at the top of your file which includes:
// CS1026B Assignment 1
// Your name
// A brief description of what this program does
• Also Include brief comments (lines beginning with two forward slashes //) in your code that
explain the major steps of the program. Ex:
// Convert height to meters
• Include brief comments above each method you write about what the method does.
• Use named constants as appropriate.
• Use Java conventions and good Java programming techniques, for example:
o Meaningful variable names
o Conventions for naming variables and constants
o Readability: indentation, white space, consistency
• Assignments are to be done individually and must be your own work. Software may be used to
detect cheating.

What to Hand In

LabeledPhoto.java, PhotoCollection.java, CreatePhotoCollections.java, Picture.java
(If you want the marker to use your own test data, submit that as well; see the note under Testing
above.)

What You Will Be Marked On

1. Functional specifications:
i. Are the required methods written according to specifications?
ii. Do they work as specified?
iii.Are they called as specified?
2. Non-functional specifications as described above.