CSC212 Homework 3 2014

From dftwiki
Jump to: navigation, search

--D. Thiebaut (talk) 16:21, 24 September 2014 (EDT)


This homework is due on Oct. 2nd Oct. 5, 2014, at 11:55 p.m.


Problem #1

  • Create a text file in your directory with the following lines. Call it temperatures0.dat:


   1977   9   18.3     8.1       0    48.6   117.9
   1977  10   15.4     9.0       0    76.0    80.4
   1977  11   10.3     4.7       4   147.9    76.4
   1977  12    9.6     4.6       2    88.0    25.6
   1978   1    7.5     2.0       6   134.1    64.7
   1978   2    6.2     0.9      13   134.1    56.9
   1978   3   10.6     3.9       3   121.3   122.4
   1978   4   10.9     3.7       4    63.6   118.6
   1978   5   17.3     7.5       0    30.6   188.1
   1978   6   18.5     9.5       0    29.6   177.3
   1978   7   18.7    11.8       0   110.1   115.8
   1978   8   19.6    11.4       0    40.5   164.9
   1978   9   18.4    10.2       0    34.1   182.2
   1978  10   16.6     9.0       0    12.0    81.1


These numbers are weather numbers for Cardiff Bute Park, in the U.K. The 7 numbers on each line represent the following quantities, shown here with their standard abbreviation:

  • the year of the recording (yyyy)
  • the month of the recording (mm)
  • the max daily temperature in degrees Celcius (tmax)
  • the min daily temperature in degrees Celcius (tmin)
  • the number of days of frost (af)
  • the total rainfall (rain)
  • the sunshine duration (sun)


Your Assignment


  • Write a program called Hw3_1.java that will
  1. read the temperature file,
  2. record the year when the rain was maximum,
  3. record the year when the sun was maximum,
  4. output this information on two lines of text:
  javac Hw3_1.java  
  java Hw3_1 temperatures0.dat 
   rain 1977 147.90
   sun  1978 188.10


Submission


  • Submit your program to Moodle, Homework 3, Problem 1.


Problem #2


  • Create a new version of your program and call it Hw3_2.java.
  • Create a new text file called temperatures.dat, with the contents found below:


Cardiff Bute Park
Location 3176E 1773N, 9 metres amsl
Estimated data is marked with a * after the value.

   yyyy  mm   tmax    tmin      af    rain     sun
              degC    degC    days      mm   hours
   1977   9   18.3     8.1       0    48.6   117.9
   1977  10   15.4     9.0       0    76.0    80.4
   1977  11   10.3     4.7       4   147.9    76.4
   1977  12    9.6     4.6       2    88.0    25.6
   1978   1    7.5     2.0       6   134.1    64.7
   1978   2    6.2     0.9      13   134.1    56.9
...
   2009   7   20.2    13.1       0   194.1     ---
   2009   8   20.7    ---      0    76.0     120.5
   2009   9   19.1    10.6       0    34.7     ---
   2009  10   16.3     8.6       0    73.6     ---
   2009  11   12.6     6.5       0   220.4     ---   
   2009  12    7.2     0.0      15   114.9     ---   
   2010   1    6.0    -1.2      16    73.5     ---   
   2010   2    7.3     0.8       7    72.1     ---  


  • You will notice that the new file will contain lines other than data lines (like the header), and that some data are missing, and replaced by "---". Your program has to be able to skip these non-data without problems. In particular, without generating exceptions to the operating system!
  • Using the program you wrote for Lab #5, build Hw3_2.java so that it will look for the max rain and sun recordings, and output 2 lines with the years and the max numbers found.


Requirements


  • The output format should be the same as for Problem 1.
  • You must declare and use your own exception class.
  • You must use a method to open the file,
  • You must use another method that reads only 1 line at a time from the input file and returns the year, rain and sun data found on that line. You can use an array or a triplet to pass these numbers back to the main program.


Submission


  • Submit your program to Moodle, Problem 2 Section.



Problem #3


  • Below is a the main method of a class that implemented our Python-List. It was our excursion in the world of data structures, looking at a way to implement a "clever" array that would grow as we needed.



    
    public static void main(String[] args) {
        // --- Test List ---
        PythonList2 dwarves = new PythonList2(5);

        dwarves.append("Blick");
        dwarves.append("Flick");
        dwarves.append("Glick");
        dwarves.append("Plick");
        dwarves.append("Quee");
        dwarves.append("Snick");
        dwarves.append("Whick");

        System.out.println("Snow White's friends:");
        for (int i = 0; i < dwarves.length(); i++)
            System.out.println(dwarves.at(i));

        String dwarf;
        dwarf = dwarves.at(-1);
        if (dwarf != null)
            System.out.println(dwarf);
        dwarf = dwarves.at(20);
        if (dwarf != null)
            System.out.println(dwarf);

        int n = dwarves.index( "Quee" );
        if ( n != -1 ) 
            System.out.println( "Quee found at Index: " + n );
        else
            System.out.println( "Quee not found" );

        n = dwarves.rindex( "Quee" );
        if ( n != -1 ) 
            System.out.println( "Quee found at Index: " + n );
        else
            System.out.println( "Quee not found" );

        n = dwarves.index( "Queen" );
        if ( n != -1 ) 
            System.out.println( "Queen found at Index: " + n );
        else
            System.out.println( "Queen not found" );

    }


Your assignment is to create a new class called Hw3_3.java with a main() function as similar to the one shown below as possible, but that uses a Java Vector data structure to implement the list of dwarves. The new main() function should basically do everything the same as the main() shown here:
  1. declare dwarves as a vector
  2. add all the names to the vector
  3. use a loop to print all the names in the vector
  4. attempt to display the names at Indexes -1 and 20.
  5. look for "Quee", starting at the front of the vector
  6. look for "Quee", starting at the end of the vector
  7. look for "Queen", starting at the front of the vector.


  • The output of your new main() method, that now uses Vector, is shown below:


Snow White's friends:
Blick
Flick
Glick
Plick
Quee
Snick
Whick
invalid index
invalid index
Quee found at Index: 4
Quee found at Index: 4
Queen not found


  • Note that the output is slightly different (showing invalid indexes, for example) from that of the original program.


Submission


  • Submit your program to Moodle, Homework 3, Problem 3.