CS 1723-001 Data Structures
Fall 2001 -- Review for Exam I
Sample Questions (New! NOTE: at 9:45 pm,
I'm not adding any more answers. Good luck tomorrow.):
(Note: I will add material to this,
including answers,
on Tuesday, 16 October 2001. Notice that Exercise 3
has an answer at the bottom.)
- Using "wrapper" classes: (
Answer to both 1 and
2 Don't
look at until you have tried the problem.)
Start with a double double d = 47.5.
- Show how to wrap d so that we end up with
a Double dWrap that contains the same number
47.5 inside.
- Show how to unwrap this class to get the value of the
double that is inside back out.
- Using the Java Stack class: (
Answer to both 1 and
2 Don't
look at until you have tried the problem.)
- Show how to create an instance of the Java
Stack class.
- Show how to push the double above onto the stack in a,
- Show how to pop (remove) this stack element and then
how to get the value of the
double that was wrapped on the stack.
- Implementing stacks from scratch:
In a separate class (perhaps called DoubleStack),
allocate an array of doubles, and use an
integer
variable top to write methods
pop, push, and
empty that will implement a stack of
doubles. (Don't do any error checking
or checking for a full stack or an empty stack.)
- Implementing a stack of Comparables
from scratch:
- Show where you would change the previous stack so that
it would be a stack of items declared Comparable
items.
- With this stack (perhaps called
ComparableStack), what does it mean to say
the stack implements Comparable?
- What were the circumstances in class under which we
wanted the Comparable interface implemented?
- Strings: (
Answer
Don't
look at until you have tried the problem.)
Using the charAt method of the String
class, show how to decide if a string is a palindrome,
that is, reads the same forwards as backwards, such as the
famous words that Adam and Eve might have exchanged
(with the blanks omitted): "madam im adam" and
"name no one man", or Napolean's possible
words (blanks can stay):
"able was i ere i saw elba".
- Extending classes: (
Partial
Answer Don't
look at until you have tried the problem.)
Consider a simple class hierarchy (adapted and simplified from
your Weiss text), with classes
Person, Employee,
Student, and Persons:
class hierarchy
- Write a new class Faculty with a new
int data field
courseLoad that gives the number of hours the
faculty member is teaching. Show additions to the
Persons class to add another person to
the array: a new faculty member with name
Warner, Joshua,
ssn 563-55-5719, age 47, salary
53000, and course load 9.
- Suppose you want to add objects of type Person
to a list of Comparable items, as we did
in class. Suppose we then want to remove the objects in
alphabetic order by name, using the removeMin
method in the general CompList class we
had before. What changes and additions are needed to the
class hierarchy above? (Then this becomes a priority queue,
where the priority is the alphabetic position of the name
(people get preference who come early in the alphabet).)
- Suppose instead you wanted removeMin to
remove in decreasing order of age. Show the changes to (b)
above. (Here it is a priority queue, with age as the priority.)
- RPN: (
Answer Don't
look at until you have tried the problem.)
Consider the following arithmetic expression:
3*5^(4-2)+6
- Use the method from class to translate this expression
to RPN. (Do it step-by-step, using the stack and showing steps.)
- Use the method from class to evaluate the resulting RPN.
(Again do it step-by-step, showing the use of a stack.)
Material presented in class that might be covered on the exam:
- Basics of object-oriented programming:
- Simple List class to maintain a list
of ints. (Only works for integers.)
Final source.
This illustrates a class with methods.
- The same list of ints, with an interface
Notes. This illustrates the
use of an interface just to give the methods that are supposed
to be supplied.
- The use of "wrapper" classes to allow basic types to have
a reference to them. (See
Week 2 Notes.)
- The Comparable interface.
(See
Week 2 Notes.)
- Creating a list of Comparable items.
The one here is very similar to the list of ints
given above, except that at various places, a Comparable
appears instead of an int.
The important thing is to use this list to hold various types:
(See
Notes.)
One can even use this list to keep track of a user-defined
type such as rational numbers: See
Notes.
- The Stack, its Use and Implementation: (See
Week 3 Notes.)
- Use of the Java Stack class.
Since this in a stack of references, if basic types are
pushed, they must be wrapped, as is illustrated in these
Notes to give
a stack of ints. (Notice that this uses
the Java Labrary Stack class.)
- The implementation of a simple stack of ints
using an array of ints:
Notes.
(Notice that this is done from scratch, without using any
part of the Java Library.)
- Here is the use of the Java library Stack
class to push several different types onto the same stack:
Notes.
(One wouldn't normally want to do this.)
- Strings: (See
Week 4 Notes.)
This includes especially a list of useful String methods.
- Extending classes: (See
the example of Progressions,
which is part of the Week 4 notes.)
- Queues: (See
Week 5 Notes.)
- RPN Evaluation and Translation To: (See
Week 6 Notes.)
- The form of RPN.
- Evaluation of an RPN expression.
- Translation of an arithmetic expression to RPN.
- Combination of 2 and 3 to form an evaluator of arithmetic
expressions.
- Simulation: (See
Week 7 Notes.)
This material will not be on the exam.
Projects and Exercises:
Recitations: See the List
of Recitations.
Revision date: 2001-10-12.
(Use ISO 8601,
an International Standard.)