辅导data structure、讲解Python编程、Java,c++程序辅导 解析C/C++编程|讲解Java程序

- 首页 >> Java编程
Part 1 – Delayed Stack (8%)
A stack is a data structure where the element removed is always the most recently added out of the
remaining elements. See: Stack (abstract data type) - Wikipedia. The two main operations are push(E)
which adds an element and pop() which removes an element.
A DelayedStack works like a normal stack except has a restriction ('delay condition') that prohibits
elements from being removed until a certain number of push operations have occurred. Once the
required number of push operations occur, any number of elements may be removed, however the
moment another element is added, the delay condition comes back into force.
Your task is to create a class called MyStack that implements the generic DelayedStack interface according
to the specification written in the docstrings for each method. Your class should be able to be instantiated
with
DelayedStack<...> s = new MyStack<...>(9);
where the ... can be replaced by any object, and the int parameter for the constructor represents the max
delay value (ie. number of push operations that must occur before pop operations can start to occur).
If the max delay value is changed, the change does not take effect until the next time the delay is reset to
the maximum (ie. when push occurs after a sequence of pop operations).
For this task you may not import anything from the java standard library, or external libraries.
Page 2 of 6
Examples (1)
DelayedStack s = new MyStack(4); //delay condition of 4
s.push(“first element”);
s.push(“something else”);
s.pop(); //return value is null, because so far only 2 elements have
been pushed
s.push(“third”);
s.push(“fourth”);
s.pop(); //return value is “fourth”
s.push(“another one”);
s.pop() //return value is null again, because the delay condition has
been reset
s.push(“2”);
s.push(“3”);
s.push(“4”);
s.pop(); // return value is “4”
s.pop(); // return value is “3”
s.pop(); s.pop(); s.pop(); s.pop();
//return values are “2”, “another one”, “third”, “something else”
Examples (2)
DelayedStack s = new MyStack(0); //delay condition of 0
means that there is never a restriction. Same with negative values, or
1.
s.push(“hello”);
s.pop(); //returns “hello”
s.setMaximumDelay(2);
s.getMaximumDelay(); //return value is 2
s.pop(); //IllegalStateException is thrown, the stack is empty
s.push(“X”);
s.push(“a”);
s.push(“b”);
s.push(“c”);
s.pop(); //return value is “c”
s.pop(); //return value is “b”
s.setMaximumDelay(4);
s.getDelay(); //return value is 0.
s.pop(); //return value is “a” – delay is not set until the next push
s.push(“Y”); s.push(“Z”);
s.setMaximumDelay(-1);
s.getDelay(); //return value is 2 – delay is not set yet
s.push(“An”);
s.getDelay(); //return value is 1
s.pop(); //return value is null
s.push(“AX”);
s.getDelay(); //return value is 0
s.pop(); //return value is “AX”
Page 3 of 6
Part 2 – Delayed Queue (8%)
A queue is a data structure where the element removed is always the oldest element (the one which has
been waiting the longest) out of the remaining elements, ie. the one which was added the least recently.
See: Queue (abstract data type) - Wikipedia. The two main operations are enqueue(E) which adds an
element and dequeue() which removes an element.
A DelayedQueue works like a normal queue except has a restriction ('delay condition') that prohibits
elements from being removed until a certain number of enqueue operations have occurred. Once the
required number of enqueue operations occur, any number of elements may be removed, however the
moment another element is added, the delay condition comes back into force.
Your task is to create a class called MyQueue that implements the generic DelayedQueue interface
according to the specification written in the docstrings for each method. Your class should be able to be
instantiated with
DelayedQueue<...> s = new MyQueue<...>(7);
where the ... can be replaced by any object, and the int parameter for the constructor represents the max
delay value (ie. number of enqueue operations that must occur before dequeue operations can start to
occur).
If the max delay value is changed, the change does not take effect until the next time the delay is reset to
the maximum (ie. when enqueue occurs after a sequence of dequeue operations).
For this task you may not import anything from the java standard library, or external libraries.
Page 4 of 6
Examples (1)
DelayedQueue s = new MyQueue(4); //delay condition of 4
s.enqueue(“first element”);
s.enqueue(“something else”);
s.dequeue(); //return value is null, because so far only 2 elements
have been pushed
s.enqueue(“third”);
s.enqueue(“fourth”);
s.dequeue(); //return value is “first element”
s.enqueue(“another one”);
s.dequeue() //return value is null again, because the delay condition
has been reset
s.enqueue(“2”);
s.enqueue(“3”);
s.enqueue(“4”);
s.dequeue(); // return value is “something else”
s.dequeue(); // return value is “third”
s.dequeue(); s.dequeue(); s.dequeue(); s.dequeue();
//return values are “fourth”, “another one”, “2”, “3”
Examples (2)
DelayedQueue s = new MyQueue(0); //delay condition of 0
means that there is never a restriction. Same with negative values, or
1.
s.enqueue(“hello”);
s.dequeue(); //returns “hello”
s.setMaximumDelay(2);
s.getMaximumDelay(); //return value is 2
s.dequeue(); //IllegalStateException is thrown, the queue is
empty
s.enqueue(“X”);
s.enqueue(“a”);
s.enqueue(“b”);
s.enqueue(“c”);
s.dequeue(); //return value is “X”
s.dequeue(); //return value is “a”
s.setMaximumDelay(4);
s.getDelay(); //return value is 0.
s.dequeue(); //return value is “b” – delay is not set until the next
push
s.enqueue(“Y”); s.enqueue(“Z”);
s.setMaximumDelay(-1);
s.getDelay(); //return value is 2 – delay is not set yet
s.enqueue(“An”);
s.getDelay(); //return value is 1
s.dequeue(); //return value is null
s.enqueue(“AX”);
s.getDelay(); //return value is 0
s.dequeue(); //return value is “c”
Page 5 of 6
Writing your own testcases
We have provided you with some test cases but these do not test all the functionality described in the
assignment. It is important that you thoroughly test your code by writing your own test cases.
For this assignment, you must write JUnit tests. You should place all of your test cases within two files:
MyStackTest.java and MyQueueTest.java, each within their respective components of the assignment.
Ensure that your tests can be run with JUnit as demonstrated in the lectures and tutorials.
Submission Details
You must submit your code and tests using the assignment page on Ed. To submit, simply place your files
and folders into the workspace, click run to check your program works and then click submit.
You are encouraged to submit multiple times, but only your last submission will be considered.
Marking
 10 marks will be assigned based on the results of the automatic tests and correctness of the
program (5 marks for part 1 and 5 marks for part 2). This component will use hidden test cases
that cover every aspect of the specification. Your program must match the exact output in the
examples and the test cases on Ed.
 5 marks will be assigned to the code coverage of the testcases you have written yourself (2.5
marks for part 1 and 2.5 marks for part 2). For this, we will use a script to automatically generate
a code coverage report using Jacoco. For this reason, please make sure you structure your
testcases in the manner described above.
 1 mark will be assigned for a manual check of your code style. Your code style should follow the
Google Java Style Guildeline. This includes things like appropriate indentation, vertical spacing
between functions and logical sections of code and horizontal spacing between conditional
statements and variable declarations. It will also cover adherence to OO design principles.
Page 6 of 6
Academic Declaration
By submitting this assignment you declare the following:
I declare that I have read and understood the University of Sydney Student Plagiarism: Coursework Policy
and Procedure, and except where specifically acknowledged, the work contained in this
assignment/project is my own work, and has not been copied from other sources or been previously
submitted for award or assessment.
I understand that failure to comply with the Student Plagiarism: Coursework Policy and Procedure can lead
to severe penalties as outlined under Chapter 8 of the University of Sydney By-Law 1999 (as amended).
These penalties may be imposed in cases where any significant portion of my submitted work has been
copied without proper acknowledgment from other sources, including published works, the Internet,
existing programs, the work of other students, or work previously submitted for other awards or
assessments.
I realise that I may be asked to identify those portions of the work contributed by me and required to
demonstrate my knowledge of the relevant material by answering oral questions or by undertaking
supplementary work, either written or in the laboratory, in order to arrive at the final assessment mark.
I acknowledge that the School of Computer Science, in assessing this assignment, may reproduce it
entirely, may provide a copy to another member of faculty, and/or communicate a copy of this assignment
to a plagiarism checking service or in-house computer program, and that a copy of the assignment may be
maintained by the service or the School of Computer Science for the purpose of future plagiarism checking.

站长地图