Assignment 6

You will practice arrays, writing functions/methods, and reading from files.

Methods and functions are used interchangeably in the text. In reality:

  • Functions are public static methods that belong only to the class.
  • Methods (you will learn later) are not static and belong to the object.

 

1. Air Particles (30 points)

The purpose of this assignment is for you to practice 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 people’s 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.

Programming

Write one function and the main method for testing 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

  1. read in the total number of lines in the file (this is the first line of the file)
  2. initialize the array
  3. read from StdIn while it isn’t empty
    populate the array with the double you read in
  4. call findHighestLevel on your array
  5. print “Highest PM2.5 Level is ##” (## represents an actual number)


Executing and Debugging

  • First navigate to the Assignment6 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. Hangman (40 points)

In this assignment you will practice writing functions by implementing a simple hangman game. The functions you will implement perform a functionality of the hangman game, such as checking if this letter is a part of the chosen word, or checking if the letter was guessed already or not. 

You will need to read from an input file using StdIn and randomly select a word, keep track of what letters were guessed in a character array, and check whether the character guessed is a part of the word. StdOut will be used to print out game stats to an output file. 

Overview of files provided

  • UPDATE and SUBMIT the Hangman.java file to Autolab
  • Multiple input files(input0.in, input1.in, input2.in, input3.in, and input4.in) are included; they store a variety of words to play the hangman game. Each file has a number at the top indicating how many words are in that file. You are welcome to create your own input files, as they will not be submitted to Autolab.
  • The StdIn/StdOut/StdRandom classes are provided to read data, export to an output file, and generate random numbers. Do not submit to Autolab.
  • The Hangman class holds the methods the students will implement. This file will be submitted to Autolab.

Programming

The first function is provided for you.

1. selectWord – PROVIDED

Randomly selects a word from an input file to use for the hangman game. The input file contains the words that will be used in the game.

DO NOT UPDATE this function.

 

To run the program.
          java Hangman input0.in stats.out

The word contains 5 characters 
input0.in contains only 1 word, which has 5 characters
input1.in contains 5 words, each of a unique length from 3 to 7 characters 

At this point the rest of the game will not work since you have not filled in the rest of the functions. Entering in a letter for input will not behave properly. You can enter 0 to exit the program. 

Here is an example of running the program with input0.in: 

 

Functions to be implemented by you:

 

2. checkLetter

This function updates the array of letters based on the letter that the user guessed.

Traverse the word array to check if the guessed letter is in the word. The word and the userLetters arrays are guaranteed to have the same length, these are parallel and their indices map directly. 

If the letter is in the word, update the userLetters array by accessing the position of that letter in the array (as it appears in the word) and changing it from ‘_’ to the letter. 

Return true if the guessed letter was in the word and false if it was not. 

Here is a visual to show how this function is expected work: 

Testing checkLetter function:

Run the program as shown in function (1). 

Enter in letters as guesses and watch as they change from _ to the guessed letter. 

input0.in has only one word (array), so that will always be the chosen word. 

  • If you guess a, r, or y then the program will print “{letter} was in the word”, and the character output will change from _ to the respective letter. 
  • For any other letter the program will print “{letter} was not in the word”, and the underscores will stay exactly as they were the last time they were printed.
  • At this point if you guess all the letters in the word, the program will not end since checking if the game has been won will be implemented in a later function. You can enter 0 to quit the program. However, if you guess wrong 6 times, then the program will end since the full hangman character has been drawn. 

Here is an example of a correct guess:

Here is an example of an incorrect guess:

Notice the first part of the hangman character being drawn. 

 

3. markGuessed

Locate the index of a letter in the guessedLetters array and mark it as true if it was guessed. This helps us keep track of which letters were already guessed, so we do not use repeated letters. 

It is guaranteed that only lowercase letters will be passed into this function. The guessedLetters array is a boolean array that is already created, and has 26 spaces, one for each lowercase letter of the alphabet. At the very start, all indices will contain false by default.

  • Index 0 is ‘a’, index 1 is ‘b’, index 2 is ‘c’, and so on.

As computers can only understand numbers, letters are represented using numbers. Each letter has a decimal value in the ASCII table. For example, a is 97, b is 98, A is 65, and B is 66, and so on.

  • Calculate the index of the letter by converting the letter to an int, and subtracting the numerical value of the letter ‘a’ from this letter. (Cast ‘a’ to an int).

In the example below the guessed letter ‘u’ has the decimal value 117.

  • Since index 0 refers to ‘a’ (decimal value 97), we subtract 97 from 117. 
  • The result is 20, that is the index for ‘u’. 

Return true if the letter was not guessed before, or false if the letter was already guessed. 

Here is a visual where we guess the letter u twice in a row. Blank spaces represent false. 

Testing markGuessed method

Run the program as shown in function (1) and try to guess the same letters multiple times. If a letter was guessed for the first time, the program prints “You guessed {letter}”. Otherwise if a letter was already guessed before, the program will print “You already guessed the letter {letter}, please enter another letter”.

Once again, if you win then the game will not end as this is implemented in the next method. Enter 0 to exit the program.  

Here is an example with guessing the same letter again:

 

4. checkWin

This function traverses the userLetters array and checks to see if there are any unknown spaces left. An unknown space is marked by the underscore character ‘_’. 

  • If there are no underscores left, the game is won.

Return true if there was a win, and false if there wasn’t. 

To test, you can run the program and enter in guesses. If you guess all the characters in the word (no more underscores get outputted) then the program will end. Otherwise if there is at least one more unknown character and underscores are still being outputted, then the program will NOT end. 

For input0.in, the word is always “array”. If you enter a, then r, then y in that same order, then

  • The program will not end after entering a
  • The program will not end after entering r
  • The program will end after entering y

Once the program ends successfully it may print “Exporting stats to {output file name}…”. This will be filled out in the next function. 

Testing checkWin method

Here is an example of running with input0.in. In this example checkWin returns false after the first 2 guesses so the game continues, but after the 3rd guess it returns true and the game ends with a win:

 

5. exportStats

This method exports stats such as whether you won or lost, the number of points earned, what the word was, and the number of total and incorrect guesses to an output file. A lot of the information is passed into the function as parameters, so pay attention to what information you already have access to.

Use StdOut.setFile(filename) to indicate the output file (the output file name is passed into the function). 

Use StdOut.println() to print statements to the output file. 

Expected output format (follow this format):

  • 1st line: (You won) or (You lost)
    • Call the checkWin() function with the correct arguments to see if there was a win. 
  • 2nd line: Points: {int points}
    • If there was a win, calculate the number of points using this formula: 

100 – (number of incorrect guesses)/(number of total guesses) * 100

  • If there wasn’t a win, print the number of points as 0. 
  • 3rd line: The word was: {word}
  • 4th line: Total guesses: {number of total guesses}
  • 5th line: Incorrect guesses: {number of wrong guesses}

Only print what is expected of the program, do not print anything else.

  • This output is case sensitive. 
  • Avoid any extra spaces.

Once the game ends without the user quitting (either you ran out of guesses or got the word correctly), then these will be printed to the output file name you specified in the command line arguments, this file will be created by the program if it doesn’t exist already. You can check the output file to see if it is formatted correctly, and that everything printed makes sense. For example, if you lost, then the score must be 0, otherwise it must be a number between 0 and 100, the more wrong guesses there are the lower the score is. 

Here is an example of what the output file looks like when we win the game but got 2 incorrect guesses:

Here is an example of what the output file looks like when we lost the game:

Note: total guesses may vary but points will always be 0 whenever you lose, in this example we entered in 6 incorrect guesses in a row

Executing and Debugging

    • You can run your program through VSCode or you can use the Terminal to compile and execute. We suggest running through VSCode because it will give you the option to debug.
    • How to debug your code
    • If you choose the Terminal:
      • first navigate to Hangman directory/folder
        • to compile:  javac Hangman.java

By Sulaiman Hasan and Kriti Kumaran

 

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 Assignment6.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, Hangman.java 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 the programs and submit on Autolab.

  • We provide a zip file (find it under Assignment6 on Autolab) containing AirParticles.java, Hangman.java.
  • For this assignment UPDATE and SUBMIT the corresponding files.