Data Structures and Algotirhtms

trujsobsk
hwk3.pdf

CS241 - Fall 2017 - Assignment #3

Assigned: October 17th, 2017 Due: October 26th, 2017

Collaboration policy: The goal of homework is to give you practice in mastering the course material. Consequently, you are encouraged to collab- orate with others (groups of at most three). In fact, students who form study groups generally do better on exams than do students who work alone. If you do work in a study group, however, you owe it to yourself and your group to be prepared for your study group meeting. Specifically, you should spend at least 30–45 minutes trying to solve each problem beforehand. If your group is unable to solve a problem, it is your responsibility to get help from the instructor before the assignment is due. You must write up each problem solution and/or code any programming assignment by yourself without assistance, even if you collaborate with others to solve the problem. You are asked to identify your collaborators. If you did not work with anyone, you must write “Collaborators: none.” If you obtain a solution through research (e.g., on the web), acknowledge your source, but write up the solution in your own words. It is a violation of this pol- icy to submit a problem solution that you cannot orally explain to the instructor. No other student may use your solutions; this includes your writing, code, tests, documentation, etc. It is a violation of this policy to permit anyone other than the instructor and yourself read-access to the location where you keep your solutions.

1

Submission Guidelines: Your group has to submit your work on Black- board by the due date. Only one submission per group is necessary. Just make sure that you identify your group in the header by putting all names in the “author” field. For each of the program- ming assignments you must use the header template provided in Blackboard. The header must contain, your name, course number, semester, homework number, problem number, and list of collaborators (if any, otherwise put “none”). Your answers to questions that do not require coding must be in- cluded in this header as well. Your code must follow the Java formatting standards posted in Blackboard. Format will also be part of your grade. To complete your submission, you have to upload two files to Blackboard: your source file and your class file. The submission will be returned without grading if any of these guidelines is not followed.

Style and Correctness: Keep in mind that your goal is to communi- cate. Full credit will be given only to the correct solution which is described clearly. Convoluted and obtuse descriptions might receive low marks, even when they are correct. Also, aim for concise solutions, as it will save you time spent on write-ups, and also help you conceptualize the key idea of the problem.

2

Assignment 3

Programming Assignment Grading Rubric: The following rubric applies only to programming assignments.

Program characteristic

Program feature Credit possible

Design 30%

Algorithm 30%

Functionality 30%

Program runs without errors

20%

Correct result given 10%

Input 15%

User friendly, typos, spacing

10%

Values read in correctly

5%

Output 15%

Output provided 10%

Proper spelling, spacing, user friendly

5%

Format 10%

Comments, name 5%

Indentation 5%

TOTAL 100%

1(40) 2(20) 3(20) 4(20) TOTAL(100)

3

Assignment:

1. (40 points) Implement a generic class MoveToFront〈T〉 that maintains a collection of items of generic type T in LIFO order (last-in first-out) without duplicates. (Similar to a Stack but without duplicates.) Use the class LinkedList〈T〉 in the Java API to implement MoveToFront. Your class must provide the following instance methods.

push: that adds an item to the collection, removing previous occurrences.

pop: that removes the item added the latest to the collection.

contains: that returns true if a given item is in the collection, or false otherwise.

isEmpty?: that returns true if the collection is empty, or false otherwise.

2. (20 points) Write a testMTF class with a main() method that tests all operations of the MoveToFront class above. Override the toString() method of the class Object, and use it to display all the items in the collection after each method is called.

3. (20 points) Compute the big-O running time of each instance method, explaining the running time (basic operation counted, number of iter- ations in loops, etc.)

4. (20 points) Now assume that MoveToFront was implemented with ArrayList instead of LinkedList. Re-compute the big-O running time of each instance method, explaining the running time (basic operation counted, number of iterations in loops, etc.)

4