Review

CSIS10B
Midterm Review Questions
You should be able to:
1) Implement a simple class given its definition and a sample demonstration (main) test program.
Example: Write the class definition for the car class described below. Actually just do the constructor,
accelerate, getSpeed, turnMoreLeft methods. Define instance variables needed to maintain the
state of the car. An app is provided on the next page showing the Car class in use.
Class Car
This class encapsulates the speed and turn rate of a car for a racing game.
Constructor Summary
Car()
Creates a new car, with a current speed of zero and turn rate of zero.
Method Summary
void accelerate(double amount)
Changes the speed by the given amount, in miles per hour. If the current speed exceeds 100, then it is
set to 100 exactly. If it becomes less than zero, then it is set to zero exactly.
double getSpeed()
Returns the current speed, in miles per hour.
int getTurnRate()
Returns the current turn rate.
void turnMoreLeft()
Decreases the turn rate by 1. If the turn rate becomes less than -10 then it is reset to -10.
void turnMoreRight()
Increases the turn rate by 1. If the turn rate becomes more than 10 then it is reset to 10.
(just do the constructor, accelerate, getSpeed, turnMoreLeft methods)
public class Car{
// declare instance variables
}
Example main application:
Car racerX = new Car();
racerX.accelerate(25);
racerX.turnMoreLeft();
racerX.turnMoreLeft();
Displays on Console:
System.out.println("RacerX speed: "+
racerX.getSpeed());
System.out.println("RacerX turn rate: "+
racerX.getTurnRate());
RacerX speed: 25
RacerX turn rate: -2
2) Use the class you define in 1 in a new application
Example: Write a main application that creates a car object. Ask the user how fast the car should be
going and what turn rate they want, then modify the car object so it matches those values entered.
public class CarApp{
public static void main(String [] args){
}
}
3) Code a for-loop that generalizes an array operation (like insert or remove) given a specific example
to base the algorithm on.
Example: given an array of String, data, of some length, that contains several null values, write code that
"packs" the array, replacing null values with data, while maintaining the original order of the values.
Here is a sample "before" and "after" picture of the array, although the array can be any mix of values
and null. The array should keep its original size:
before:
data
"a"
"c"
null
"e"
null
null
"p"
null
"s"
null
"c"
"e"
"p"
"s"
null
null
null
null
null
after:
data
"a"
4) Draw the "logical" contents of a Vector after several operations. (Show the array with values it
contains)
Example: Suppose you use the java.util.Vector class in the following program segment. What will v's
member variables look like after we execute these statements:
Vector<String>
v.add(0,"a");
v.add(0,"b");
v.remove(1);
v.add(1,"c");
v.add(0,"d");
elementCount:_____
elementData:
v = new Vector<String>{10);
5) Draw the "memory map" of a data structure (could be Vector, or Vector of Association)
Example: draw the memory map of the Vector created by statements. (
Vector<String> v(10);
for (int k=0; k<4; k++)
v.add(k, "" + char('a'+k));
Example: draw the memory map of this Vector:
Association<String,String> attrib = new Association<String,String>("height","72");
Association<String,String> attrib2 = new Association<String,String>("age","32");
Association<String,String> attrib3 = new Association<String,String>("weight","168");
Vector<Association<String,String> stats = new Vector<Association<String,String>();
stats.add(attrib);
stats.add(attrib2);
stats.add(attrib3);
6) Retrieve data from a Vector of Associations, (or Vector of Vector of Associations!)
Example: print out just the weight by referring only to the stats vector
7) Use a Vector in a simple application
Example: Suppose you have a Vector v with around 1000 Students already inserted into it, but the exact
number is unknown. Write some code that will display all the Students in v in reverse order (starting at
the end and working backwards to the beginning.)
8) Define a new member function for the Vector class
Example: Write a member function clear_to_beginning(int pos ); That accepts a valid array index
pos and removes all the elements in front of pos. After calling this function, the value at index pos is at
the beginning of the Vector and the elementCount has been reduced accordingly.
9) Trace a recursive function
a) Consider the following function:
void test_b(int n)
{
if (n>0)
test_b(n-2);
System.out.print( n + " " );
}
What is printed by the call test_b(4)?
b) Consider this function.
void foo( int counter)
{
if(counter == 0)
return;
else
{
System.out.println(""+counter);
foo(counter-1);
return;
}
}
This recursion is not infinite, assuming the method is passed a positive integer value. What will the output
be for foo(4)?
c) Consider this method:
void go( int k)
{
if(k == 0)
return;
else
{
System.out.println("hello" + counter);
go(k-1);
System.out.println(""+counter);
return;
}
}
If the method is called with the value 4, what will the output be? Explain.
10) Determine the Big-O of some code snippets
http://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/
http://leepoint.net/notes-java/algorithms/big-oh/bigoh.html
Great java ref: http://www.leepoint.net/notes-java/index.html