(1) | _____ |
2-dimensional arrays must be rectangular. |
(2) | _____ |
The elements in an array must be homogeneous. |
(3) | _____ |
Arrays use linked memory. |
(4) | _____ |
An ArrayList can not be re-sized.
|
(5) | _____ |
A checked exception is derived either from Error
or RuntimeException .
|
1 Integer[] values; 2 values = new Integer[5]; 3 for (int i=0; i < values.length; i++) 4 { 5 values[i] = new Integer(i*10); 6 }
IntNode
:
public class IntNode { public int value; public IntNode next; }
and the following implementation of a Pew
:
public class Pew { private IntNode last; public Pew() { last = null; } public void pish(int anInt) { IntNode temp; temp = new IntNode(); temp.value = anInt; temp.next = last; last = temp; } }
draw a model of memory that illustrates what happens when the following statements are executed:
Pew p; p = new Pew(); p.pish(22); p.pish(33); p.pish(44);
Person
class:
public class Person { public int age; public Person bestFriend; public String name; public Person(String name, int age, Person bestFriend) { this.name = name; this.age = age; this.bestFriend = bestFriend; } public String toString() { String s; s = name + " (" + age + ") "; if (bestFriend == null) s += "has no friends"; else s += "is friends with " + bestFriend.name; return s; } }
what will be printed by the following Driver
(assuming
everything is compiled and executed properly)? Note: Be careful!
import java.util.*; public class Driver { public static void main(String[] args) { Person alice, bob, carol, dan, temp; ArrayList people; alice = new Person("Alice", 30, null); bob = new Person("Bob", 32, alice); System.out.println(alice.toString()); System.out.println(bob.toString()); carol = new Person("Carol", 40, null); dan = new Person("Dan", 50, carol); carol.bestFriend = dan; System.out.println(carol.toString()); System.out.println(dan.toString()); people = new ArrayList(); people.add(alice); people.add(bob); people.add(carol); people.add(dan); alice.age = 65; bob.bestFriend = carol; carol.name = "Carol Ann"; dan.age = 88; for (int i=0; i<people.size(); i++) { temp = (Person)people.get(i); System.out.println(temp.toString()); } } }
Driver
above so that it explicitly uses
an ArrayList
of Person
objects (and is
type safe).
Person
class above as follows:
setName(String name)
.
setAge(int age)
method that throws
a NobodyIsThatOldException
whenever the age
is greater than 39. This exception must be checked.
(Note: You will need to write a
NobodyIsThatOldException
class.)
Driver
class so that it now works with this
new version of the Person
class.
Printer
class:
import java.util.*; public class Printer { private StringTokenizer tokenizer; public void print(String line) { tokenizer = new StringTokenizer(line, ","); if (tokenizer.hasMoreTokens()) { printTokens(tokenizer); } } private void printTokens(StringTokenizer tokenizer) { String token; token = tokenizer.nextToken(); if (tokenizer.hasMoreTokens()) { printTokens(tokenizer); } System.out.println(token); } }
carefully trace the execution of the following:
public class PrinterDriver { public static void main(String[] args) { Printer test; test = new Printer(); test.print("Aboutabl,Bernstein,Harris"); } }
For example, the following are valid expressions in prefix notation:
Suppose we have a Stack
class that holds
String
objects and a method named evaluate
in the Calculator
class that is passed the three parts
of a prefix expression (as String
objects) and returns
a String
representation of the result. Trace the
execution of the following, carefully modeling memory at each
step:
Stack terms; terms = new Stack(); terms.push("5"); terms.push("3"); terms.push("+"); result = Calculator.evaluate(terms.pop(), terms.pop(), terms.pop()); terms.push(result); terms.push("4"); terms.push("*"); result = Calculator.evaluate(terms.pop(), terms.pop(), terms.pop());
Adder
class is compiled
properly:
public class Adder { public static void main(String[] args) { double result; try { result = add(args); System.out.println(args[0]+"+"+args[1]+": "+result); } catch (ArrayIndexOutOfBoundsException aioobe) { System.out.println("Too few operands."); } } public static double add(String[] terms) throws ArrayIndexOutOfBoundsException { double left, result, right; String operator; left = 0.0; right = 0.0; try { left = Double.parseDouble(terms[0]); } catch (NumberFormatException nfe) { left = 0.0; } try { right = Double.parseDouble(terms[1]); } catch (NumberFormatException nfe) { right = 0.0; } result = right + left; return result; } }
carefully trace the execution of each the following:
total()
method in the following class. You
must use an Iterator
, and you must use it properly.
import java.util.*; public class Revenues { private ArrayList<String> items; public Revenues() { items = new ArrayList<String>(); } /** * Add an item value to the collection of revenues * * @param item A dollar value (including the leading $ and commas) */ public addItem(String item) { items.add(item); } /** * Calculates the total of all (valid) items in this collection * * @return The total revenue */ public double total() { } }
int[][]
and returns true
if and only if all of the rows
and all of the columns sum to the same value.
int[][]
of size nxn
(i.e., with n rows and n columns)
and returns true
if and only if it
contains all of the int
values from 1
to n
2.
QueueOfDoubles
that, as its name
almost implies, contains a queue of double
values (not
Double
objects). It must contain a "copy constructor".
It must use a linked implementation.
SavingsAccount
class that
contains a queue of deposits and withdrawals. It must use a
QueueOfDoubles
object. Deposits should be represented in the
queue as positive entries and withdrawals should be represented in the
queue as negative entries. It must have methods for the following:
It must be possible to call each method more than once. (Hint: Think about how this can be facilitated using the "copy constructor".)
PersonalAccounts
class (to be used by a bank)
that contains a HashMap
of
SavingsAccount
objects in which the keys are the names
of the account holders. It must have methods for the following:
SavingsAccount
.
SavingsAccount
.
SavingsAccount
objects.
Copyright 2011