Program Design II – Exam #2
Unless specified otherwise, write your answers on the provided answer
sheets. You may refer to the command-sheets (without commentary) handed out in class.
- (4 pts) Provide two facts about abstract classes.
- (4 pts) Suppose the following class is used to represent a point on an x-y coordinate
plane in geometry:
public class Point
{
private double x;
private double y;
// suppose a constructor and other useful methods are here too!
}
You are working on a program that works with various geometric objects.
You recall that a circle has a point (the center) and a radius and you
realize you could create a Circle class that inherits from Point
so that it would start with the needed (x,y) coordinates for the
center. Is this a good use of inheritance or not? Explain.
- Suppose you have been asked by Hardin-Simmons University to create a program to track students. In doing so you have created the following class to represent a student:
public class Student {
private int id;
private String name;
private double gpa;
public Student(int id, String name) {
this.id= id;
this.name= name;
this.gpa= 0.0;
}
public boolean qualifiesForScholarship() {
return gpa >= 3.0;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa= gpa;
}
public String toString() {
return name + " (" + id + ") " + gpa;
}
}
- (4 pts) Some students participate in athletics and for those
students we need to be able to store a code for which sport they play
(0=soccer, 1=basketball, 2=track) and the number of years they have
participated. Create a new class called StudentAthlete that
inherits from Student and provides these attributes.
- (4 pts) In the new StudentAthlete class create a constructor
that accepts as parameters an id, name, and sport code. The years of
participation should be 0. The constructor should invoke the
constructor of it's super class.
- (4 pts) Suppose that student athletes have different rules when
qualifying for scholarships. Student athletes can qualify for a
scholarship if their GPA is at least 3.0 as for other students. They
can also qualify if they have participated in a sport for at least one
year and their GPA is at least 2.75. Override the
qualifiesForScholarship method in the new StudentAthlete class
accordingly.
- (6 pts) Draw a UML diagram depicting these two classes. When
depicting attributes you should specify their type. When depicting
methods you should specify their return type, name, and parameters.
Be sure to show the inheritance relationship.
- (6 pts) Write a stand-alone method (not inside either of the classes)
that will accept as a parameter a full array of student objects (some of
which might be of type Student and others which might be of type
StudentAthlete. The method should list all students who qualify
for a scholarship according to their various requirements.
- (2 pts) What is tail recursion?
- (6 pts) Solve this problem posed at codingbat.com:
We have bunnies standing in a line, numbered 1, 2, .... The odd bunnies
(1, 3, ...) have the normal 2 ears. The even bunnies (2, 4, ...) we'll
say have 3 ears, because they each have a raised foot. Write a method that
accepts a value for n and recursively calculates and returns the number
of “ears” in the bunny line 1, 2, ...
(without loops or
multiplication).
- Consider this recursive function:
public static double fun(double [] a, int b, int c) {
double d,e;
if (b>c) return 0;
if (b==c) return a[b];
d= fun(a,b,(b+c)/2);
e= fun(a,(b+c)/2+1,c);
if (d >=e)
return d;
else
return e;
}
- (6 pts) What value is returned by this method if invoked with
fun(cool,0,4) assuming cool is declared and initialized as
follows. REMEMBER: An int divided by an int is always an int.
double [] cool= {12,45,22,15,32};
- (2 pts) Does this method utilize indirect recursion?
- Suppose you have been asked to write a 2-dimensional grid game that consists of GameToken objects as defined below:
[fontsize=\small]
class GameToken {
public boolean isSolid;
public boolean isSlidable;
public boolean isMobile;
public GameToken(boolean isSolid, boolean isSlidable, boolean isMobile) {
this.isSolid= isSolid;
this.isSlidable= isSlidable;
this.isMobile= isMobile;
}
}
class GameBoard {
private GameToken [][] board;
private GameToken player, blank;
public GameBoard(int rows, int cols) {
player= new GameToken(true, false, true);
blank= new GameToken(false, false, false);
// your code here
}
}
In this game each position in the 2d grid contains a reference to some
GameToken object. A wall/barrier is solid and cannot be pushed into
another cell (i.e., it is not slidable), and cannot move on its own (i.e.,
not mobile). An open space (containing air or water) is not solid, not
slidable, and not mobile. A person/character is solid, not slidable, but is
mobile. An inanimate object (desk, chair, barrel, etc.) is solid and
slidable, but not mobile.
- Modify the constructor for the GameBoard class by adding
code following the “your code here” comment to accomplish the
following:
- (4 pts) Reserve memory for the board (based on the parameters
provided) and have all board positions refer to the blank
attribute.
- (2 pts) Put the player in the top, left corner of the board.
- (6 pts) Write a method for the GameBoard class called
countCharacters that will look at an existing (initialized) board and
return the number of animated person/characters on the board. The
attribute “player” may or may not be positioned on the board when
this method is called.