Difference between revisions of "CSC212 Homework 3 2014"

From dftwiki3
Jump to: navigation, search
(Problem #3)
(Problem #3)
Line 218: Line 218:
 
<br />
 
<br />
 
* Call your program '''Hw3_3.java'''.
 
* Call your program '''Hw3_3.java'''.
* Test it thoroughly.  Your class will be tested by using it to implement lists of ints, lists of doubles, list of strings, and list of booleans, and appending, searching, or locating items at various indexes.  
+
* Test it thoroughly.  Your class will be tested by another test class that will be using it to implement lists of ints, lists of doubles, list of strings, and list of booleans, and appending, searching, or locating items at various indexes.  
* Your program should return '''null''' if the ''at()'' method is asked to retrieve the data at a nonexistent index.
+
 
 
<br />
 
<br />
 
==Submission==
 
==Submission==
 
<br />
 
<br />
* Submit your program to Moodle, Homework 3, Problem 3.
+
* Submit your program to Moodle, Homework 3, Problem 3. <font color="red">(not available yet...Stay tuned!)</font>
 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
 
[[Category:Java]][[Category:CSC212]]
 
[[Category:Java]][[Category:CSC212]]

Revision as of 16:28, 24 September 2014

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


This homework is due on Oct. 2nd, 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


  • Convert the Python-List class shown below to a generic class.


public class PythonList2 {
    private String[] array = null;
    private int maxdim;
    private int end = 0;
    
    PythonList2(int m) {
        maxdim = m;
        end = 0;
        array = new String[maxdim];
    }
    
    PythonList2() {
        this(100);
    }

    private boolean isValid( int i ) {
        return ( i >= 0 && i < end );
    }

    public void setAt( int i, String n ) {
        if ( ! isValid( i ) ) 
            return;
        array[i] = n;
    }

    public int index( String n ) {
        for ( int i=0; i < end; i++ ) {
            if ( at(i) == n )
                return i;
        }
        return -1;
    }

    public int rindex( String n ) {
        for ( int i=end-1; i >= 0; i-- )
            if ( array[i]==n )
                return i;
        
        return -1;
    }

   
    public void append(String  n) {
        if (end <= array.length - 1) {
            array[end++] = n;
            return;
        }
        String[] temp = new String[maxdim * 2];
        for (int i = 0; i < array.length; i++)
            temp[i] = array[i];
        array = temp;
        array[end++] = n;
        maxdim = maxdim * 2;
    }
    
    public int length() {
        return end;
    }
    
    public String at(int index) {
        
        if (index >= 0 && index < array.length)
            return array[index];
        else
            return null;
    }
    
    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" );

    }
}


  • Call your program Hw3_3.java.
  • Test it thoroughly. Your class will be tested by another test class that will be using it to implement lists of ints, lists of doubles, list of strings, and list of booleans, and appending, searching, or locating items at various indexes.


Submission


  • Submit your program to Moodle, Homework 3, Problem 3. (not available yet...Stay tuned!)