Show solutions

# 145 Test 3

1. (30%) Write a program that can read into a two-dimensional array a list of the horses that will be racing in each race for a particular day. The first number in the file is the number of races for that day. Each race also starts with a number that is the number of horses in that race, followed by the names of the horses. All the numbers and names appear in separate lines. Here is a sample file:
```3
4
Speedy
Slow Horse
The Terminator
Mommy's Obsession
3
Cannot Win, Ever
Dances with Wolves
Lighting McQueen
5
Just A Horse
Never Give Up
Mister Ed
El Fantastico
Next to Last
```
- See question 3 for solution
2. (20%) Implement the `toString()` function for the previous two-dimensional array (first index is races, second is horses) which returns a `String` where all the horses for each race are in the same line an their names separated by a ":". For example, the races above should look like:
```Speedy : Slow Horse : The Terminator : Mommy's Obsession :
Cannot Win, Ever : Dances with Wolves : Lighting McQueen :
Just A Horse : Never Give Up : Mister Ed : El Fantastico : Next to Last :
```
Note that the newline character is represented by '\n'.- See question 3 for solution
3. (25%) Implement a function `getWinnerNames` which takes as input an array of integers, called `winners`, and returns an array of strings with the names of those horses. Assume that this function has access (as an instance variable) to the two-dimensional array from the first problem. The input to the function is the number of the winning horse for each race. For example, if the winners for the races above were
```The Terminator
Dances with Wolves
El Fantastico```
```winners = 2
winners = 1
winners = 3```
and your function would need to return an array of strings with the names as above, in that order.
```import java.io.*;
import java.util.*;

public class Races {

/** The first index is the race number, the second one is the
horse's position */
private String[][] races;

/** @param filename we read from. Assumed to be in proper format.
Construct by reading from filename */
public Races(String filename){
try {
Scanner in = new Scanner(new File(filename));
int numRaces = in.nextInt();
races = new String[numRaces][];
in.nextLine();
for (int i=0; i < numRaces; i++){
}
} catch (FileNotFoundException ex){
System.out.println(ex.getMessage());
}
}

/** Used by the constructor to read in one race. */
int numHorses = in.nextInt();
in.nextLine();
String [] race = new String[numHorses];
for (int i = 0; i < numHorses; i++){
race[i] = in.nextLine();
}
return race;
}

/** Each race appears in one line */
public String toString(){
String result = "";
for (int i = 0; i < races.length; i++){
for (int j=0; j < races[i].length; j++){
result += races[i][j] + " : ";
}
result += "\n";
}
return result;
}

/** @param winners as array of the winner's position in each race.
Returns an array with the names of the winners for each race. We
assume that the length of winners <= the length of races.
*/
public String[] getWinnerNames(int[] winners){
String[] result = new String[winners.length];
for (int i=0; i < winners.length; i++){
result[i] = races[i][winners[i]];
}
return result;
}

// Unit testing
public static void main(String[] a){
Races r = new Races("races.txt");
System.out.println(r);

int[] winners = new int;
winners = 0;
winners = 0;
winners = 0;
String[] winnerNames = r.getWinnerNames(winners);
for (String w : winnerNames){
System.out.println(w);
}
}
}
```
4. (15%) What does the following program print out? or, does it return an error? If it returns an error identify the line which returns an error.
```//Alpha.java
public class Alpha {
protected int data;

public Alpha(){
this.data = 1;
};

public int getData(){
return data;
}

public int magic(int x){
return data * x;
}

public int magic(double x){
return magic((int) x);
}
public String toString(){
return "Alpha";}
}
```
```//Beta.java
public class Beta extends Alpha{

public Beta(){
super ();};

public String toString(){
return "Beta";}
}
```
```//Gamma.java
public class Gamma extends Beta{
public Gamma(){
super ();
this.data = 2;
};

public String toString(){
return "Gamma";
}

public static void main (String[] args){
Alpha[] a = new Alpha;
a = new Alpha();
a = new Beta();
a = new Gamma();
for (int i = 0; i < 3; i++){
System.out.println(a[i]);
System.out.println(a[i].magic(1));
}

System.out.println((Alpha)a);
System.out.println((Beta)a);
System.out.println((Gamma)a);

System.out.println("---");
for (int i = 0; i < 3; i++){
System.out.println(a[i].magic((double) i / 2.0));
}
}
}
```
```Alpha
1
Beta
1
Gamma
2
Beta
Beta
Exception raised.```
5. (10%) For the program above, overload the `magic` function so that it can take a `double` as its argument an do whatever the current `magic` function does, but using the truncated value of the double as its argument. Show your code. Tip: You can truncate a double into an int by typecasting it to an int. - See solution in previous question.