CS 15-111
Exam One
Study Topics
Public <return-type> <method-name> ( <parameter-list>)
{
}
}
WRITING NEW CLASS METHODS
1. Add a new method to the RandomMatrix class in lab2 to do the following
a. if the matrix is 2 by 2 then find the inverse
of the matrix using
inverse [ a
b] is 1/(ad - bc) * [ d -b]
[ c d]
[ -c a]
b. return true if the matrix is symmetric. i.e.
A[i][j] = A[j][i] for all i,j
c. a method isSquare that returns true
if the matrix is sqaure ie. rows = columns
2. Consider the elevator class defined in class. Write a method,
pickPassenger that will take a passenger object
P as an object and
add to the internal vector of passengers "in-order" of
the destination.
TRACING/WRITING CODE
1. Assume the world of Karel the Robot.
What is the intent of the following code? Describe in words. Can you
simplify it?
while (frontIsClear()) {
if (frontIsClear()) {
move();
} else {
turnAround();
}
}
2.
while (i < 5) {
if (i < 2) { turnLeft();
i = i + 1;}
else
{ turnLeft();
turnLeft(); turnLeft(); i = i + 2;}
}
Assume that the Robot is facing east. Find the final direction
if:
i = 1
i = 5
i = 3
3. Study the class defintion of matrix
public class Matrix
{
public Matrix() {}
// unallocated vectors
public Matrix(int n, int m); // dimensions given
public Matrix(int n, int m, final int fillValue);
// and
default fill value
private int NumRows ;
private int NumColumns ;
public int final getRows();
public int final getColumns();
public void SetDimensions(int
r, int c);
};
a. How many methods are accessors? How many are mutators? How many are
constructors?
b. Show Three different ways of creating a matrix.
c. Using the available methods only, write new methods to find the
a. A boolean method that returns
true if the matrix is symmetric (i.e M[i][j]=M[j][i])
b. A boolean method to return
true if the matrix is square.
================================
4. What relationship is satisfied for class composition? Class inheritance?
5. If passenger class object is contained in a elevator class, does
elevator class
has access to all the methods (private and public)
of the passenger class?
6. "A Meal has a vegetable", which class contains an object from the
other class?
7. Explain the code: OnePerson(int I, string n){id=i; name=n;}
FIND SYNTAX/LOGIC ERRORS
1. Find ALL syntax errors of the following code:
if facingnorth() )
{ turnLeft() }
else;
{ Move() ; }
2. Find all syntax errors
while (frontIsClear)
{ move();}
3. find redundant code
if (i < 1)
{ move(); }
else
if (i >=1)
{ turnLeft();}
else
{ pickBeeper();}
©Copyright 2001 Ananda Gunawardena. All rights reserved.
This document was last updated on 08/15/01 23:22:56