- What is abstraction? How about modularization?
- Given the code fragment below, write a possible header line for
the goofy
method. The declaration lines for each "variable" are given, but the
values
assigned to the variables are omitted. The code is NOT supposed to
"make sense" or be logical.
ArrayList listy;
int[] scores;
boolean isFull;
Point p;
p = goofy(scores[3], (Person) listy.get(scores.length -1),
isFull);
- Give the result of each of the following Java code fragments.
- 10 / 4
- 10.0 / 4
- 10 / 4.0
- 10.0 / 4.0
- 10 % 4
- Just give the final value of i here:
int i = 7;
i++;
i += 5;
i *= 4;
- Assume an array s has been created with the code below,
and
that
an int variable named suitCode has some random
integer
value already in it (the value is unknown and may be very large, or
even
negative).
String[] s = new String[5];
s[0] = "clubs";
s[1] = "diamonds";
s[2] = "hearts";
s[3] = "spades";
- Just after the code above executes, what is the value of s.length?
Explain.
- Write code that tests to see if the value of suitCode
is
between
0 and 3 (inclusive). If it is, print a message to the standard terminal
window similar to "The suit of your card is hearts", where the
bold
String is the entry at index suitCode in the array s. If the
value
of suitCode is below 0, or above 3, your code should print a
message
(to the standard terminal window) that informs the user that their card
is the joker.
- Assume now that instead of s (from part 4) being an array, s is
an
ArrayList
with the same Strings added at indicies 0 to 3 (i.e. "clubs" is at
index
0 in s, "diamonds" is at index 1, etc.).
- Rewrite your code from part 4b above, but this time, your if
statement
test may NOT use the literal value 3 (or 4), but should use a method of
the ArrayList class to determine how many entries there are in s.
Recall
that "getting things" from an ArrayList normally requires a "cast".
- Two novice programmers from different towns wrote Java code to
determine
the fine (in dollars) associated with a speeding ticket in a
residential
portion of their town (the two towns may have different fines for
speeding
in their respective residential zones). The main part of their code is
shown below (assume that the variable speed is an int that holds the
speed
of the recipient of the ticket.
- What fine is associated with a speed of 52 in town 1? Explain.
- Does the code for town 1 seem logical? Explain.
- What values get stored in the array fines[ ] for town 2?
- What fine is associated with a speed of 48 in town 2? Explain.
- What values of speed correspond to a fine of $40 in town 2?
- What speeds have a fine of $0 in town 2?
Town 1
if (speed >= 40) {
fine = 10;
}
else {
if (speed >= 45) {
fine =
25;
}
else {
if
(speed >= 55)
{
fine = 50;
}
else {
fine = 75;
}
}
}
|
Town 2
int[] fines = new int[4];
fines[0] = 0;
int value = 20;
for (int i = 1; i < fines.length; i++) {
fines[i] = value;
value *= 2;
}
int fineCode = speed - 35;
if (fineCode <= 0) {
fineCode = 0;
}
else {
fineCode = fineCode / 5;
}
if (fineCode >= fines.length) {
fineCode = fines.length -1;
}
fine = fines[fineCode]; |
- Suppose you're working in a modified version of our Cards
project,
writing
Java source code related to the game of war; where players turn over
their
cards one at a time; the "higher card" wins and adds both cards to the
bottom of their hand; the goal is to win all of your opponents cards;
and
we'll ignore ties by assuming the suits are ranked, so perhaps, clubs
<
spades < diamonds < hearts, but we don't know the ranking order
of
the suits. Assume each Card object has the following methods (and only
these methods), where the header line and a description of what each
method
does is provided below. Also assume that you are working in a file
other
than Card.java, but in the same project that Card.java and Hand.java
belong
to.
public String toString()
Returns a String version of the Card, as in "5 of clubs" or "ace of
diamonds" for example.
public int faceRank()
Returns an integer corresponding to the face value (two, three, four,
... , king, ace) rank of the Card. The larger this integer, the higher
the face value of the card.
public int suitRank()
Returns an integer corresponding to the suit rank of the Card. The
larger this integer, the higher the rank of the card, so if 2 Cards
have
the same face value (and hence the same face value rank), their suit
ranks
can be used to decide which card "wins" in the game of war.
- Write a (public) method (both the header and the body) named war,
that has two parameters, card1 and card2, both of which are Card
objects,
and returns a String. The String should indicate which card wins in
war.
For example, if the method is called with card1 representing the 5 of
clubs,
and card2 representing the ace of diamonds, the String returned should
be something like "the ace of diamonds beats the 5 of clubs". Decide
which
Card wins by comparing their face value ranks (the higher one wins),
and
if those ranks are the same, use the suit ranks to decide the winning
Card
(again, the Card with the higher suit rank wins, if the face value
ranks
are equal).
- In regards to the war method, write a single boolean test
condition
(you
may need to use AND (&&) as well as OR (||)
for
this part, that will be true exactly when card1 beats card2 in our
modified
version of war.
- Assume now that our game of war has two Hand objects named player1
and player2. As well, each Hand object has the methods
described
below.
public Card next()
Returns the next (top) Card in the Hand, and removes that Card from
the Hand.
public boolean hasNext()
Returns true if the Hand has another Card, and false if the Hand is
empty.
public void addToEnd(Card c)
Adds the given Card c to the bottom (end) of the Hand.
For this part, you may assume that every Card object now has a beats
method that returns a boolean value indicating whether or not it (the
Card
whose beats method is called) beats the input Card parameter in our
modified
game of war. For example, card1.beats( card2 ) is true if
card1
beats card2 in war, and false if card1 loses to card2 (i.e. card2 beats
card1). Similarly, card2.beats(card1) is true if card2 wins
against
card1, and false if card2 loses against card2.
Write the java code (not a method just a fragment of code) that gets
the top card from each player (if they both have cards left), compares
them to see who wins, and adds both cards to the bottom of the winning
player's Hand. Finally check to see if the losing player (the one with
lower Card for this round of war) is out of cards or not. If they are,
print a message to the standard terminal window indicating which player
(player 1 or player 2) won the game. BONUS: Assuming you are
now
writing code in the Card.java file (for the bonus part, not the part
above),
write the code for the beats method. You will likely run into a
situation where you want to have a name for the Card whose beats method
is being called. You can refer to this Card object as "this" (without
the
quotes).
- Given the following method definitions, by "tracing through" the
code,
decide what is returned by each of the method calls below.
public int method1(int a, int b} {
int answer = b;
int save;
while (b > 0) {
save = a;
a = b;
b = save % b;
answer = a;
}
return answer;
} |
public int method2(int a, int b) {
int answer = 1;
for (int i = 0; i < b; i++) {
answer *= a;
}
return answer;
}
|
- method1(20, 8)
- method2(3, 2)
- method1(12, 6)
- method2(2,5)
- Write a for loop that calculates 2 + 4 + 6 + 8 + ... + 100.
- Assume that the constructor with no parameters for the Card class
(i.e.
the Card() method) creates a random Card object. Define an
ArrayList
named deck, use a while loop to "add" 30 random cards to the deck; and
then use an iterator to search in deck for the first Card whose
suit
is "spades" (if c is one of the Cards in deck, use c.getSuit().equals("spades")
for your "test"). Finally, print (to the standard terminal window) a
message
that indicates what card was found (use the toString() method
for this), or if the ArrayList had no spades in it.