🎯 Unit 4 Activities

Table of Contents


πŸ’» ACTIVITY PROGRAM SETUP INSTRUCTIONS
  1. Go to the public template repository for our class: BWL-CS Java Template
  2. Click the button above the list of files then select Create a new repository
  3. Specify the repository name: CS2-Unit-4-Activity-#

    Replace # with the specific activity number.

  4. Click

    Now you have your own personal copy of this starter code that you can always access under the Your repositories section of GitHub! πŸ“‚

  5. Now on your repository, click and select the Codespaces tab
  6. Click Create Codespace on main and wait for the environment to load, then you’re ready to code!

πŸ”΄ ACTIVITY #1: Pokemon Dataset

In this activity, you’ll practice using arrays and working with text files with a dataset about Pokemon.

First, review the notes on the File, Scanner, and IOException classes: πŸ““4.6: Using Text Files

PART A: Create Array from the Data File

The following exercise reads in a data file about Pokemon and prints out the first 10 lines in the file. This file has the extension .csv which stands for Comma Separated Values. All spreadsheets can be saved as CSV text files, and spreadsheet software can easily open CSV files as spreadsheets.

  1. Add the pokemon.csv file (that I emailed to you) to your repository by dragging it into the File Explorer tab.
  2. Include throws IOException in the main method HEADER like below:
      public static void main(String[] args) throws IOException {
     ...
      }
    
  3. Set up File, Scanner, and String[] (array) objects at the beginning of the main method’s BODY:
      File myFile = new File("pokemon.csv");
      Scanner scan = new Scanner(myFile);
      String[] pokemonLines = new String[10];
    
  4. Complete the while loop to read in the first 10 lines of the pokemon file using the Scanner class, save each line into the pokemonLines array, and print it out. Here is the loop structure:
  int i = 0; 
  // 1. Add in the loop conditions
  while (         ) {
    // 2. Read in the next line of the file

    // 3. Assign the line to the ith element of the pokemonLines array

    // 4. Print out the line

    i++; // increment line counter
  }
  scan.close();           
  • Your loop condition should check if scan has another line of input (scan.hasNext()) AND also check that the line counter i is less than 10.
  • To read in the next line of the file, use: String line = scan.nextLine();
  • To assign the line to the array, use: pokemonLines[i] = line;
  • Finally, print out the line before incrementing the line counter!

PART B: Display Data Attributes

If you take a look at the Pokemon CSV file, you’ll notice that each line contains multiple data attributes separated by commas. These attributes include each Pokemon’s name, type, speed, etc. on each row. Typically, the first line of a CSV file serves as the header, indicating the names of these attributes.

// pokemonLines[0] contains the first line of the Pokemon CSV file:
Number, Pokemon, Type 1, Type 2, HP, Attack, Defense, Speed, PNG, Description

Splitting Strings

The Java String class provides a useful method called split(String delimeter) that allows us to split a string into an array of substrings based on a specified delimiter which is a character like a comma or a space that separates the units of data. This method returns a String array where each element in the array represents a field of data from the line.

Here is an example of how to use the split() method to split a line of data with commas separating the fields from the Pokemon csv file into identifiable chunks of data.

The first line of headers in the file indicates that the 0th element of the data array is the Pokemon’s number, element 1 is the name, etc. We only need to save the data that we want to use. In this case, we want to save the name, type1, and speed.

// Split the line of data into an array of Strings
String[] data = line.split(",");
// Identify the data 
// data: Number,Name,Type1,Type2,HP,Attack,Defense,Speed,PNG,Description 
   String name = data[1];
   String type1 = data[2];
   ...
   String speed = data[7];
   String imageFile = data[8];

If we want to do math or comparisons with the speed, we can convert it to an int using the Integer.parseInt method that will be described in the next lesson.

Your program currently reads in some of the data from the pokemon file into a String array of lines.

  1. Complete a randomPokemon() method to print out a random pokemon name:
      /// Write a function that prints out a random Pokemon name
      public void randomPokemon(int length) {
     // 1. pick a random number from 1 to length (the 0th row is the headers)
    
     // 2. get the line at that random index from the array pokemonLines
    
     // 3. Use the split method to split the line into a String array data
    
     // 4. Print out the name using the correct index in the split data
      }
    
  2. In the main, call your new method using: randomPokemon(i); where i represents the length of the file (number of lines we read in during the loop)
  3. Run the program multiple times to see different Pokemon names.
  4. Update your randomPokemon() method to print out other data attributes, such as description or speed.