Project Philosophy
READY
Dining Project
Date: Jan 2, 2019
by Nicholas Duchon
|
Outline:
|
General Objectives
Here are some notes about the projects, the particular features
of object-oriented design and object-oriented programming
(OOD/OOP) the we want to cover in this class and some of the
features of Java to help support that style of programming. We
also want to explore the Java GUI system a little, with
particular emphasis on viewing the data structures and effective
ways to display the running of multiple threads competing for
resources.
The particular scenarios selected for each semester ask you to
implement as many of these objectives as possible in some
compelling way. We are always open to additions and suggestions.
General objectives for each project:
- Project 1 - classes, text data file, GUI, searching
- Define and implement appropriate classes, including:
- instance and class variables,
- constructors, mostly using Scanner parameters
- toString methods, and
- other appropriate methods.
- Read data from a text file:
- specified at run time,
- JFileChooser
jfc = new JFileChooser (".");
// start at dot, the current directory
- JFileChooser
jfc = new JFileChooser ("../Data");
// start at a directory relative to the current directory
- using that data to create instances of the classes,
- creating a multi-tree (class instances related in
hierarchical, has-some, relationships), and
- organizing those instances in existing JDK structures
which can be sorted, for our projects use ArrayList's.
- Create a simple GUI:
- Set the title of the window to include the project name,
your name and the name of the data file:
String title =
String.format (
"%s, %s, File: %s",
programName, yourName, fileName);
setTitle (title);
- presenting the data in the structures with with some
buttons, at least:
- Read - read a data file, as above
- Display - set the text area to world.toString ()
- Clear - clear the text area
- text fields supporting SEARCHING on the various fields of
each class.
- Project 2 - HashMap class, Comparator, sorting
- Use the JDK HashMap class to write more efficient code when
constructing the internal data structures from the data file.
- Implement SORTING using the Comparator interface together
with the JDK support for sorting data structures, thus sorting
on different fields of the classes from Project 1.
- Extend the GUI from Project 1 to let the user sort the data
at run-time and display the results using world.toString().
- Project 3 - More JDK classes - GUI's and threads
- Explore other GUI classes, such as JTree, JTable, and
JProgressBar.
- Create and run threads
- Competing for one resource.
- Project 4 - Concurrency
- Resource pools
- Threads competing for multiple resources
- Blocking threads
- Extending the GUI interface to visualize the resource pools
and progress of the various threads.
Introduction - the Dining Project
series
For this set of projects, you will simulate some of the aspects
of a number of restaurants.
Here are the classes and their basic instance variables you
should define:
- DiningSolution extends JFrame
- variables used by the GUI interface
- world: World
- Item
- index: int
- parent: int
- name: String
- World extends Item
- countries: ArrayList <Country>
- Tool extends Item
- cost: double
- type: String
- used: Eat
- Eat extends Item
- calories: int
- diner: Diner
- eatTools: ArrayList <Tool>
- reqs: ArrayList <String>
- rest: Restaurant
- time: int
- jpb: JProgressBar
- variable associated with
controlling this thread
and the associated GUI display
|
- Diner extends Item
- age: double
- busyFlag: boolean
- eats: ArrayList <Eat>
- height: double
- weight: double
- Country extends Item
- area: int
- population: int
- rests: ArrayList <Restaurant>
- Restaurant extends Item
- cash: double
- diners: ArrayList <Diner>
- menuSize: int
- rating: int
- tools: ArrayList <Tools>
- TakeOut extends Restaurant
- FoodTruck extends Restaurant
- EatIn extends Restaurant
|
- Popular extends EatIn
- salads: int
- softs: int
- wings: int
- FastFood extends EatIn
- Elegant extends EatIn
- appets: int
- deserts: int
- entrees: int
- soups: int
- Cafe extends EatIn
- Bar extends EatIn
- beers: int
- cognacs: int
- whiskeys: int
|
Eventually, in Projects 3 and 4, you will be asked to show the
progress of the jobs using JProgressBar's.
Here's a very quick overview of the projects:
- Read a data file, create the internal data structure, create a
GUI to display the structure, and let the user search the
structure.
- Sort the structure and use hash maps to create the structure
more efficiently.
- Create a thread for each job (eating a dish), a diner can only
eat one dish at a time, and create a GUI to show the progress of
each job; and display the data structure using a JTree.
- Simulate competing for resources (tools required for eating
each dish).
Documentation
HINT: You should start working on a documentation file before you
do anything else with these projects, and fill in items as you go
along. Leaving the documentation until the project is finished is
not a good idea for any number of reasons.
Also see: Grading below
The documentation should include the following (graded) elements:
- Cover page (including name, date, project, your class
information)
- Design
- including a UML class diagram
- classes, variables and methods: what they mean and why they
are there
- tied to the requirements of the project
- User's Guide
- how would a user start and run your project
- any special features
- effective screen shots are welcome, but don't overdo this
- Test Plan
- do this BEFORE you code anything
- what do you EXPECT the project to do
- what data files are you going to use, and why
- Lessons Learned
- express yourself here
- a way to keep good memories of successes after hard work
CAUTION: the documentation file size should be reasonable,
currently < 5 MBytes, mostly < 1 MB.
Project 1 - Also see Grading
The goals are:
- Create a GUI
- Let the user select a data file, using JFileChooser
- Read the data file, creating the specified internal data
structure
- see the Introduction
for the classes and variables of the structure
- Display the internal data structure in a nice format in the
GUI
- use JScrollPane and JTextArea
- Display the results of a Search specified by the user
- JTextField to specify the search target
- Searching names for matching substrings
- Note that a search may return more than one item
- DO NOT create new data structures (beyond the specified
internal data structure) to search
you may create a structure of found items as a return value
Data file format:
- Each item in the simulation will appear on a single line of
the data file.
- The data file may have comment lines, which will start with a
// or **.
- There may be blank lines in the data file, which your program
should accept and ignore.
- The data lines will start with a flag value, indicating which
item is being specified, its name, its index, and the index of
its parent
- the parent index is used to specify the connections used to
create the internal data structure (ie, assign an item to its
parent or parent ArrayList).
- The program should ignore lines that start with any String
other than the flag values the program is currently expecting.
- For most items there will be additional fields appropriate to
the class of that item.
- The fields on a line are space delimited (perhaps more than
one space)
- This works well with Scanner methods, such as next(), nextInt() and
nextDouble().
Here are examples of the valid lines, without comments:
world
Mars
10000 0
country
Colombia
20861 10000
48653419 1038700
restaurant
Food_by_Louie
35385 20861 1
56 70936.70
takeout
Meals_from_Dick
45331 20861 4
16
66142.27 1
foodtruck
Porkers_by_Berry
32543 20861 3
58
96393.49 KFUQYH
eatin
House_of_Alec
42203 20861 4
86
41758.35 60
popular
Ribs_by_Eddy
33138 20861 4
75
52377.74 101
9 14 4
fastfood
Burgers_by_Tracy
49457 20861 2
65
30278.99 67
3 9
elegant
Chez_Jayne
34389 20861 3
74
57359.54 51
199 1
9 25 4
cafe
Cafe_Carl
40248 20861 0
50
32922.50 22
4 5
bar
Bar_Mario
32180 20861 4
70
72525.87 48
175 8 10
tool
Soup
60239 45331 261.06 spoon
diner
Katherine
56241 45331 39.46 82.90 312.51
eat
Saint_Albray
62456 56241 125 899
spoon
Here are the details of valid lines, with an example of each
line, including comments about the data types and their meanings
on each line:
// world
name index parentIndex
// <String> <String> <int> <int>
world
world
10000 0
// country name index parentIndex population area
// <String> <String> <int> <int>
<int> <int>
country
Colombia
20861 10000
48653419 1038700
// restaurant name index parentIndex rating menuSize cash
// <String> <String> <int> <int>
<int> <int> <double>
restaurant
Food_by_Louie
35385 20861 1
56 70936.70
// takeout name index parentIndex rating menuSize cash
drivers
// <String> <String> <int> <int>
<int> <int> <double> <int>
takeout
Meals_from_Dick
45331 20861 4
16
66142.27 1
// foodtruck name index parentIndex rating menuSize cash
license
// <String> <String> <int> <int>
<int> <int> <double> <String>
foodtruck
Porkers_by_Berry
32543 20861 3
58
96393.49 KFUQYH
// eatin name index parentIndex rating menuSize cash
seatCount
// <String> <String> <int> <int>
<int> <int> <double> <int>
eatin
House_of_Alec
42203 20861 4
86
41758.35 60
// popular name index parentIndex rating menuSize cash
seatCount softDrinks salads wings
// <String> <String> <int> <int>
<int> <int> <double> <int> <int>
<int> <int>
popular
Ribs_by_Eddy
33138 20861 4
75
52377.74 101
9 14 4
// fastfood name index parentIndex rating menuSize cash
seatCount burgerStyles friesStyles
// <String> <String> <int> <int>
<int> <int> <double> <int> <int>
int>
fastfood
Burgers_by_Tracy
49457 20861 2
65
30278.99 67
3 9
// elegant name index parentIndex rating menuSize cash
seatCount wineCount appetizerCount soupCount entreeCount
desertCount
// <String> <String> <int> <int>
<int> <int> <double> <int> <int>
<int> <int> <int> <int>
elegant
Chez_Jayne
34389 20861 3
74
57359.54 51
199 1
9 25 4
// cafe name index parentIndex rating menuSize cash
seatCount coffeeCount teaCount
// <String> <String> <int> <int>
<int> <int> <double> <int> <int>
<int>
cafe
Cafe_Carl
40248 20861 0
50
32922.50 22
4 5
// bar name index parentIndex rating menuSize cash
seatCount beerCount whiskeyCount cognacCount
// <String> <String> <int> <int>
<int> <int> <double> <int> <int>
<int> <int>
bar
Bar_Mario
32180 20861 4
70
72525.87 48
175 8 10
// tool name index parentIndex cost
// <String> <String> <int> <int>
<double>
tool
Soup
60239 45331 261.06 spoon
// diner name index parentIndex age height weight
// <String> <String> <int> <int>
<double> <double> <double>
diner
Katherine
56241 45331 39.46 82.90 312.51
// eat name index parentIndex time calories [requirements]*
// <String> <String> <int> <int>
<int> <int> [<String>]*
eat
Saint_Albray
62456 56241 125 899
spoon
You may assume that the data file is correctly formatted and that
the parent links exist and are encountered in the data file as item
indices before they are referenced as parent links.
There will be a Java program (DiningCreator.jar) provided with
this package that will generate data files with various
characteristics with the correct format.
You should use this program to generate your own data files to
test various aspects of your project programs.
Sample input file:
** File:
../Data/010a.txt
** Data file for Dining projects
** Date: Fri Oct 12 22:43:03 EDT 2018
** Random: false
** Sequence restraurant types: false
** parameters:
** countries
restaurants
tools_per_rest
diners_per_rest
dishes_per_diner
tools_per_dish
**
1
1
1
1
1
1
// world name index parentIndex
// <String> <String> <int> <int>
world
world
10000 0
// country name index parentIndex population area
// <String> <String> <int> <int>
<int> <int>
country
Czech_Republic
32245 10000
10610947
77247
// takeout name index parentIndex rating menuSize cash
drivers
// <String> <String> <int> <int>
<int> <int> <double> <int>
takeout
Meals_from_Anna
48409 32245 3
19
97520.01 15
// tool name index parentIndex cost
// <String> <String> <int> <int>
<double>
tool
Decorating
51489 48409 272.15 knife
// diner name index parentIndex age height weight
// <String> <String> <int> <int>
<double> <double> <double>
diner
Karl
49894 48409 23.97 67.70 301.43
// eat name index parentIndex time calories [requirements]*
// <String> <String> <int> <int>
<int> <int> [<String>]*
eat
Abbaye_de_Belloc
72821 49894 142 516
knife
Sample output as plain text - which should be displayed in a
JTextArea on a JScrollPane in the BorderLayout.CENTER area of a
JFrame.
This version takes the lines starting with ** as comments that are
also shown in the output.
** File:
../Data/010a.txt
** Data file for Dining projects
** Date: Fri Oct 12 22:43:03 EDT 2018
** Random: false
** Sequence restraurant types: false
** parameters:
** countries
restaurants
tools_per_rest
diners_per_rest
dishes_per_diner
tools_per_dish
**
1
1
1
1
1
1
world
Czech_Republic
10610947 77247
Meals_from_Anna
3 19 $ 97,520.01 drivers: 15
-- Tools:
Decorating
cost: $ 272.15 type: knife
-- Diners:
Karl
age: 23.97
height: 67.70
weight: 301.43
-- Dishes:
Abbaye_de_Belloc
time: 142 calories: 516 knife
Suggestions:
Methods that should be implemented.
Each class should have an appropriate toString method. Here are
examples of two such methods:
- The displays line up columns MUCH better if a mono-spaced font
is used in the JTextArea:
jta.setFont (new Font ("Monospaced", Font.PLAIN, 12));
- Item class: A good version of toString includes a parameter
for indentation, which can be used by all the child methods
thus:
public String toString (int d) {
if (showClass) {
String cn =
getClass().getName(); // .toLowerCase();
if (d == 0)
return String.format ("%15s: %-30s", cn, name);
return
String.format ("%" + d + "s%15s%5d: %-30s", "", cn, d, name);
}
if (d == 0)
return
String.format ("%-30s", name);
return String.format ("%" + d +
"s%-30s", "", name);
} // end method toString
- The toString in Restaurant is more complex since this class
has a rather complicated relationship with its children, and
uses a helper method (toStringChildren):
static int D = 3;
// in the Item class
public
String toString (int d, String more) {
String st =
super.toString (d+D);
st += String.format ("%3d %4d
$%,10.2f", rating, menuSize, cash);
st += more;
st += toStringChildren (d);
return st;
} // end method toString
String toStringChildren (int d) {
String st = "";
if (showTools) {
if
(tools.size () > 0) st += String.format ("\n%"+(d+6*D)+"s--
Tools:", "");
for (Tool t:
tools) st += "\n" + t.toString (d+D);
}
if (showDiners) {
if
(diners.size () > 0) st += String.format
("\n%"+(d+6*D)+"s-- Diners:", "");
for (Diner
dr: diners) st += "\n" + dr.toString (d+D);
}
return st;
} // end toStringChildren
Each class should have an appropriate Scanner constructor,
allowing the class to take advantage of super constructors, so any
particular constructor will focus only on the additional elements
of interest to that particular class. As an example, here's one
way to implement the Elegant constructor:
- Elegant Scanner constructor, the earlier fields are handled by
parent class constructors: Item <String, int, int>,
Restaurant <int, int, double>, and EatIn <int>
constructors.
Elegant (Scanner sc) {
super (sc);
wines = sc.nextInt
();
appets = sc.nextInt ();
soups = sc.nextInt
();
entrees = sc.nextInt ();
deserts = sc.nextInt ();
} // end Scanner constructor
In the World class, we want to read the text file line by line.
Here are some methods and code fragments that you should
find helpful:
- Handling the data file line by line:
void
processFile (Scanner sf) {
comments = "";
while (sf.hasNextLine ()) {
String line =
sf.nextLine ();
if
(line.trim().length() == 0)
continue;
Scanner sc =
new Scanner (line);
switch
(sc.next()) {
case "restaurant" : addRestaurant (sc);
break;
- The following method is helpful for connecting a restaurant to
its country, since that operation is done for so many different
restaurant types:
void
connect (Restaurant r) {
for (Country c: countries)
if (c.index
== r.parent)
c.rests.add (r);
} // end connect Restaurant
- For a more complex linking operation, consider one way to
handle adding an eating task:
void
addEat (Scanner sc) {
Eat t = new Eat (sc);
for (Country c: countries)
for
(Restaurant r: c.rests)
for (Diner d: r.diners)
if (d.index == t.parent) {
d.eats.add (t);
t.diner = d;
t.rest = r;
}
} // end method addEat
You will probably find the comments in the following helpful,
they are mostly about similar projects and general issues in Java
relevant to our programs:
Project 2 - Also see Grading
Extend Project 1 to use
advanced data structures and support sorting on various keys.
Elaboration:
- Required data structure - the data structure specified in
Project 1:
- World has Countries
- Country has Restaurants
- Restaurant has Diners and Tools
- Diner has Eats
- Eat has Requirements
- Use the HashMap class to support efficient linking of the
classes used in Project 1.
- The instances of the hash map class should be local to the
readFile (Scanner) method.
- The HashMap instances should be passed as explicit
parameters to other methods used when reading the data file.
- For example, the body of the methods like the following
should be replaced to effectively use an <Integer,
Restaurant> hash map, the surrounding code needs to
support this structure:
Restaurant
getRestaurantByIndex (int x, java.util.HashMap
<Integer, Restaurant> hmr) {
return hmr.get(x);
} // end getRestaurantByIndex
- Since the body of this method has become trivial, perhaps
the call to this method can be simply replaced by the get
method of the HashMap.
- Your code should be sure to handle a null return from this
call gracefully.
- The HashMap instances should be released (go out of scope,
hence available for garbage collection) when the readFile
method returns.
- Comments: The idea here, besides getting some experience
with an interesting JDK Collections class, is to change the
operation of searching for an item with a particular index
from an O(N) operation, that is searching through the entire
data structure to see if the code can find the parent index
parameter, to an O(1) operation, a hash map lookup. Of course,
this isn't so very interesting in such a small program, but
consider what might happen with hundreds of countries,
thousands of restaurants, and perhaps millions of diners and
dishes (Eat instances).
- Comments: Also, after the readFile operation, the indices
are no longer interesting, and could be completely eliminated
from the program. In this program, removing the index
references could be accomplished by removing those variables
from the parent class, Thing.
- Implement comparators to support sorting:
- Sorts should be done in the appropriate ArrayList's.
- For example - sorting on int values:
case
"Restaurants by menu size":
for (Country c: world.countries)
Collections.sort (c.rests,
(a,b) -> a.menuSize - b.menuSize);
break;
- Sorting on a double value:
Collections.sort
(r.diners, (a,b) -> Double.compare(a.height,
b.height));
- Implement the following sorts:
- Countries by area
- Countries by population
- Restaurants by stars
- Restaurants by cash
- Restaurants by menu size
- Tools by cost
- Diners by age
- Diners by height
- Diners by weight
- The results should be shown using the World.toString
method in the JTextArea.
- Extend the GUI from Project 1 to allow the user to:
- sort by the comparators defined above.
- Again, the GUI elements should be distinct from the other
classes in the program.
Project 3 - Also see Grading
Implement threads and a GUI
interface using advanced Java Swing classes.
The project will be graded
according the criteria for the final project - see below.
Elaboration:
- Required data the
data structure specified in Project 1:
- World has Countries
- Country has Restaurants
- Restaurant has Diners and Tools
- Diner has Eats
- Eat has Requirements
- Extend Project 2 to use the
Swing class JTree effectively to display the contents of the
data file.
- (Optional) Implement a JTable to also
show the contents of the data file. There are lots of
options here for extending your program.
- Impossible jobs? None of the jobs in Project 3 should be
impossible, but some will be in Project 4.
- Threads:
- Implement a thread for
each job
representing a dish (instance of Eat).
- Use the synchronize
directive to avoid race conditions and insure that a diner
is eating no more than one dish at a time.
- The run method should
be synchronizing on the job's Diner.
- Thus, a convenient
approach is to add a reference to the Eat class,
referencing the instances Diner (parent) directly.
- in Project 4, the jobs
will also require appropriate tools, thus synchronizing
on the job's Restaurant.
- The thread for each job
should be started immediately AFTER the entire data file
has been read.
- Use delays to simulate
the progress of each job.
- Use a JProgressBar for
each job to display the progress of that job.
- Use JButton's on the Job
panel to allow the job to be suspended or cancelled.
- Display: use either a JPanel or a row in a JTable (MUCH more
challenging) to display and control the job thread.
- Exactly what your display looks like is up to you as long as
at least the required information and controls are shown for
each job.
- As before, the GUI elements
should be distinct (as appropriate) from the other classes in
the program.
- See the code at the end of
this posting for some suggestions.
Suggestions for Project 3 Eat class.
- Here is a sample of code for a Job (equivalent to the Eat
class for these projects) class in another context, the
Sorcerer's Cave project.
- The code for this class will need some modifications, but this
should give you an idea of the issues involved.
- In fact, you should find much of this code address issues
similar to the requirements of the Eat class in this project.
Also, some of the code at the following sites might give you some
ideas about how to proceed with this project:
- Project
3 Example - Sorcerer's Cave, note that even this one isn't
complete
- run
method - detailed analysis of the run method in the Job
class in the Cave project
//
j:<index>:<name>:<creature
index>:<time>[:<required artifact
type>:<number>]*
class Job extends CaveElement
implements Runnable {
JPanel parent;
Creature
worker = null;
int jobIndex;
long jobTime;
String
jobName = "";
JProgressBar
pm = new JProgressBar ();
boolean
goFlag = true, noKillFlag = true;
JButton
jbGo = new JButton ("Stop");
JButton
jbKill = new JButton ("Cancel");
Status status
= Status.SUSPENDED;
enum Status
{IMPOSSIBLE, RUNNING, SUSPENDED, WAITING, DONE};
public Job (HashMap
<Integer, CaveElement> hmElements, JPanel cv, Scanner
sc) {
parent = cv;
sc.next (); // dump first field, j
jobIndex = sc.nextInt ();
jobName = sc.next ();
int target = sc.nextInt ();
worker = (Creature) (hmElements.get (target));
jobTime = sc.nextInt ();
pm = new JProgressBar ();
pm.setStringPainted (true);
parent.add (pm);
parent.add (new JLabel (worker.name, SwingConstants.CENTER));
parent.add (new JLabel (jobName ,
SwingConstants.CENTER));
parent.add (jbGo);
parent.add (jbKill);
jbGo.addActionListener (e -> toggleGoFlag ());
jbKill.addActionListener e -> setKillFlag ());
new Thread (this).start();
}
// end constructor
// JLabel jln = new JLabel
(worker.name);
//
following shows how to align text relative to icon
// jln.setHorizontalTextPosition
(SwingConstants.CENTER);
// jln.setHorizontalAlignment
(SwingConstants.CENTER);
// parent.jrun.add (jln);
public void toggleGoFlag () {
goFlag = !goFlag;
}
// end method toggleRunFlag
public void setKillFlag () {
noKillFlag = false;
jbKill.setBackground (Color.red);
}
// end setKillFlag
void showStatus (Status st) {
status = st;
switch (status) {
case IMPOSSIBLE:
jbGo.setBackground
(Color.gray);
jbGo.setText
("Impossible");
break;
case RUNNING:
jbGo.setBackground
(Color.green);
jbGo.setText
("Running");
break;
case SUSPENDED:
jbGo.setBackground
(Color.yellow);
jbGo.setText
("Suspended");
break;
case WAITING:
jbGo.setBackground
(Color.orange);
jbGo.setText
("Waiting turn");
break;
case DONE:
jbGo.setBackground
(Color.red);
jbGo.setText
("Done");
break;
}
// end switch on status
}
// end showStatus
public void run () {
long time = System.currentTimeMillis();
long startTime = time;
long stopTime = time + 1000 * jobTime;
double duration = stopTime - time;
if (status == Status.IMPOSSIBLE) {
showStatus (Status.IMPOSSIBLE);
return;
} // end if impossible
synchronized (worker.party) { // party since looking forward
to P4 requirements
while (worker.busyFlag) {
showStatus
(Status.WAITING);
try {
worker.party.wait();
}
catch
(InterruptedException e) {
} // end try/catch
block
} // end while waiting for
worker to be free
worker.busyFlag = true;
} // end
sychronized on worker
while (time < stopTime && noKillFlag) {
try {
Thread.sleep (100);
} catch (InterruptedException e)
{}
if (goFlag) {
showStatus
(Status.RUNNING);
time += 100;
pm.setValue
((int)(((time - startTime) / duration) * 100));
} else {
showStatus
(Status.SUSPENDED);
} // end if stepping
}
// end runninig
pm.setValue (100);
showStatus (Status.DONE);
synchronized (worker.party) {
worker.busyFlag = false;
worker.party.notifyAll ();
}
}
// end method run - implements runnable
public String toString () {
String sr = String.format ("j:%7d:%15s:%7d:%5d", jobIndex,
jobName, worker.index, jobTime);
return sr;
}
//end method toString
} // end class Job
Project 4 - Also see Grading
Extend project 3 to include making jobs wait until tools with the
resources required by the job are available at the restaurant.
Elaboration:
- Reading Job specifications from a data file and adding the
required resources to each Job instance.
- Resource pools - Restaurant.ArrayList <Tool> list of
tools at each restaurant, treated as resource pools, along with
supporting assignment to jobs.
- Eat (job) threads - using the resource pools and supporting
the concept of blocking until required resources are available
before proceeding.
- Synchronization - Since the jobs now also require tools, and
the tools are now held by restaurants, the synchronization
should be on the Restaurant of the job, not the Diner as in
Project 3.
- The Job threads should be efficient:
- If the diner is free and all the required tools are
available, the job should start updating the JProgress bar, as
in Project 3.
- Otherwise, the Job should not hold any resources if it
cannot progress.
- Use synchronization to avoid race conditions.
- Each job (Eat) thread should hold any required
synchronization locks for a very short period.
- When a job is over, all the resources used by the job (the
tools) should be released back to the restaurant.
- NOTE: If a job can never progress because the tools are not
available at the restaurant, the program should report this
and cancel the job.
- GUI showing:
- Resources in pools - how many tools are currently
available at each restaurant.
- Thread progress, resources acquired, and resources
requests still outstanding.
Grading
Deliverables - for all projects:
- Java source code files
- Data files used to test your program
- Configuration files used
- A well-written document including the following sections:
- Design: including a UML class diagram showing the type of
the class relationships
- User's Guide: description of how to set up and run your
application
- Test Plan:
- which data file is to be used and expected
results, and
- including test data and actual results,
- with screen snapshots of some of your test cases
- (optionally) Comments: design strengths and limitations,
and suggestions for future improvement and alternative
approaches
- Lessons Learned
- Use one of the following formats: MS Word doc, docx,
OpenOffice odt, pdf, rtf.
Your project is due by midnight, EST, on the day of the date
posted in the class schedule.
We do not recommend staying up all night working on your project -
it is so very easy to really mess up a project at the last minute
by working when one was overly tired.
Your instructor's policy on late projects applies to this
project.
Submitted projects that show evidence of plagiarism will be
handled in accordance with UMUC Policy 150.25 — Academic
Dishonesty and Plagiarism.
Format - Documentation format and length.
- See:
- The documentation describing and reflecting on your design and
approach should be written using Microsoft Word (or other common
word processor).
- Documentation should be of reasonable length.
- The font size should be 12 point.
- The page margins should be one inch.
- The paragraphs should be double spaced.
- All figures, tables, equations, and references should be
properly labeled and formatted using APA, IEEE or ACM style.
Coding hints:
- Code format:
- header comment block, including the following information in
each source code file:
- file name
- date
- author
- purpose
- appropriate comments within the code
- appropriate variable and function names
- correct indentation
- Errors:
- code submitted with errors will be returned ungraded
- Warnings:
- Your program should have no warnings
- Use the following compiler flag to show all warnings:
javac -Xlint
*.java
- More
about setting up IDE's to show warnings
- Generics - your code should use generic declarations
appropriately, and to eliminate all warnings
- Elegance:
- just the right amount of code
- effective use of existing classes in the JDK
- effective use of the class hierarchy, including features
related to polymorphism.
- GUI notes:
- The TITLE of the JFrame should include the program name,
your name, and the file name.
- GUI should resize nicely
- DO NOT use the GUI editor/generators in an IDE (integrated
development environment, such as Netbeans and Eclipse)
It is VERY difficult to get the display to resize nicely using
these tools
See the following for more about resizing:
- Do use JPanel, JFrame, JTextArea, JTextField, JButton,
JLabel, JScrollPane
- panels on panels gives even more control of the display
during resizing
- JTable and/or JTree for Projects 3 and 4
- Font using the following gives a nicer display for this
program, setting for the JTextArea jta:
jta.setFont (new java.awt.Font
("Monospaced", 0, 12));
- GridLayout and BorderLayout - FlowLayout rarely resizes
nicely
- GridBagLayout for extreme control over the displays
- you may wish to explore other layout managers
- ActionListener, ActionEvent - responding to JButton events
- Read and Display buttons should be distinct, and Display
should present the world.toString result - the CURRENT state
of the internal data structures.
- Starting with JDK 8, lambda expressions make defining
listeners MUCH simpler.
See the example below, with jbr (read), jbd (display) and
jbs (search) as three different JButtons.
jcb is a JComboBox
<String> and jtf is a JTextField.
jbr.addActionListener (e -> readFile());
jbd.addActionListener (e ->
displayWorld ());
jbs.addActionListener (e -> search
((String)(jcb.getSelectedItem()), jtf.getText()));
- JFileChooser - select data file at run time
- JProgressBar in Projects 3 and 4
- JSplitPane - optional, but gives users even more control
over display panels
- JTable - (optional) can give nice features, such as sorting,
but rather (VERY!) complex
Grading Table
Attributes |
Value |
Project design |
20 points |
Project functionality |
40 points |
Test data |
20 points |
Approach documentation |
15 points |
Approach documentation
grammar and spelling |
5 points |
Total |
100 points |
Package
- Dining
project data file creator - jar file
- Download
some data files and sample solution as a zip file.
- This package also includes a sample solution to Project 4.
- YOUR project should only take this as a thought about how to
approach the GUI interface.
- Data files included in the download:
- Codes:
- Countries, Restaurants per country, tools per restaurant,
diners per restaurant, dishes per diner, tools per dish
- CT = countries
- RC = restaurants per country
- TR = tools per restaurant
- DR = diners per restaurant
- DD = dishes per diner
- TD = tools per dish
-
CT RC TR DR DD TD
- 010a.txt
- 1 1 1 1 1 1
- 015a.txt
- 1 10 1 1 1 1
- 020a.txt
- 1 1 20 1 1 1
- 030a.txt
- 1 1 1 30 1 1
- 040a.txt
- 1 1 1 1 10 1
- 050a.txt
- 1 1 1 2 5 1
- 055a.txt
- 1 1 1 2 5 1
- 060a.txt
- 1 1 3 2 1 2
- 065a.txt
- 1 1 3 2 1 2
- 100a.txt
- 5 9 1 1 1 1
- 110a.txt
- 3 4 20 5 3 5
END.