Supplemental Problems extend the range of problems considered in the course and help sharpen problem-solving skills.
Each program for a supplemental problem must follow these instructions:
Include within your code, javadoc documentation for
/***
* equals is a method
* @param first is a String
* @param last is a String
* @returns a boolean value
*/
equals (String first, String last)
/*****************************************************************
* Henry M. Walker *
* PO Box Science II *
* Program for CSC 207 *
* A Simple Class Hierarchy *
* Assignment for Friday, September 28 *
*****************************************************************/
/* ***************************************************************
* Academic honesty certification: *
* Written/online sources used: *
* [include textbook(s), *
* CSC 207 labs or readings; *
* complete citations for Web or other written sources] *
* [write "none" if no sources used] *
* Help obtained *
* [indicate names of instructor, class mentors *
* or evening lab tutors, consulted *
* according to class policy] *
* [write "none" if none of these sources used] *
* My signature below confirms that the above list of sources *
* is complete AND that I have not talked to anyone else *
* [e.g., CSC 161 students] about the solution to this problem *
* *
* Signature: *
*****************************************************************/
if ((no_comments)
|| (missing pre- or post-conditions)
|| (formatting_does_not_show_structure)
|| (long_methods_not_divided_into_sections_with_clarifying_comments)
|| (no_evidence_of_compilation)
|| (no_test_plan___no_listing_of_circumstances__OR__no_listing_of_test_cases)
|| (no_test_runs)
|| (no_commentary_on_correctness))
|| (no_signed_certification_regarding_sources_and_help)
return (no_grade);
Two grading forms will be used in the grading of each supplemental problem.
This problem introduces some simplified elements related to a program to track shopping in a grocery store. Many details would be expanded greatly for a real application, but this problem might make a [small] start.
For this problem, submit your code to the GitHub Classroom assignment for https://classroom.github.com/a/nJNLE3TG.
Items available in a grocery store might be categorized as produce (e.g., vegetables, fruit, cheese, meat, etc.), beverages (bottled water, milk, soft drinks, energy drinks, etc.), and packages (boxes of crackers, noodles, tea, etc.). A class hierarchy to model this categorization of items follows:
To clarify,
All materials in a store fall within a general category of a class Item.
Class Produce is a subclass of Item of fresh, uncooked ingredients.
A Beverage is an Item for drinking.
A Package is a subclass of Item that comes in a rectangular box.
For this problem, you should implement the following:
Class definitions for Item and its subclasses Produce, Beverage, and Package
A ShoppingCart class, modeled upon the SchoolDirectory class from the CSC 207 lab Generalization, Polymorphism, and Exceptions. The ShoppingCart class should have these features:
Prolog: Many discussions of data organization focus on individual data structures, such as data organized into linked lists or trees.
This problem suggests one way in which multiple data structures might be combined in solving a multi-faceted problem.
An alternative solution to problem might utilize a database, such as MySQL, for data storage and retrieval. Although databases are widely used in contemporary applications, database technology is beyond the scope of CSC 207, and the solution of this supplemental problem is designed to provide practice with both singly-linked lists and binary search trees.
Problem Statement: An abbreviated system containing student data will store both usernames and credits toward graduation.
To allow efficient inserting, searching, and printing of student information, the student data will be stored in a binary search tree, ordered by username.
To allow printing of students in increasing order of credits toward graduation, the student data will also be stored in a singly-linked list, ordered by credits.
Methods for this problem include:
/** * a single new node is created, * containing a student's username and credits toward graduation * @pre * username is not NULL * @post * the new node is inserted to the binary search tree * containing all student records, ordered by username * @post * the node is also inserted into a singly-linked list, * ordered by credits toward graduate */ public void insert (String username, double credits); /** search tree for username to obtain credits for graduation * @pre * none * @returns * if the username appears in a node, * the credits toward graduate are returned * @throws * if the username does not appears in a node, * throws NoSuchElementException */ public double findCredits (String username); /** * update credits toward graduation for * @pre * username is not NULL * @pre * newCredits gives the new number of credits for the username * @post * credits toward graduation is updated with the new parameter * @post * node is not moved on the binary search tree * @post * node's position on the linked list is adjusted as needed, * to maintain the ordering of list nodes in ascending order * by credits toward graduation * @returns * true if username found (with node updated and moved as needed) * false if username not found */ public boolean updateCredits (String username, double newCredits); /** * print username and corresponding credits toward graduation, * ordered by username * @post * each username/credits value printed on a separate line, * ordered by username */ public void printByUsername (); /** * print username and corresponding credits toward graduation, * ordered by credits toward graduation * @post * each username/credits value printed on a separate line, * ordered by credits toward graduation */ public void printByCredits ();
Programming Notes:
For each student, the username and credits toward graduation will be stored exactly once. (Storage of data in multiple places invites inconsistencies to arise, after data are updated in one location, but not others.)
Since each node will be contained in both a binary search tree and a linked list, two sets of points are needed.
Consistent with needs for data storage and references, a StudentNode class must contain the following fields:
private String username; //always stored in lowercase
private double credits; //credits toward graduation
private left; //pointer to left subtree within a tree structure
private right; //pointer to right subtree within a tree structure
private next; //pointer to next node in singly-linked list
These StudentNodes should be utilized within a Registrar class that contains root and first references for the root of the tree and the beginning of the singly-linked list.
Example: An illustration showing 4 nodes in this structure follows.
When a printer serves multiple users, users may sent several print requests to the operating system, but only one file can be printed at a time. Thus, the operating system can direct a file from one print request to the printer, but the other requests must be stored in some type of data structure. The simplest approach involves a single queue. Each user sends one or more files to the operating system, and the operating system places the print requests on a queue. The operating system then sends the files to the printer one-at-a-time from the print queue. Of course, once a printer starts to print a file, the printer must complete that job before moving onto the next print request. Although this approach is simple and treats all jobs equally, this approach has the disadvantage that many short printing jobs (of only a few pages) may have to wait substantial amounts of time for a large print job (hundreds of pages) to finish.
One way to give some preference to certain print jobs is to create multiple queues of varying priorities. For convenience of notation, suppose a system has n queues, labeled q0, q1, q2, ..., qn-1. Suppose qi has priority i, where priority 0 is top priority, priority 1 is the next highest priority, ..., and priority n-1 is the lowest priority. That is requests in q0 are chosen before print requests in any other queue, and requests in qn-1 must wait until requests from all other queues have been printed.
The division of print requests into queues of varying priorities can work well in many cases, but this approach has the drawback that low priority print requests may never be printed if a steady stream of higher priority requests enter the system. In operating-systems jargon, this situation is called starvation; some work is never done because the system spends all of its time on other processing.
To avoid the possibility of starvation, a typical approach is to periodically move a print request from a low-priority queue to the next higher priority queue, as this would cause any print request to eventually move up to the head of the highest priority queue. To specify this promotion from one queue to the next more precisely, suppose that every time k requests from queue qi have been sent to the printer, the system removes a request from queue qi+1 and enqueues the request on queue qi.
The Printing Protocol
The following diagram provides an overview of the process of user submission of print requests, their placement on an appropriate queue, and their subsequent transmission to the printer for printing:
The formal protocol follows:
At any time, a user may send a print request to the operating system.
The operating system uses an enqueue operation to place the print request on a queue, based on the number of pages to be printed
If the printer is idle, the operating system will select a print request from the highest priority non-empty queue.
If the printer is busy, the operating system simply collects print requests in its queues.
If the printer completes a print request, the operating system processes print requests as follows:
Determining Initial Print-request Priority
The print-queue protocol above requires the operating system to determine which queue should be used for each new print request submitted by the user. Three assignment approaches follow:
Problem to be Solved
Supplemental Problem 3 is to investigate the extent to which multiple queues and different assignment algorithms have an impact on the average waiting time for users' print requests.
For this problem, submit your code to the GitHub Classroom assignment for https://classroom.github.com/a/xNeusa_Y.
For the purposes of this problem, suppose that printing one page requires one unit of time. The simulation described below will cover 1000 units of time, and the simulation will need a "clock" variable that keeps time units 0, 1, 2, ..., 999.
To investigate the impact of different queue-selection strategies, testing should include each of the queue-selection approaches and several loads (numbers of Print Requests entering over the simulation).
Draft problem-specific grading form
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/207.sp13/suppl-prob.shtml
|
created 22 May 2008 last revised 23 April 2013 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |