Skip to Main Content

Introduction to Computer Science

Computer Science Department

Functions on Arrays and Reading Files – 80 course points

The purpose of this assignment is to teach students how to use functions to manipulate arrays.

General Guidelines

Write 2 programs and submit on Autolab.

  • We provide a zip file (find it under Assignment5 on Autolab) containing AirParticles.java and CharacterCounter.java.
  • For each problem UPDATE and SUBMIT the corresponding file only writing where it indicates.

Observe the following rules:

  1. DO NOT add any import statements
  2. DO NOT add the project statement
  3. DO NOT change the class name
  4. DO NOT change the headers of ANY of the given functions
  5. DO NOT add any new class fields
  6. DO NOT use static variables
  7. DO NOT use System.exit()
  8. ONLY print the result as specified by each problem. Observe the examples’ output, display only what the problem is asking for.
  9. DO NOT print other messages, follow the examples for each problem.
  10.  

1. Air Particles (40 points)

The purpose of this assignment is to teach students how to analyze data from input using arrays.

Overview

Fine air particles can come from power plants, motor vehicles, airplanes, residential wood burning, forest fires, agricultural burning, volcanic eruptions, dust storms, etc. Particles smaller than 2.5 micrometers (often called PM2.5) are able to bypass the nose and throat and penetrate deep into the lungs and some may even enter the circulatory system.
Due to the many adverse effects fine particles can inflict on a large number of people, PM2.5 is one of the major pollutants closely monitored by health authorities around the world. You will most likely come across a dedicated column for PM2.5 alongside the Air Quality Index (AQI), Pollutants Standards Index (PSI) or the air quality standards adopted by your country.
The data provided to you in this assignment was obtained from the Geoplatform screening tool. The assignment only uses the PM2.5 levels from the dataset. (The government took the site down but the data is in the provided zip folder)

Task

  • UPDATE and SUBMIT AirParticles.java in AutoLab.

Programming

Write one function in AirParticles.java.
  • findHighestLevel(double[] communities) finds and returns the largest value in the double array communities. Each array index contains the PM2.5 level for a community in the country.
      • Note 1: the function returns a double value.
      • Note 2: do not update the argument array communities.

In this function you are practicing finding the largest value in the input array and returning a value from a function.

  • main(String args[]) is used to test your findHighestLevel method
    • read in the total number of lines in the file (this is the first line of the file)
    • initialize the array
    • read from StdIn while it isn’t empty
      • populate the array with the double you read in
    • call findHighestLevel on your array
    • print “Highest PM2.5 Level is ##” (## represents an actual number)

Executing and Debugging

  • First navigate to the Assignment5 directory/folder

    • to compile: javac AirParticles.java
    • to execute: java AirParticles < SmallerPM2.5.txt or java AirParticles < PM2.5.txt
      • NOTE: YOU CANNOT USE `StdIn.setFile()` THE < REDIRECTS FILE CONTENTS AUTOMATICALLY.
    • (on Windows) Get-Content SmallerPM2.5.txt | & java AirParticles The main method is implemented for you to test your function using the input files SmallerPM2.5.txt or PM2.5.txt
Author Credit Aastha Gandhi

2. Character Counter (40 points)

This assignment is designed to help students practice working with 1D arrays, reading from a file and writing to a file using StdIn and StdOut libraries.

Students will use an array to keep track of number of times each character appears in a file.

Overview

The ASCII (American Standard Code for Information Interchange) has a decimal value/representation for encoding 128 different letters, punctuation and other characters. In the age where millions of bytes of data are stored and sent across networks with a single click, it’s important to optimize the memory and time of the computers that carry out these tasks.

Huffman Coding is a method of data compression that counts the occurrences of each character and maps them to a certain bit string based on their frequency for optimized space.

You do not need to understand Huffman Coding for this assignment. Here are two resources if you are interested in learning more about it.

In this assignment, you will be implementing your own program that will keep track of the number of occurrences of these 128 characters. This is equivalent to the first part of the Huffman Coding algorithm.

Task

  • UPDATE and SUBMIT CharacterCounter.java in AutoLab.

Programming

  1. You will use a 1D integer array to keep track of the number of times each of the 128 characters appears in the input file.
  2. Then, once the entire file has been read and each character has been accounted for, you will print (only from index 32 to 126 inclusive) to the output file in the following format:
    • [character],[ascii value],[occurrences],[frequency]
      • NO SPACE before or after the comma.
    • character: is the character itself (e.g. A, r, …).
    • ascii value: is the decimal value of the character (e.g. 65, 114, …).
    • occurrences: number of times the character appears in the file.
    • frequency: is a percentage computed by (occurrences) / (total number of characters in the file) * 100
  3. Once the entire file has been read. Print to the output file in the following format:

      • [character],[ascii value],[occurrences],[frequency]
        • NO SPACE before or after the comma.

Notes:
Only modify the main method, no helper methods necessary

    • Keep track of the number of times the character appears for all characters in the file, but only print out char values of 32 to 126 inclusive to the output file.
  •  

The first argument, args[0] will be the input file.

  • Use StdIn.setFile(args[0]) to set the input file.
  • Read characters while there are characters to be read from the file. StdIn.hasNextChar() returns true if there are characters to be read.
  • To read one character use StdIn.readChar()

Use a 1D integer array (size 128) to keep track of the number of times each character appears in the input file.

If you look at the ASCII table you will notice that:

  • The first 32 characters (0 through 31) cannot be printed and ARE NOT of interest to this assignment.
  • Character 32 through 126 (inclusive) ARE the characters of interest to this assignment.
  • Notice that each character has a decimal value. The decimal value (represents the character) is the value stored in the computer.
      • The character ‘A’ has decimal value 65.
      • When you cast a char as an int, the value is the character’s decimal value.
      • The println() below will print 65 when the two lines of code are executed. Try it!
      • char c = 'A';
      • System.out.println( (int) c);

Once a character is read from the file, use the character as the array index (cast the character as an integer).

  • If you read an ‘A’ , index 65 is expected to be incremented.
  • If, for example, you read ‘4’, then index 52 is expected to be incremented.

This way you will be able to keep track of the number of times each character appears in the file.

The second argument, args[1] will be the output file.

  • Use StdOut.setFile(args[1]) to set the output file.
  • Then use StdOut.println() to write to the file.

Executing and Debugging

  • First navigate to the Assignment5 directory/folder

    • to compile: javac CharacterCounter.java
    • to execute: java CharacterCounter filename.in output.out


(only displays partial output)

Author Credit Mary Buist and Anna Lu

Submitting Guidelines

VSCode Extensions

You can install the VSCode extension pack for Java. Take a look at this tutorial.

We suggest:

Importing VSCode Project

  1. Download Assignment5.zip from Autolab Attachments.
  2. Unzip the file by double clicking.
  3. Open VSCode
    • Import the folder to a workspace through File > Open Folder

Before submission

Collaboration policy. Read our collaboration policy here.

Submitting the assignment. Submit AirParticles.java and CharacterCounter.java separately via Autolab. To do this, click the Assignments link from the course website; click the Submit link for that assignment.

Getting help

If anything is unclear, don’t hesitate to drop by office hours or post a question on Piazza.

  • Find instructors office hours here
  • Find tutors office hours in Canvas -> Tutoring, RU CATS
  • Find head TAs office hours here
  • POST on Piazza in Canvas -> Piazza
  • In addition to office hours we have the CSL (Coding and Social Lounge), a community space staffed with lab assistants which are undergraduate students further along the CS major to answer questions.
  • Refer to our Programming Assignments FAQ for instructions regarding our assignments and policies.

Submitting

Write 2 programs and submit on Autolab.

  • We provide a zip file (find it under Assignment on Autolab) containing AirParticles.java and CharacterCounter.java.
  • For each problem UPDATE and SUBMIT the corresponding file.