1. (12) What is the
value or ranges produced from the following?
a. Random rM = new Random(); ______________
System.out.println( rM.nextInt(10)
);
b. Random rM = new Random(); ______________
System.out.println( rM.nextInt(11)
- 5 );
c.
Random rM = new Random(); ______________
System.out.println( car [ rM.nextInt(4)
] );
d. Random rM = new Random(); ______________
System.out.println( rM.nextInt(10)
+ rM.nextInt(5)
);
e. String name =
“Marty”;
name.startsWith(“mar”); ______________
f.
String name =
“Marty”;
name.toLowerCase().startsWith(“mar”); ______________
2. (30) Use definitions on last page. What is the
output of the following?
a. for ( int
i=0; i < direction.length; i++ ) ______________
System.out.print ( direction[
i ] );
b. for ( int
i=0; i < car.length; i++ ) ______________
if ( car[ i ] >= 0 )
System.out.print
( car[ i ] );
c.
for ( int
i=notes.size()-1; i>0; i-- ) ______________
System.out.print (
notes.get( i ) );
d. TicketMachine tm = new TicketMachine(50);
for (int i=1; i<=3; i++)
tm.insertMoney(
10 ); ______________
tm.printTicket();
e. TicketMachine tm = busLine.get(“yellow”);
tm.insertMoney(20);
tm.printTicket(); ______________
f.
int sum = 0;
for ( int i=0; i < car.length; i++ ) ______________
if ( car[ i
] >= 0 )
sum = sum + car[ i
];
System.out.print ( sum / car.length
);
g. for (int
i=0; i<3; i++) ______________
for (int
j=i; j>0; j--)
System.out.print(
i + “ “ + j );
h. int i=car.length-1;
while ( i >= 0 && car[ i
] > 0) ______________
{
System.out.print(
car[ i ] );
i--;
}
i.
for(int i=0; i<busTerminal.length;
i++) ______________
{
busTerminal[i].insertMoney(70);
System.out.print(busTerminal[i].refundBalance() );
}
j.
Iterator in = notes.iterator(); ______________
while( in.hasNext() )
System.out.print(
in.next() );
3. (5) Draw the object diagram for
the following:
TicketMachine t0, t1, t2;
HashMap <String, TicketMachine> machines = new HashMap<String,
TicketMachine>
();
t0=new TicketMachine(50);
t1=new TicketMachine(25);
t2=new TicketMachine(40);
machines.put("50 Cents", t0);
machines.put("25 Cents", t1);
machines.put("40 Cents", t2);
4. (12) Give the definition for the
following array, ArrayList and HashMaps.
a) Define an array named rhymes of
type char initialized to contain the chars ‘b’, ‘c’, ‘d’, ‘e’, ‘p’, ‘z’.
b) Define an ArrayList
collection named subway that
references TicketMachine objects with price 40, 20,
and 50 cents.
c)
Define a HashMap
collection named opposites that maps
“day” to “night”, “large” to “small”, and “Republican” to “Democrat”.
5.
(5) Define a TicketMachine emptyMachine method
that returns the value of total field
and sets total to zero.
6.
(21) For the class Subway
below:
public class Subway
{
a)
ArrayList
<TicketMachine>
tm = new ArrayList <TicketMachine>
();
b)
public Subway() {
tm.add(
new TicketMachine(40 ) );
tm.add( new TicketMachine(20
) );
tm.add( new TicketMachine(50 ) );
}
c)
public Subway(int
n) {
for (int
i=0; i<n; i++)
tm.add(
new TicketMachine(50 ) );
}
d)
public int emptyAllMachines() {
int
total = 0;
for (int
i=0; i<tm.size(); i++)
total = total + ( tm.get(i)).emptyMachine();
return total;
}
e)
public void insertRandomMoney()
{
Random im
= new Random();
for (int
i=0; i<tm.size(); i++)
tm.get(i).insertMoney(im.nextInt(26)+5);
}
}
public class TicketMachine
{
private int price;
private int
balance;
private int
total;
public TicketMachine(int ticketCost)
{
price = ticketCost;
balance = 0;
total = 0;
}
public void insertMoney(int amount)
{
if(amount > 0)
balance += amount;
else
System.out.println("Use
a positive amount: " + amount);
}
public void printTicket()
{
if(balance >= price) {
System.out.println("#
The BlueJ Line");
System.out.println("#
Ticket");
System.out.println("#
" + price + " cents.");
total += price;
balance -= price;
}
else {
System.out.println("You
must insert at least: " +
(price -
balance) + " more cents.");
}
}
public int refundBalance()
{
int amountToRefund;
amountToRefund
= balance;
balance = 0;
return amountToRefund;
}
}
a)
int car [] = { 12, 14, -4, -3, 20, 8 };
b) TicketMachine [] busTerminal
= { new TicketMachine(40),
new TicketMachine(30),
new TicketMachine(50)
};
c)
char [] direction = new
char[4];
direction[0]=’N’;
direction[1]=’E’;
direction[2]=’S’;
direction[3]=’W’;
d) HashMap <String,
TicketMachine>
busLine
= new HashMap <String,
TicketMachine>
();
busLine.put(“blue”, new TicketMachine(40));
busLine.put(“yellow”, new TicketMachine(30));
busLine.put(“red”, new TicketMachine(50));
e) ArrayList <String> notes = new ArrayList <String> ();
notes.add(“race”);
notes.add(“shop”);
notes.add(“study”);