Q1: 20 points Q2: 10 points Q3: 15 points Q4: 15 points Q5: 10 points Q6: 10 points Q7: 20 points Total: 100 points
import java.util.*; // we will use Stack
public class Dog {
private String name;
private int age;
public Dog (String newName, int newAge) {
name = newName;
age = newAge;}
public String getName {return name;}
public void setName (String newName) {name = newName;}
public int getAge ( ) {return age;}
public void setAge (int newAge) {age = newAge;}}
In Questions 3 and 4, the following code block B is to be run before some of the given code blocks, in a static method of the class Dog:
Stack s = new Stack( );
Dog [ ] pack = new Dog[3];
Dog ace = new Dog("Ace", 6);
Dog biscuit = new Dog("Biscuit", 1);
Dog cardie = new Dog("Cardie", 3);
Terrier
and (Terrier)
char
and Character
&&
and ||
String
, in addition to its existing
attributes.
for (int i = 0; i < pack.length; i++)
pack[i] = new Dog("Ace", 6);
int sum = 0;
for (int j = 0; j < pack.length; j++)
if (pack[j] == ace) sum += pack[j].getAge( );
System.out.println (sum);
s.push (ace);
s.push (biscuit);
s.push (cardie);
int sum = 0;
for (int i = 0; i < s.size( ); i++)
sum += s.peek( ).getAge( );
System.out.println (sum);
for (int i = 0; i < 3; i++) {
s.push (ace);
s.push (cardie);}
while (s.size( ) > 3)
s.pop ( );
System.out.println (s.peek( ).getName( ));
pack[0].setName("Ace");
pack[1].setName("Cardie");
pack[2].setName("Biscuit");
public String toString (String name, int age) {
String w = name + ", age: " + age;
return w;}
Dog [ ] [ ] superpack = new Dog [5] [3];
for (int j = 0; j < 5; j++)
for (int i = 0; i < 3; i++)
superpack [i] [j] = ace;
public void replace (Dog [ ] kennel) {
int n = kennel.length;
for (int i = 0; i < n; i++) {
kennel[i] = new Dog ("Cardie", 3);
for (int j = 0; j < i; j++)
kennel[j].setAge(4);}}
// while (the ship has more containers) {
// unload the next container from the ship into temp storage
// shift containers from left to right or right to left
// as needed until the label of the new container is
// between the labels of the top containers of the
// left and right stacks
// push the new container onto the left stack
// shift all containers to the right stack
// pop and send out each container in turn from the right stack
ShowDog
extending the class Dog
from
above. Along with its Dog
atttributes, a
ShowDog
object will have two new attributes, its
breed (stored as a String
) and its entry number
(stored as an int
). Write a new four-parameter
constructor for the ShowDog
class, allowing the
user to set all four attributes. (You need not write get and
set methods for the new attributes.)
Square
and Chessboard
as follows. A
Chessboard object has an 8 by 8 two-dimensional array of Square
objects. Each Square object has two attributes -- its contents,
which consist of a ChessPiece
object (where we
assume that the ChessPiece
class is already
defined), and an number called grains
, stored as an
int
. Write get and set methods for each attribute
of Square. Write methods public Square getSquare(int i,
int j)
and public void setSquare(int i, int j,
Square s)
for the Chessboard class. You are not asked to
write constructors for either class.
The grains
attribute refers to the legend in
which the inventor of chess asks for the following reward -- one
grain of wheat for the first square of the board, two for the
second, four for the third, and so on for the whole board.
Write a method reward
in the Chessboard class that
will set the grains
attribute of each square of the
board as follows. The square (0, 0) gets one grain, and then
each
square in turn gets twice the number of the preceding squre, in
the order (0, 0), (1, 0), ..., (7, 0), (0, 1), (1, 1), ..., (7,
7). (Your method should assume that the board has been created
and populated with valid Square objects.)
What is the value of the grains
attribute of square
(7, 7) after your method has been run?
Last modified 4 October 2011