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:
    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
    - 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
    then your function would receive as an input the array
    winners[0] = 2
    winners[1] = 1
    winners[2] = 3
    and your function would need to return an array of strings with the names as above, in that order.
    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][];
          for (int i=0; i < numRaces; i++){
            races[i] = readRace(in);
        } catch (FileNotFoundException ex){
      /** Used by the constructor to read in one race. */
      private String[] readRace(Scanner in){
        int numHorses = in.nextInt();
        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");
        int[] winners = new int[3];
        winners[0] = 0;
        winners[1] = 0;
        winners[2] = 0;
        String[] winnerNames = r.getWinnerNames(winners);
        for (String w : winnerNames){
  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.
    public class Alpha {
      protected int data;
      public Alpha(){ = 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";} }
    public class Beta extends Alpha{
      public Beta(){
        super ();};
      public String toString(){
        return "Beta";}
    public class Gamma extends Beta{
      public Gamma(){
        super (); = 2;
      public String toString(){
        return "Gamma";
      public static void main (String[] args){
        Alpha[] a = new Alpha[3];
        a[0] = new Alpha();
        a[1] = new Beta();
        a[2] = new Gamma();
        for (int i = 0; i < 3; i++){
    //Test the overloaded magic() System.out.println("---"); for (int i = 0; i < 3; i++){ System.out.println(a[i].magic((double) i / 2.0)); }
    } }
    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.