COSC 135B After Exam
2 Practice Problems
Tom Linton, Fall 2000, Central College
-
Consider a new class named Money. This class will represent monetary
objects using two fields, dollars and cents. Since monetary
exchanges need to be exact, these will be int variables. In addition
to these two fields, our class should have each of the following methods:
-
Constructor methods with no inputs (initialize both dollars and cents to
zero), a single int input (the dollars amount), two int inputs (assume
the first is dollars and the second input represents the cents) and a single
Money input (make a new Money object that has the same dollars and cents
values).
-
getDollars() and getCents(), which return the current
values of the two fields;
-
setDollars(int d) and setCents(int c) which set the current
value of the two fields;
-
add(Money m) which returns the sum of the input Money object (m)
and the calling Money object. For example, suppose that bank1 is
a Money object with bank1.dollars = 3 and bank1.cents = 79,
and that bank2 is another Money object with bank2.dollars =
23 and bank2.cents = 53, then bank1.add(bank2) should
return a Money object with dollars = 27 and cents = 32 (note the addition
is somewhat complicated, you can't just add dollars and cents, sometimes
you need to carry 100 cents over to a dollar!).
-
subtract(Money m) which subtracts the input Money object from
the calling money object and returns the resulting Money object. Like addition,
this can be complicated, it is possible to subtract 5 dollars and 79 cents
from 6 dollars and 12 cents.
-
lessThan(Money m) a boolean method that is true only when the
calling object is worth less than the input object. For the bank1 and bank2
Money objects above, bank1.lessThan(bank2) is true, while bank2.lessThan(bank1)
is false.
-
toString() which returns a meaningful String version of the Money
object, something like "12 dollars and 27 cents".
Write the Money class and each of its methods.
-
Write a method that takes an array of ints as input and returns an integer
that equals the number of entries in the array that are odd and positive.
For example, if the array is {3, -3, 4, -12, 18, 1, 7, 789, -453}, your
method should return 4, since 3, 1, 7 and 789 are all odd and positive.
-
Write a method that takes a positive integer as input, say n, and returns
an array that contains the first n powers of 2. For example, on input 5,
your method should return the array {2, 4, 8, 16, 32}.
-
One method to encrypt (make into a secret code) a message is to write the
letters of the message that occur in odd indexes on one line, and the even
indexed letters on the next line, and then concatenate the two lines. For
example, if the message is "here is my secret message", we get:
odd characters: hr sm ertmsae
even characters: eei ysce rma
encrypted message: hr sm ertmsaeeei ysce rma
Write a method to code an input message, and another method to decode
an encrypted message.
-
How do you write each of the following in JAVA?
-
Swap the elements (doubles) in positions 0 and 5 of the array named a[].
-
Test if the last element (int) of an array named a[] is even, or the first
element is odd.
-
Calculate the number of characters (no restrictions on what denotes a character)
in the String called str1.
-
Test whether the int variable index1 is a legal index for the
array a[] (that is, whether or not the statement a[index1] will
cause an indexOutOfBounds error).
-
Test whether the String "easy" appears in the String variable named myString.
-
Test whether string1 and string2 are equal (i.e. contain the same "word").
-
Extract the sub string of the String variable named sentence,
which starts at index 3 and ends at index 7. You want to include the character
at index 7.
-
Check if the Checkbox named dealCards is selected?
-
If the array a[] has the values (the bottom row just lists the indices
for your convenience)
| 25 |
46 |
37 |
9 |
46 |
29 |
-6 |
16 |
| 0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
What are the contents of the array after the following code is executed?
int left=0, right=7, p=0, temp;
while (left <= right) {
while( a[left] <= a[p] )
left++;
while ( a[right] >= a[p] )
right--;
if (left <= right) {
temp = a[left];
a[left] = a[right];
a[right] = temp;
left++;
right--;
}
}
-
Assume that a[][] is a (rectangular) 2-dimensional array of ints.
-
How do you figure out the number of rows in a[][]?
-
What JAVA statement will tell you the number of columns of a[][]?
-
Assuming that a[][] has 7 rows and 5 columns, write JAVA code to print
out rows 0, 2, 4 and 6 of a[][] in the TextArea named output.