 # CSCI A201/A597 and I210 Practical Solutions

Second semester 2000-2001

Solutions for the problems given on the practical exam.
There were six problems, for the six time zones. Here they are, in order:

1. Write a program that asks the user for an integer (`size`,) allocates an array of `size` prices (prices are stored as `double`) and then asks the user to enter `size` prices, one by one, each price on a line.

Once the user finishes entering the prices, your program should compute the average of all the prices entered and should print it.

After that the program prints all the elements in the array, one element per line. For each element

• print two asterisks next to the element if it is less than the computed average, and
• print the difference between the element and the average (in parentheses) next to the element if it represents a price that is bigger than the computed average.

Then print the array one more time, but this time in reversed order.

Example:

```prompt> java Practical
Thank you, please enter the 4 prices below.
Price 1: 1
Price 2: 1
Price 3: 2
Price 4: 2
Thank you!
The average is: 1.5
The array you entered is:
0: 1.0 (**)
1: 1.0 (**)
2: 2.0 (0.5)
3: 2.0 (0.5)
And backwards:
3: 2.0 (0.5)
2: 2.0 (0.5)
1: 1.0 (**)
0: 1.0 (**)
Thanks for using this program!```

Here's a solution:

```class Practical {
public static void main(String[] args) {
double[] prices = new double[size];
System.out.print("Thank you, please enter the ");
System.out.println(size + " prices below.");
double sum = 0;
for (int i = 0; i < prices.length; i++) {
System.out.print("Price " + (i + 1) + ": ");
sum += prices[i];
}
System.out.println("Thank you!");
double average = sum / prices.length;
System.out.println("The average is: " + average);
System.out.println("The array you entered is:");
for (int i = 0; i < prices.length; i++) {
if (prices[i] <= average) {
System.out.println(i + ": " + prices[i] + " (**)");
} else {
System.out.println(i + ": " + prices[i] + " (" +
(prices[i] - average) + ") ");
}
}
System.out.println("And backwards:");
for (int i = prices.length - 1; i >= 0; i--) {
if (prices[i] <= average) {
System.out.println(i + ": " + prices[i] + " (**)");
} else {
System.out.println(i + ": " + prices[i] + " (" +
(prices[i] - average) + ") ");
}
}
System.out.println("Thanks for using this program!");
}
}```

2. Write a program that simulates a vending machine that sells items which are worth 65 cents. Your program (called `Vending`) starts by greeting the user, then accepting coins. The machine accepts one coin at a time and understands the following coins:

• `nickel`
• `dime` and
• `quarter`
After each coin the program prints back the amount that is still needed to purchase the item. When the amount exceeds 65 cents the program prints the change (if any) and thanks the user for using the program.

Example run:

```prompt> java Vending
coin> nickel
60 cents remaining
coin> quarter
35 cents remaining
coin> quarter
10 cents remaining
coin> quarter
Thank you. Your change is: 15 cents.
Thanks for using this program. ```

Here's a possible solution:

```class Vending {
public static void main(String[] args) {
int amount = 0;
final int PRICE = 65;
while (amount < PRICE) {
String coin;
System.out.print("coin> ");
if (coin.equals("nickel")) {
amount += 5;
} else if (coin.equals("dime")) {
amount += 10;
} else if (coin.equals("quarter")) {
amount += 25;
}
if (amount < 65) {
System.out.println((PRICE - amount) + " cents remaining.");
} else {
System.out.println("Thank you. Your change is: " +
(amount - PRICE) + " cents.");
}
}
System.out.println("Thanks for using this program.");
}
} ```

3. Write a program that reads a two-dimensional square array of positive integers from the user, then calculates the average of the elements in the array and prints it back with all the even numbers replaced by an uppercase E and all the odd ones replaced by an uppercase O. The user inputs the size first, then enters `size` lines of `size` numbers each. You don't need to do any error checking.

Example:

```prompt> java Practical
Hello and please enter the size: 4
Thanks, please start entering the lines of the array (4 numbers per line).
Enter line 0> 1 2 3 4
Enter line 1> 2 3 4 1
Enter line 2> 3 4 1 2
Enter line 3> 4 3 2 1
Great. The average is: 2.5
Here's the array re-written:
O E O E
E O E O
O E O E
E O E O
Thanks for using this program!```

Here's a possible solution:

```import java.util.*;

class Practical {
public static void main(String[] args) {
System.out.print("Hello and please enter the size: ");
System.out.println("Thanks, please start entering the lines " +
"of the array (" + size + " numbers per line.)");

int[][] a = new int[size][size];
String line;
double sum = 0;
for (int i = 0; i < size; i++) {
System.out.print("Enter line " + i + "> ");
StringTokenizer st = new StringTokenizer(line);
for (int j = 0; j < size; j++) { // program-friendy user
a[i][j] = Integer.parseInt(st.nextToken());
sum += a[i][j];
}
}
System.out.println("Great. The average is: " + sum / (size * size));
System.out.println("Here's the array re-written:");
for (int i = 0; i < size; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] % 2 == 0) {
System.out.print("E ");
} else {
System.out.print("O ");
}
}
System.out.println();
}
System.out.println("Thanks for using this program!");
}
} ```

4. Write a program that reads two arrays of integers that the user is specifying on two lines. The user need not specify the size of the arrays, just the arrays, one per line. Then if the two arrays are of the same length the program calculates and prints an array that results from adding the elements of the two arrays one by one. Otherwise the program reports that the addition cannot be done because the arrays are not of the same length.

Example:

```prompt> java Practical
Welcome to the program!
Please enter the first array: 1 2 3 4
Thank you.
Please enter the second array: 4 3 2 1
Thank you.
The arrays have the same length.
The resulting array is: 5 5 5 5
Thanks for using this program. ```

Here's a possible solution:

```import java.util.*;

class Practical {
public static void main(String[] args) {
System.out.println("Welcome to the program!");
System.out.print("Please enter the first array: ");
StringTokenizer a = new StringTokenizer(line); // for counting
StringTokenizer b = new StringTokenizer(line); // for actual elements
int size = 0;
while (a.hasMoreTokens()) {
a.nextToken();
size += 1;
}
int[] one = new int[size];
for (int i = 0; i < size; i++) {
one[i] = Integer.parseInt(b.nextToken());
}
System.out.println("Thank you.");
// Notice that what follows is identical to the fragment above
System.out.print("Please enter the second array: ");
a = new StringTokenizer(line); // for counting
b = new StringTokenizer(line); // for actual elements
size = 0;
while (a.hasMoreTokens()) {
a.nextToken();
size += 1;
}
int[] two = new int[size];
for (int i = 0; i < size; i++) {
two[i] = Integer.parseInt(b.nextToken());
}
// Now the new part
if (one.length != two.length) {
System.out.println("Sorry, arrays not of same length.");
} else {
System.out.println("The arrays have the same length.");
int[] result = new int[one.length];
for (int i = 0; i < result.length; i++)
result[i] = one[i] + two[i];
System.out.print("The resulting array is: ");
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
System.out.println();
}
System.out.println("Thanks for using this program.");
}
} ```

5. Write a program that simulates a vendor that sells books of stamps which are worth 3 dollars and 40 cents each. (The vendor only sells books, each book is \$3.40). Your program (called `Vendor`) starts by greeting the user, then accepting money. Your vendor accepts the following monies only:

• `cent`
• `nickel`
• `dime`
• `quarter` and
• `dollar`
Your vendor accepts money one line at a time, and should allow more than one coin to be entered on a line. After that, the program calculates and prints the amount that is still needed to purchase the item. When the amount finally exceeds 340 cents the program prints the change (if any) and thanks the user for using the program.

Example run:

```prompt> java Vendor
Welcome. We sell stamps (\$3.40) Please enter money:
enter> nickel nickel dollar dollar quarter
Thanks. Your credit is \$2.35 I need \$1.05 more.
enter> cent cent
Thanks. Your credit is \$2.37 I need \$1.03 more.
enter> dollar quarter
Thanks. Your credit is \$3.62 The stamps are yours.
Thanks for using this program. ```

Here's a possible solution:

```import java.util.*;

class Vendor {
public static void main(String[] args) {
System.out.print("Welcome. We sell stamps (\$3.40) ");
String line;
int amount = 0;
while (amount < 340) {
System.out.print("enter> ");
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
String coin = st.nextToken();
if (coin.equals("cent"))         { amount += 1;   }
else if (coin.equals("nickel"))  { amount += 5;   }
else if (coin.equals("dime"))    { amount += 10;  }
else if (coin.equals("quarter")) { amount += 25;  }
else if (coin.equals("dollar"))  { amount += 100; }
}
System.out.print
("Thanks. Your credit is: \$" + amount / 100.0 );
if (amount >= 340) {
System.out.println(" The stamps are yours.");
if (amount > 340) {
System.out.println
("Your change is: \$" + (amount - 340) / 100.0);
}
} else {
System.out.println
(" I need \$" + (340 - amount) / 100.0 + " more.");
}
}
System.out.println("Thanks for using this program.");
}
}```

6. Write a program that creates a two-dimensional array and fills it with random integers which have values between -50 and 50. The number of lines and columns should be specified by the user on the command line. Your program should show the array to the user and then count and report the number of negative values in the array (strictly less than zero).

Example:

```prompt> java Practical 2 3
Welcome to the array generation program!
We will generate a 2 by 3 array of random integers now. Please wait.
... Done.
Here's the array:
23 -8 12
-20 1 -49
This array contains 3 negative numbers.
Thanks for using this program!```

Here's a possible solution:

```import java.util.*;

class Practical {
public static void main(String[] args) {
System.out.println("Welcome to the array generation program!");
int lines = Integer.parseInt(args),
cols = Integer.parseInt(args);
System.out.println("We will generate a " + lines + " by " + cols +
" array of random integers now. Please wait.");
Random generator = new Random();
int[][] data = new int[lines][cols];
for (int i = 0; i < lines; i++)
for (int j = 0; j < cols; j++)
data[i][j] = generator.nextInt(100) - 50;
System.out.println("... Done.");
System.out.println("Here's the array: ");
int count = 0;
for (int i = 0; i < lines; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(data[i][j] + " ");
if (data[i][j] < 0) { count += 1; }
}
System.out.println();
}
System.out.println("This array contains " + count +
" negative numbers.");
System.out.println("Thanks for using this program.");
}
}```

Last updated: Mar 31, 2001 by Adrian German for A201