Skip to Main Content

Introduction to Computer Science

Computer Science Department

Functions – 50 course points

The purpose of this assignment is to practice functions, passing arrays as arguments to functions, update arrays within a function, and returning arrays.

Refer to our Programming Assignments FAQ for instructions on how to install VScode, how to use the command line and how to submit your assignments.

Programming

Write 2 program and submit on Autolab.

  • We provide a zip file in Autolab containing AirParticles.java and ISBN.java.
  • For each problem UPDATE and SUBMIT the corresponding file.

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 methods
  5. DO NOT add any new class fields
  6. DO NOT use System.exit()
  7. ONLY print the result as specified by each problem. Observe the examples’ output, display only what the problem is asking for.
    1. DO NOT print other messages, follow the examples for each problem.

1. Air Particles (15 points). 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 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.

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.

The main method is implemented for you to test your function using the input file PM2.5.txt. Execute the program that reads the input file, populates the array, and call the findHighestLevel function as follows:

  • java AirParticles < SmallerPM2.5.txt
  • java AirParticles < PM2.5.txt

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

2. ISBN (35 points). The International Standard Book Number (ISBN) is a number that uniquely identifies a book. Publishers can buy or receive ISBNs from the International ISBN Agency. There are two types of ISBNs: ISBN-13 and ISBN-10, which are 10- and 13-digits long, respectively.ISBN-10 has a pattern, where the last digit can be found using the first 9. Additionally, ISBN-10 can also be used to calculate its ISBN-13 equivalent (and vice versa).

Write two functions in ISBN.java. The main method is implemented for you so you can test your functions. Instructions on how to run this file to make use of the test.txt file are detailed below.

    1. calculateISBN10(int[] isbn) computes the last digit of the ISBN-10 number.
      The first 9 of the 10 digits of the ISBN will be passed to the function as the parameter isbn integer array of size 10. The last integer in the array will be -1. Your job is to calculate the last digit (called the check digit), and update the last index of the isbn array with the computed value. Here is how to compute the check digit:
      • Step 1. multiply each isbn digit (starting at the first digit) by an integer weight (starting at 10 and descending to 2).  Add all the products together, let’s call this number sumOfProducts
      • Step 2. Find the remainder when sumOfProducts is divided by 11, let’s call this number rem
      • Step 3. Find the difference between 11 and rem, call this number diff.
      • Step 4. The check digit is the remainder of diff divided by 11. Update the last array index with this value.

      Let’s do an example. The 10-digit ISBN number for Pride and Prejudice by Jane Austen is 0141439513. Here is how the last digit 3 is computed:

      • The isbn array will contain the first 9 digits 014143951:  
        • Step 1: (0*10) + (1*9) + (4*8) + (1*7) + (4*6) + (3*5) + (9*4) + (5*3) + (1*2) = 140
        • Step 2: 140 % 11 = 8
        • Step 3: 11 – 8 = 3
        • Step 4: 3 % 11 = 3
      • Our calculation gave us 3 as our check digit. Update the last index of the array with this value.
    • Note 1: The check digit will be a number between 0 and 10. (Do not worry if it is a 10, as the main method will print it out as an “X” so as to not make it an 11-digit ISBN).
    • Note 2: All of the 9 integers of the incomplete ISBN will be between indexes 0 and 9.
    •  
    • In this function you are practicing updating the input array. Once calculateISBN10 is done executing, the array update made by the function is visible to the caller function.
    •  
    1. calculateISBN13(int[] shortIsbn) uses the parameter integer array shortIsbn to compute the ISBN-13 number.
      The integer array parameter, shortIsbn, is of size 10, it contains a ISBN-10 number. Your job is to create a new integer array of size 13, compute the ISBN-13 number, store the number in the created array and return it.

      Here is how to computer the ISBN-13 number:

      • Step 1: The first 3 digits of an ISBN-13 are always 978. The rest of the 10 numbers will be the ISBN-10. This gives us a 13-digit number. Store all 13 digits into the array of size 13 you created.
      • Recalculate the last digit (the check digit).
      • Step 2: Multiply each of the first 12 digits with 1 or 3, alternatively. Add all the twelve products together, let’s call this number sumOfProducts.
      • Step 3: Find the remainder when sumOfProducts divided by 10, let’s call this number rem.
      • Step 4: If rem is 0 (zero), then the check digit is zero. If rem isn’t 0 (zero) the check digit is the difference between 10 and rem. Update the last array index with this value. 
    • Let’s do an example.
    • The ISBN-13 of Pride and Prejudice by Jane Austen, is 9780141439518. Let us see how to compute this number.
      • The method receives, through the shortIsbn array, the ISBN-10 for Pride and Prejudice by Jane Austen, which is – 0141439513: 
        • Step 1: Insert 978 in front the ISBN-10 number, 9780141439513. Store all 13 digits in the array size 13 you created.
        • Step 2: (9*1) + (7*3) + (8*1)  + (0*3) + (1*1) + (4*3) + (1*1) + (4*3) + (3*1) + (9*3) + (5*1) + (1*3) = 102
        • Step 3: 102 % 10 = 2
        • Step 4: 2 is not zero, so 10 – 2 = 8
        • Our calculation gave us 8 as our check digit. Update the last index of the array with this value.
    • In this function you are practicing creating and returning a new array to the caller function.

The main method is implemented for you to test your functions using the input file books.txt. Execute the program that reads the input file, and calls the two function as follows:

  • java ISBN < books.txt

Note 1: indices 0 to 10 will not be updated.

Note 2: only the main method displays X in place of 11, and 0 in place of 10.

Problem by Aastha Gandhi

Before submission

Collaboration policy. Read our collaboration policy here.

Submitting the assignment. Submit AirParticles.java and ISBN.java via the web submission system called 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 CAVE (Collaborative Academic Versatile Environment), a community space staffed with lab assistants which are undergraduate students further along the CS major to answer questions.