Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help me with this java problem ( finish todo parts ) : package ps 1 ; / * * Cons is a simple cons
Please help me with this java problemfinish todo parts:
package ps;
Cons is a simple cons cell record type.
class Cons
RatPoly head;
Cons tail;
ConsRatPoly h Cons t head h; tail t;
RatPolyStack is a mutable finite sequence of RatPoly objects.
Each RatPolyStack can be described by p p where is
an empty stack, p is a one element stack containing the Poly
p and so on RatPolyStacks can also be described
constructively, with the append operation, : such that p:S
is the result of putting p at the front of the RatPolyStack S
A finite sequence has an associated size, corresponding to the
number of elements in the sequence. Thus the size of is the
size of p is the size of p p is and so on
Note that RatPolyStack is similar to lists like @link
java.util.ArrayList with respect to its abstract state a finite
sequence but is quite different in terms of intended usage. A
stack typically only needs to support operations around its top
efficiently, while a vector is expected to be able to retrieve
objects at any index in amortized constant time. Thus it is
acceptable for a stack to require On time to retrieve an element
at some arbitrary depth, but pushing and popping elements should
be O time.
public class RatPolyStack
private Cons polys; head of list
private int size; redundantlystored list length
Definitions:
For a Cons c let Seqc be if c null,
chead:Seqctail otherwise
Countc be if c null,
Countctail otherwise
These are helper functions that will make it easier for us
to write the remainder of the specifications. They are
seperated out because the nature of this representation lends
itself to analysis by recursive functions.
Abstraction Function:
RatPolyStack s models Seqspolys
This explains how we can understand what a Stack is from its
'polys' field. Though in truth, the real understanding comes
from grokking the Seq helper function
RepInvariant:
ssize Countspolys
This defines how the 'size' field relates to the 'polys'
field. Notice that spolys null is not a given Invariant;
this class, unlike the RatPoly class, allows for one of its
fields to reference null, and thus your method implementations
should not assume that the 'polys' field will be nonnull on
entry to the method, unless some other aspect of the method
will enforce this condition.
@effects Constructs a new RatPolyStack,
public RatPolyStack
size ;
polys null;
Pushes a RatPoly onto the top of this.
@requires p null
@modifies this
@effects thispost p:this
public void pushRatPoly p
TODO: Fill in this method, then remove the RuntimeException
throw new RuntimeExceptionRatPolyStackpush unimplemented!
;
Removes and returns the top RatPoly.
@requires this.size
@modifies this
@effects If this p:S
then thispost S && returns p
public RatPoly pop
TODO: Fill in this method, then remove the RuntimeException
throw new RuntimeExceptionRatPolyStackpop unimplemented!
;
Duplicates the top RatPoly on this.
@requires this.size
@modifies this
@effects If this p:S
then thispost p p:S
public void dup
TODO: Fill in this method, then remove the RuntimeException
throw new RuntimeExceptionRatPolyStackdup unimplemented!
;
Swaps the top two elements of this.
@requires this.size
@modifies this
@effects If this p p:S
then thispost p p:S
public void swap
TODO: Fill in this method, then remove the RuntimeException
throw new RuntimeExceptionRatPolyStackswap unimplemented!
;
Clears the stack.
@modifies this
@effects thispost
public void clear
TODO: Fill in this method, then remove the RuntimeException
throw new RuntimeExceptionRatPolyStackclear unimplemented!
;
Returns the RatPoly that is 'index' elements from the top of
the stack.
@requires index && index this.size
@effects If this S:p:T where Ssize index, then
returns p
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started