Overview | Schedule | Resources | Assignments | Home |
For this problem you are to implement a rational number abstract data type as a Java class according to the following characteristics:
Rational
.
int
instance variables (fields) called
numerator
and denominator
(representing the fraction numerator/denominator).
void display()
which displays the rational number as
numerator / denominator
void assign(int x, int y)
where x
will be the numerator value and
y
will be the denominator value, with the
precondition that y
cannot be zero (i.e., you need not
worry about zero in the denominator)
boolean invert()
which returns false if the original
numerator is zero and otherwise swaps the
numerator and denominator and returns true
void mult( rational p )
which multiplies the owner by p
and stores the product in the owner. In case
you forgot, 1/6 x 2/5 = 2/30. For extra
credit, store the result in reduced form
(e.g., 1/15).
Consider the following Haskell function:
mystery([ ],[ ]) = true mystery([ ],[h:t]) = false mystery([h:t],[ ]) = false mystery([h1:t1],[h2:t2]) = mystery(t1,t2)
mystery([ ],[ ])
?mystery(['a', 'b'], ['d', 'e'])
?mystery(['a', 'b'], ['d'])
?mystery
computes.Consider the following Haskell function:
test(32)=0 test(x) = 1 + test(2*x)
What is returned by each of the following?
test(16)
test(4)
test(33)
Write a Haskell function, addup
, which adds the first
n
integers. For example addup(3)
would evaluate to 6.
Consider the following definitions:
private Listcell myRef; private class Listcell { private char data; private Listcell next; } void fooAux(String str, int i, Listcell prevFront) { if (i == str.length()) { return; } else { Listcell tmp = new Listcell(); tmp.data = str.charAt(i); tmp.next = prevFront; myRef = tmp; fooAux(str, i+1, myRef); } } void foo(String str) { myRef = null; fooAux(str, 0, myRef); }
foo("cat hat");
foo
does in one sentence.foo
so that it accomplishes its task using iteration.Given the doubly linked list node definition
private class Node { int elem; Node next; Node prev; }
write a code fragment which will delete the node referenced by
p
if that node has a node to its left (prev
)
and right (next
).
Questions, short answer and otherwise, could also be asked about the following topics:
Overview | Schedule | Resources | Assignments | Home |
DePauw University,
Computer Science Department,
Spring 2006
Maintained by Brian Howard
(bhoward@depauw.edu
).
Last updated