CS367 Programming Assignment 2
| ||||||||||||||||||||||||||||||||||
OverviewDescriptionYou have no doubt been in situations where you have seen the same sequence of images repeatedly displayed. For example, in a movie theater, before the previews start there is often a loop of images (advertisements, trivia questions, famous scenes, etc.) that is projected onto the movie screen. A screen-saver might cycle through a series of pictures from your last vacation. Information kiosks show sequence of pictures or instructions in a continuous loop. A series of drawings shown very quickly (and repeated over and over) results in the never-ending animation in the corner of a webpage you visit. A Java program (in Eclipse archive form) that displays a sequence of labeled images may be found here. A major weakness of this program is that the content of the image sequence is "wired into" the program. That is, to change any detail of the sequence, the program must be rewritten. In this assignment we will investigate a better approach. We will implement a simple console-based editor that allows a user to create, edit, save, and load a sequence of images (including the title of an image and length of time to display it). The control file the editor creates can then be uploaded and shown in a continuous loop. It is typical for today's programmers to use ADTs that are already implemented as part of a class library or an API (e.g., Java's Application Program Interface). Understanding ADT implementations is still important to be able to make informed choices as to which ADT implementation is best suited for an application in terms of memory and time efficiencies. For example, knowing the tradeoffs between array and linked implementations of the List ADT helps us choose between Java's ArrayList and LinkedList classes. It is also important to be able to implement your own data structures when pre-built ones are not available or when standard ADTs (and their operations) are unsuitable for the intended application. In this assignment we provide you with an interface for a specialized ADT called a Loop. In addition to the main editor application class, you will write a class that implements a Loop ADT using a circular doubly-linked chain of nodes, as well as iterator and exception classes needed to support your Loop ADT implementation. GoalsThe goals of this assignment are to:
| ||||||||||||||||||||||||||||||||||
Program requirementsThe Image ClassAn Image object contains the name of the file containing the image, its title (possibly null) and a positive integer indicating the length of time (duration) the image should be displayed (in seconds). The Image class is provided for you (in the file Image.java). All the fields that store information about the image are private. The class has methods to retrieve and change information about a given instance of the Image class. It also has a method displayImage that creates a window and displays the contents of the image file. displayImage requires that all image files be placed in a top-level folder named images. The javadoc on the Image class provides the necessary information. You may not change the interface of Image class. Image Loop Editor Menu of CommandsYour main method, in the file ImageLoopEditor.java, will create an empty LinkedLoop of Images and then prompt the user to enter commands, which it will process until the user types x for exit. Your program will prompt for input by displaying : enter command (? for help)> You may assume that at least one non-whitespace character will always be entered. Your program will need to ensure the following:
If a valid command is entered, your program should carry out the command. If an invalid command is entered your program should display "invalid command" and display the prompt again. Here are the display editor commands your program should provide:
Note: Displaying the current context means displaying the image (i.e., its title, filename and duration) immediately before the current image, the current image, and the image immediately after the current image (one per line). For example, if the current image is "Lassie [dog2.jpg ,10]", the image before current is "Rin Tin Tin [dog1.jpg, 20]", and the image after current is "Bruiser [dog3.jpg, 15]", your program should display: Rin Tin Tin [dog1.jpg, 20] --> Lassie [dog2.jpg ,10] <-- Bruiser [dog3.jpg, 15] The arrows "--> <--" are displayed to highlight the current image. However, if there are fewer than three images in the loop, do the following:
The Loop ADTA Loop is essentially a circular List. However, unlike a List, a Loop does not have a beginning or an end and items within the Loop cannot be accessed using a position. Instead, the Loop has a current item and the ability to move forward or backwards. A Loop can be modified by removing the current item or by adding an item before the current item. The Loop ADT is represented in Java by the LoopADT<E> interface which is provided for you (see LoopADT.java). Complete information about Loop ADT operations can be found in the javadoc for the LoopADT<E> interface. The LinkedLoop<E> ClassYou will write an LinkedLoop<E> class (in a file named LinkedLoop.java) which implements the LoopADT<E> interface. In addition to the methods given in the LoopADT<E> interface, your LinkedLoop<E> class must have a constructor that takes no arguments and creates an empty loop. You may not add any other public constructors or methods. The internal data structure used by your LinkedLoop<E> must be a circular, doubly-linked chain of nodes. You may not use Java's LinkedList class for this assignment. You must implement your own chain of doubly-linked nodes. To implement the circular, doubly-linked chain of nodes, you will need to use the DblListnode<E> class (provided for you; see DblListnode.java). Your LinkedLoop<E> class will have a field of type DblListnode<E> that references the node representing the current item. You may include other private fields that you find helpful, but no public fields. Additional ClassesThere are two other classes that you will need to write to go along with your LinkedLoop<E> class. The EmptyLoopException classThe EmptyLoopException must be a checked exception. It is thrown by some methods (as described in the LoopADT<E> interface documentation). Your image loop editor class must handle them appropriately. The LinkedLoopIterator<E> classThe LoopADT<E> interface includes an iterator() method that returns an Iterator<E> object. You must write a specialized iterator class, named LinkedLoopIterator<E>, which will be used as the type of iterator returned by the LinkedLoop<E>'s iterator() method. The LinkedLoopIterator<E> class must implement the Iterator<E> interface (you do not need to implement the remove method - just throw an UnsupportedOperationException). You will need to write a constructor for your LinkedLoopIterator<E>; a package-access constructor that takes a DblListnode<E> as a parameter is suggested. TestingTesting interactive programs can be frustrating if you aren't careful about recording the commands that you've entered. A bug can suddenly occur and you may not recall the exact sequence of commands to reproduce the bug. A technique for testing interactive programs is to process a fixed set of commands from an input file. Input files can be developed that test particular aspects of the program and these tests are easily repeated by running the program again on a particular input file. Your program must be written so that it can be provided with a single, optional, command-line argument that is the name of an input file containing image loop editor commands. If it is not provided, then the program operates as an interactive console application reading commands from the console keyboard and displaying the results in the console window. If more than one command-line argument is given, display "invalid command-line arguments" and quit. If a single command-line argument is provided, it is assumed to be the name of an input file. If the file doesn't exist or is unable to be opened, display "problem with input file" and quit. Otherwise, you may assume the file contains lines that start with at least one non-whitespace character and that the last line always has a single "x" for exit. Input from either the console or a file can be accomplished easily. If no command-line arguments are provided, construct a Scanner from System.in as shown (see also Java I/O): // assuming 'in' has been declared as a Scanner in = new Scanner(System.in); If a command-line argument is provided do the following: File inFile = new File(args[0]); if (!inFile.exists() || !inFile.canRead()) { System.err.println("Problem with input file!"); System.exit(1); } in = new Scanner(inFile); How to proceedAfter you have carefully read this assignment page and given thought to the problem we suggest the following steps:
| ||||||||||||||||||||||||||||||||||
Handing inWhat should be handed in? Make sure your code follows the style and commenting standards used in CS 302 and CS 367. Electronically submit the following files to the Program 2 Dropbox on Learn@UW:
Please turn in only the file named above. Extra files clutter up the "handin" directories. | ||||||||||||||||||||||||||||||||||
Last Updated: 8/11/2016 © 2008-2016 CS367 Instructors |