95-733 Internet Technologies Spring 2013
Homework 1 Three parts
Due: 11:59 PM Tuesday January 29, 2013
This project is made up of three parts. Each Part is worth 33%
of your project 1 score.
Part 1 Ajax and JSON
=====================
0) Read the article by Philip McCarthy on Ajax at:
http://www.ibm.com/developerworks/library/j-ajax1/
1) Build a working Ajax shopping cart using the code from the article.
So that the TA's have an easy time grading the assignments, Netbeans
and Glassfish will be used as demonstrated in class.
Name your project ACoolAJAXShoppingCart.
It is a requirement that you employ an external CSS style
sheet to add a bit of value to McCarthy's HTML. You may add
any style features that you feel would make the presentation
more interesting and attractive.
2) Add a button to each item displayed so that the user may
delete items from the shopping cart. Your delete button
will behave like the add button, but will count down
rather than up. If an item is not currently in the cart
then a delete request will be ignored. If a delete
request causes a quantity to go to zero then the
deleted item will be removed from the cart and no longer
displayed.
Name your project ACoolAJAXShoppingCartImproved.
3) Modify your solution to part 2 so that it uses JSON rather
than XML. You are required to use the Google JSON libray
found here: http://code.google.com/p/json-sans-eval/.
Note that your servlet will generate a content type of
application/json and your XMLHttpRequest object will
return JSON via the responseText property rather than the
responseXML property.
This part requires that you design a JSON string that
corresponds to the XML shopping cart. Be sure to see
Google's example at the link given above.
For a careful description of JSON see
http://www.json.org/
Part 1 Submission
=================
Place your work into a single directory named Homework1Part1.
Zip this directory into a file named Homework1Part1.zip.
Submit your zip file to the Assignment section of
Blackboard.
Name your project ACoolAJAXShoppingCartImprovedJSON.
For full credit, you are required to document parts 2
and 3 only.
Part 2 StAX processing and XSD
==============================
There are three standard approaches to processing XML formatted
data. These are DOM, SAX and StAX. In this part, we will be using the
StAX approach.
JSR 173 defines the Streaming API for XML. This part of homework
1 will use JSR 173 to process schedules and compute a common meeting
time. Your solution will include the import of the javax.xml.stream.*
API.
There are four schedules found under the directory
www.andrew.cmu.edu/~mm6/95-702/McCarthysSchedule. These schedules are
named schedule1.xml, schedule2.xml and so on. There is also an XSDL
document called schedule.xsd that contains the grammar for the schedule
language. Use a browser to examine one of the four schedules and study
the schedule grammar carefully.
Write a program in Java called Scheduler that attempts to find a
meeting time when n > 1 people are free to meet. Scheduler examines a set
of schedules and tries to find a meeting time for each day of the week. If
it is able to find a common meeting time then it displays the day and time
of the meeting. If it is unable to find such a time it announces that fact
for that particular day. It does this for each of the seven days. That is,
for each day, a common meeting time is either announced or declared
as not possible. The input to the scheduling process will include a minimum
meeting time in seconds. If a meeting time of 60 * 60 = 3600 seconds is
required then the scheduler will not generate meeting times for anything less than
one hour.
Scheduler will read a list of URL's from a local urlList.xml file.
It will then fetch an XML document from each of these URL's. It will compute
any common meeting times and display a report to the user.
Looking for 2 hour meeting times, when applied to all four schedules,
my scheduler produces the following output:
java Scheduler 7200
Loading 4 schedules.
**This group can't meet on Monday**
**This group can't meet on Tuesday**
**This group can't meet on Wednesday**
**This group can't meet on Thursday**
**This group can't meet on Friday**
**This group can't meet on Saturday**
**Meeting scheduled for a minimum of 7200 seconds at 13:0:0:16:0:0
on Sunday.
The minimum meeting time will be passed to your program via a command
line argument.
The above screen dump may not currently be accurate because the xml files
change periodically.
Suggested Approach
==================
It is suggested that you write an object oriented solution to
this problem. Clearly, one type of object that we want to represent
is a schedule object. Define a Schedule class with a single constructor
and a single accessor method. The constructor for a Schedule object
will take a URL object as an input parameter. It will then read the
entire document using StAX and extract and retain data. These data
will be made available to the user via the second method of this
class. This second method will be called getAvailable and will
have a signature similar to the following:
public LinkedList getAvailable(String day) throws Exception
There will be a second class called URLList that is used to
represent objects holding URLList documents. Its constructor takes
a single URL as an input parameter and uses StAX to read the contents
of a URLList document. It retains data and makes these data available
via two accessor methods. These methods have the following signatures:
public int getNumURLs() throws Exception
public String getURL(int i) throws Exception
Another class that maintains a start and stop time and
provides utility methods for time calculations is also called for.
Think about what utility methods you will need.
The scheduling activity might best be written as a static method
of the Scheduler class.
As a general rule, don't try to solve the entire
problem. Solve smaller problems by hand and focus on building
classes and objects that will act as useful tools for the larger
problem. Test the classes and objects on smaller instances of the
problem. Solve smaller problems first.
Algorithm
=========
In my solution, I used a simple backtracking search as
exemplified in Michael Main's "Bear Game". This is the approach
that I suggest that you take. In the "bear game", we start with
an initial number of bears and wish to reach a goal number of
bears within a certain number of steps.
We are only allowed to perform one of two operations to reach
our goal. We may increment the number of bears by a fixed constant
(provided at run time) or we may divide the current number of
bears in half (if the current number of bears is even.)
For example, suppose we start with an initial value of 10 bears
and want to reach 5 bears. Suppose too that our increment is 10
and we are allowed to execute 2 steps. Main's algorithm would proceed
as follows:
10 --> 20 --> 30 fails need to backtrack
20 --> 10 because 20 is even but this fails too
--> 5 we found a solution in one step
As an exercise, suppose the goal is 15 bears and we begin
with 10 and are allowed 4 steps with an increment of 4.
Here is the code that you might wish to modify to
solve the scheduling problem.
public static boolean bears (int initial, int goal,
int incr, int n)
{
if (initial == goal) return true;
else if (n==0) return false;
else if (bears(initial+incr, goal, incr, n-1))
return true;
else if (initial % 2 == 0)
return bears(initial/2, goal, incr, n-1);
else return false;
}
Simpler Project (Partial credit)
================================
The most difficult part of the assignment above is
the part that requires you to search for a common meeting
time using an algorithm like that shown in the "Bears Game".
This alternative assignment allows you to skip that part of
the problem. Everything else remains the same.
For less credit (maximum of 80%) you may instead write a
solution that simply displays all four schedules. The program
would still read the URLList.xml file and the schedule
files and would still need to perform StAX parsing. The output
would look like the following:
java Scheduler
Loading 4 schedules.
Schedule1.xml
Monday
9:00 - 10:00
11:00 - 12:00
Tuesday
:
Schedule2.xml
Monday
9:00 - 10:00
11:00 - 12:00
Tuesday
:
:
Schedule4.xml
:
Developing a simple grammar
===========================
It is required that you design an XSDL grammar for the
urlList.xml file. See the grammar associated with schedule documents
for a guide. Also, you are required to complete the tutorial located
at W3C Schools on XML Schema: (see http://www.w3schools.com/schema/default.asp).
Here is a copy of my urlList.xml file. It is currently configured
to provide four schedules to the scheduler. Note the reference to the
urlList.xsd document.