Lab 8: Arrays, ArrayLists, and Files: Analysing Data
CS 141: Introduction to Programming , Fall 2015


This lab is due Monday, Nov 9, by the end of the lab. (Try to have Part 1 done by Nov 2.)

Overview

The goal of this lab is to give you practice working with arrays, ArrayLists, and files. In the first part of the lab, you will focus on using a 1D arrays of primitives. In the second part, you will work with a 1D array of objects. Finally, in the last part, you will be given code to read in the data from a text file.

Practice with Arrays (this part will not be collected)

Many of the problems in the Basic Array Problems are similar to the methods you must implement below. Try to do the problems below first but if you have trouble, take a look at the solutions for the Basic Array Problems. Many of these basic problems will be covered in class and are also discussed in your text.

Part 1: 1D Arrays

Part 2: Array of Objects

In the above program, you worked with an array of primitive types (i.e. doubles). The downside is that it was not always possible to maintain the connection between the year and the temperature. For example, when you sorted the temperatures, you had no way of knowing what year each temperature corresponded to. This can easily be fixed by creating an array of objects where each object encapsulates the related items (in this case, the year and temperature).

In this part of the lab, you will create a simple class that holds a year and a temperature. You will then create another class which contains an array of the simple class. By adding a compareTo() method, we will be able to use the Arrays class to sort the array. Once sorted, you can print out the sorted list where each object maintains the grouping of the year and temperature. This will enable us to observe how the temperature is related to year and ask questions such as "are most of the hottest years more recent?"

Instructions:

Part 3: Files

In this part of the lab, you are to read in the array of temperature values from a file.

  1. Download the file meanJulyTemp1903to2015.txt and save it into the src folder of your project (along with your .java files).
  2. Download MyFileReader.java and also save it into the src folder of your project. This class reads in an array of doubles. You may need to "refactor" it by fixing the package name. We will go over the code in class.
  3. In the constructor of your TemperatureObjects class, you just need to replace the temperatures array
    double temperatures[] = {
    63.6, 66.8, 68.8, 72.0, 67.6, 70.4, 62.9, 67.2, 69.5, 67.0,
    66.1, 68.3, 66.5, 64.2, 69.0, 66.3, 66.7, 64.7, 65.1, 69.5,
    ...};
    with
    MyFileReader reader = new MyFileReader("src\\your_package_name\\meanJulyTemp1903to2015.txt");
    double temperatures[] = reader.data;
If you are working on a Mac, the path/file syntax will be different. Try instead:
MyFileReader reader = new MyFileReader("src/your_package_name/meanJulyTemp1903to2015.txt");

Compile and run your program. It should run exactly as before. The advantage of having the data in a file is that:

Deliverables

To receive full credit, you need to:

  1. Make sure that your Java code:

  2. Demonstrate: