ENGR 131 HW 7

From Johnwiki

Jump to: navigation, search

ENGR 131 Homework #7: Arrays

Due at beginning of next recitation. Turn in the following:

  • Typed sheet with answers to the questions and survey below.
  • A hard copy of your final program.
  • An email to me with your program files (.java) attached.

Contents

Introduction

This assignment will familiarize you with arrays in Java. An array is a list of data with a fixed number of elements. An array may contain any type (e.g., int, String).

Arrays are declared like this.

 int[] integerArray = new int[10] ; // List of 10 integers.

The size of the array may depend on a variable.

 Scanner input = new Scanner(System.in) ;
 System.out.printf("How many integers? ")
 int size = input.nextInt() ;
 int[] integerArray = new int[size] ;

Arrays have two special properties. One is length, which returns the size of the array. The other lets you store or retrieve the value of the array at an index i with the syntax [i]. Note, you can any variable or literal number, not just i.

 int[] primes = new int[3] ;
 
 primes[0] = 2 ; // Store first three prime numbers.
 primes[1] = 3 ;
 primes[2] = 5 ;
 
 for(int i = 0 ; i < primes.length ; i++) // Example of 'length'.
   System.out.println(primes[i]) ;

Instructions

Your assignment is to write a program that prints the most frequent names in a roster. That is, the names with the highest number of occurrences. The user will enter a list of names. Your job is to print out the name (or names) that appear most frequently.

  1. Prompt the user for how many names to store.
  2. Prompt the user for each name, and store them all in an array.
  3. Determine which names appear most frequently, and print them out on a single line. Do not repeat any!

Example

 How many names? 5
 Name 1? Bob
 Name 2? Alice
 Name 3? Charlie
 Name 4? Charlie
 Name 5? Bob
 These names appear most frequently: Bob Charlie

Hints

  • Write a function frequencyOfElement that calculates the frequency of a given name in the array. If the array A is {"Louie", "Lindsay", "Jason", "Louie"}, then frequencyOfElement(A, "Louie") is 2.
 static int frequencyOfElement(String[] array, String name)
 {
   // Returns frequency (number of occurrences) of the given name.
 }
  • Write a function uniqueElement that returns only the unique elements in a array. Using the array A from the previous hint, uniqueElements(A) is {"Louie", "Lindsay", "Jason"}.
 static String[] uniqueElements(String[] array)
 {
   // Returns a new array containing the unique elements of array.
 }
  • Use the equals method of String to test for equality between two Strings.
 String x = "test" ;
 String y = "tes" + "t" ;
 
 if(x.equals(y)) // RIGHT!
   System.out.printf("%s equals %s%n", x, y) ;
 else
   System.out.printf("%s doesn't equal %s%n", x, y) ;
 
 if(x == y) // WRONG!
   System.out.printf("%s equals %s%n", x, y) ;
 else
   System.out.printf("%s doesn't equal %s%n", x, y) ;

Questions

Question 1

How would you modify your program to find the most frequent words in a file? What about frequent two-word combinations?

Question 2

If the input were only integers, you can solve this problem with a single loop through the input. Can you describe how to do this? (Hint: arrays are indexed by integers).

Question 3

If you could index (access the elements) an array by a String instead of an integer, could you describe a more efficient method to solve the original problem? For example, instead of array[2] = 1, you could say array["foo"] = 1.

Survey

Rate the difficulty for you on a scale of 1 to 10 (1 = Very easy for me, 10 = Very difficult for me).

Rate your understanding of the requirements on a scale of 1 to 10 (1 = Did not understand anything, 10 = Understood everything).

Personal tools