CS367 Programming Assignment 1
| ||||||||||||||||||||||||||||||||||||||||||
AnnouncementsCheck here periodically.
| ||||||||||||||||||||||||||||||||||||||||||
OverviewWhy are we doing this program? DescriptionFor this assignment you will write a program that processes an arbitrary number of itineraries. Each itinerary consists of a list of destinations along with the number of days spent at each destination. The itineraries are stored in a text file that must be loaded (read and stored) by your program. Your program will process this text file, specified as a command-line argument, and then apply a prescribed set of operations and display the resulting output on the console window. For Programming Assignment 1 you will not be building a data structure from scratch. Instead, you will use Java's ArrayList class to implement the itinerary class specified below and in your main class to manage a list of itineraries. GoalsThe goals of this assignment are to: | ||||||||||||||||||||||||||||||||||||||||||
SpecificationsWhat are the program requirements? The Destination classEach itinerary contains a list of destinations. The Destination class represents a single destination that keeps track of the name of the destination (as a String) and the number of days spent at that destination (as an int). The Destination class implements the following methods (do not add any other public methods than those listed below):
The Itinerary classThe Itinerary class stores a list of destinations. The Itinerary class implements the following methods (do not add any other public methods than those listed below):
For the internal structure of the Itinerary class, you may write your own ArrayList class or use Java's ArrayList class that implements Java's List interface. You must also make use of Java Iterators (or ListIterators) where appropriate. You may also write additional helper classes to be used by the Itinerary class. The ItineraryApp classYour application program, ItineraryApp, creates and uses a list of Itinerary objects read from a text file explained below. The application must make use of Java Iterators (or ListIterators) where appropriate. The command line format is:
where FileName is the name of the text file to be processed. The main method of the ItineraryApp class does the following tasks:
Itinerary text filesText files storing lists of itineraries read by your program. Each line in the text file represents one itinerary and lists the destinations followed by the number of days at each destination: destination name 1:days1:destination name 2:days2:...:destination name N:days N where the destination names 1 through N are sequences of any characters excluding colons (i.e., ":"). You may assume the text files are in the specified format, and contain at least one itinerary with a minimum of one destination. You may also assume that destination names are unique and are to be represented by your program as Strings. Note differences in capitalization and spacing should be considered the same as in this example: New York vs. NEW YORK vs. new york Note, destinations are to be added to itineraries in the order in which they appear in the text file and itineraries are to be added to the list in the main application in the order in which they appear in the text file. Command-line argumentsRecall that in Java, when you run a program it is a main method that runs, and it always has the following header: public static void main(String[] args) The array-of-Strings parameter, args, contains the command-line arguments that are specified when the program is run. If the program is run from a Linux prompt (i.e., not from a programming environment like Eclipse), the command-line arguments are simply typed after the name of the class whose main method is to be executed. For example: java ItineraryApp itineraries1.txt runs the Java interpreter on the main method of the ItineraryApp class, passing the string "itineraries1.txt" as the command-line argument. To use command-line arguments when you run a Java program using Eclipse:
How to proceedAfter you have read this program 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 file to your In "handin" directory by the due date and time (or refer to the late policy):
Please turn in only the file named above. Extra files clutter up the "handin" directories. | ||||||||||||||||||||||||||||||||||||||||||
Enrichment: Mutual ExclusionHow are data structures shared in large systems? Scenario: Ben and Jerry work together in accounting. One day they both get to work and open up the same spreadsheet from the server. Ben loads it at 9:30 a.m., Jerry loads it at 10. They both spend all day making changes to different parts of the spreadsheet. Ben saves it at 4:30 p.m., Jerry saves it at 5. What happens to each person's work? How could they ensure that they never overwrite anyone else's work? One solution would be, if someone needs to edit the spreadsheet, first they look around and see if anyone else is working on it. If someone is, they have to wait; if nobody is, they tell everyone to stay out until they're done. Enter the world of multithreaded computing. Suddenly, the spreadsheet is a data structure, Ben and Jerry are hundreds of threads vying to change the same data, working on one spreadsheet per day becomes performing hundreds, maybe thousands of changes per second, probably more. Suddenly, the remote chance of two threads working on the same space in memory--and corrupting it--becomes a definite possibility, and when it happens, unpredictable things occur. This problem, whether two people sharing one spreadsheet or a million threads sharing an ArrayList, is called the Mutual Exclusion problem, and it's hard. Interesting enough, Ben and Jerry's solution is a real way of solving this problem, preventing two threads from making changes at the same time. But there are other questions we can ask, like: How can we make sure everyone gets to accomplish their work, and one thread doesn't monopolize access to the data? Computer Scientist Edsger Dijkstra originally posed one example of this problem as the Dining Philosophers problem, and it remains an interesting puzzle, any many solutions have been offered since. The Dining Philosophers and related problems raised by multi-threading are presented in CS 537, Introduction to Operating Systems. Methods of synchronizing threads and controlling their access to data structures is called thread-safe development, and is also discussed at length in CS 537. | ||||||||||||||||||||||||||||||||||||||||||
| Last Updated: 9/23/2008 © 2008 Rebecca Hasti and Jim Skrentny, CS367 Instructors, hasti@cs.wisc.edu |