Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this java problem ( TODO parts ) : package ps 1 ; / * * Cons is a simple cons cell

I need help with this java problem(TODO parts):
package ps1;
/** Cons is a simple cons cell record type. */
class Cons {
RatPoly head;
Cons tail;
Cons(RatPoly h, Cons t){ head = h; tail = t; }
}
/** RatPolyStack is a mutable finite sequence of RatPoly objects.
Each RatPolyStack can be described by [p1, p2,...], where [] is
an empty stack, [p1] is a one element stack containing the Poly
'p1', and so on. RatPolyStacks can also be described
constructively, with the append operation, ':'. such that [p1]:S
is the result of putting p1 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 0, the
size of [p1] is 1, the size of [p1, p1] is 2, 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 O(n) time to retrieve an element
at some arbitrary depth, but pushing and popping elements should
be O(1) time.
*/
public class RatPolyStack {
private Cons polys; // head of list
private int size; // redundantly-stored list length
public void div(){
//TODO: Fill in this method, then remove the RuntimeException
throw new RuntimeException("RatPolyStack->div() unimplemented!
");
}
/** Integrates the top element of this, placing the result on top
of the stack.
@requires this.size()>=1
@modifies this
@effects If this =[p1]:S
then this_post =[p2]:S
where p2= indefinite integral of p1 with integration constant 0
*/
public void integrate(){
//TODO: Fill in this method, then remove the RuntimeException
throw new RuntimeException("RatPolyStack->integrate() unimplemented!
");
}
/** Differentiates the top element of this, placing the result on top
of the stack.
@requires this.size()>=1
@modifies this
@effects If this =[p1]:S
then this_post =[p2]:S
where p2= derivative of p1
*/
public void differentiate(){
//TODO: Fill in this method, then remove the RuntimeException
throw new RuntimeException("RatPolyStack->differentiate() unimplemented!
");
}
/** Returns the number of RayPolys in this RatPolyStack.
@return the size of this sequence.
*/
public int size(){
//TODO: Fill in this method, then remove the RuntimeException
throw new RuntimeException("RatPolyStack->size() unimplemented!
");
}
/** Checks to see if the representation invariant is being violated and if so, throws RuntimeException
@throws RuntimeException if representation invariant is violated
*/
private void checkRep() throws RuntimeException {
if(polys == null){
if(size !=0)
throw new RuntimeException("size field should be equal to zero when polys is null sine stack is empty");
} else {
int countResult =0;
RatPoly headPoly = polys.head;
Cons nextCons = polys;
if(headPoly != null){
for (int i =1;; i++){
if(nextCons != null){
countResult = i;
nextCons = nextCons.tail;
} else
break;
}
}
if(countResult != size)
throw new RuntimeException("size field is not equal to Count(s.polys). Size constant is "+size+" Cons cells have length "+countResult);
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

50 Tips And Tricks For MongoDB Developers Get The Most Out Of Your Database

Authors: Kristina Chodorow

1st Edition

1449304613, 978-1449304614

More Books

Students also viewed these Databases questions

Question

6. Write a job specification.

Answered: 1 week ago