CSIS10B
Midterm Review Questions
SOLUTIONS
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
private int speed;
private int turnRate;
public Car(){
speed=0;
turnRate=0;
}
public void accelerate(int amount){
speed += amount;
if (speed > 100)
speed=100;
if (speed < 0)
speed=0;
}
public int getSpeed(){
return speed;
}
public void turnMoreLeft(){
turnRate-=1;
if (turnRate < -10)
turnRate = -10;
}
}
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.
import java.util.Scanner;
public class CarApp{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
Car racer = new Car();
System.out.println("What speed do you want to drive at?");
int desiredSpeed = in.nextInt();
Car.accelerate( desiredSpeed );
System.out.println("What turn rate do you want?");
int desiredTurnRate = in.nextInt();
// keep turning right as long as the current turn rate is too small
while ( racer.getTurnRate() < desiredTurnRate)
racer.turnMoreRight();
// keep turning left as long as the current turn rate is too big
while ( racer.getTurnRate() > desiredTurnRate)
racer.turnMoreLeft();
// note: if they are the same, no turn will be performed
}
}
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"
SOLN 1: "In Place" packing
int oldIndex = 0;
int newIndex = 0;
while ( oldIndex < data.length ) {
while ( data[oldIndex]==null ) // advance oldIndex past any null data
oldIndex++;
data[newIndex] = data[oldIndex]; // found real data. just copy from old position to new position
oldIndex++;
// advance both pointers toward end of array
newIndex++;
}
// done packing. now put nulls in to end of packed array
while ( newIndex < data.length ) {
data[newIndex] = null; // null-out the remaining cells in array
newIndex++;
}
OR
SOLN 2: "Temp Array" packing
int tempData = new int [ data.length ]
int oldIndex=0, newIndex=0;
while ( oldIndex < data.length ) {
while ( data[oldIndex]==null ) // advance oldIndex past any null data
oldIndex++;
tempData[newIndex] = data[oldIndex]; // found real data. just copy from old position to new
oldIndex++;
// advance both pointers toward end of array
newIndex++;
}
data = tempData; // copy tempData over to data (deletes old data vector at same time)
// no need to add nulls, tempData was initially filled with null on creation
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 = new Vector<String>{10);
v.add(0,"a");
v.add(0,"b");
v.remove(1);
v.add(1,"c");
v.add(0,"d");
elementCount:__3___
elementData:
"d"
"b"
"c"
null
null
null
null
null
null
null
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
System.out.println("weight = " + stats.get(2).getValue());
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.)
int k = v.size()-1;
while ( k >= 0){
System.out.println( v.get(k) );
k--;
}
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.
OPTION 1: USING OTHER Vector METHODS:
public void clear_to_beginning(int pos){
assert 0<=pos && pos < size();
while ( pos>0){
remove(0);
pos--;
}
}
// as long as we are not at beginning yet
// remove the first item of vector (auto reduces size by 1)
OPTION 2: "FROM SCRATCH"
public void clear_to_beginning(int pos){
for (int k=pos; k< size(); k++){
elementData[k-pos] = elementData[k];
}
elementCount-=pos; // subtract from elementCount the number of cells we removed
for (int k = elementCount; k<size(); k++)
elementData[k]=null;
// free up objects we are not using
}
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)?
test_b(4)
n=4
test_b(2)
n=2
test_b(0)
n=0
print "0 "
return
print "2 "
return
print "4 "
return
output:
0 2 4
b) Consider this function.
void foo( int counter)
{
if(counter == 0)
return;
else
{
System.out.println(""+counter);
foo(counter-1);
return;
}
}
What will the output be for foo(4)?
foo(4)
counter=4
print "4"
foo(3)
counter=3
print "3"
foo(2)
counter=2
print "2"
foo(1)
counter=1
print "1"
foo(0)
return
return
return
return
return
output:
4
3
2
1
c) Consider this method:
void go( int k)
{
if(k == 0)
return;
else
{
System.out.println("hello" + k);
go(k-1);
System.out.println(""+k);
return;
}
}
If the method is called with the value 4, what will
the output be? Explain.
go(4)
k=4
print "hello 4"
go(3)
k=3
print "hello 3"
go(2)
k=2
print "hello 2"
go(1)
k=1
print "hello 1"
go(0)
k=0
return
print "1"
return
print "2"
return
print "3"
return
print "4"
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
a) O(n2)
b) O(n3)
c) O(n)
© Copyright 2026 Paperzz