CIS 160 - Array assignment

Objectives

  • Create an array
  • Use repetition to obtain user input until the user signals done
  • Use an array to accumulate values
  • Display an array

Assignment

The challenge in this assignment is to ask the user for multiple lines of input and then display the count of each digit contained in that input. The user should be able to enter any regular characters and continue until they type "QUIT" (either uppercase or lowercase). Check the sample output for details about how your program should operate. User input is shown in bold italic text. All the other text should be output by your program.

You are required to use an array of ten elements to store the counts for the digits. The output must be done using a loop rather than having a separate output statement for each array element.

Notes

Some String methods you might find helpful for this assignment are:

  • .length() // returns the length of the string
  • .charAt(int pos) // returns the character (as a char) at the specified position in the string
  • .equals(String s) // returns true is s is the same as the current String
  • .equalsIgnoreCase(String s) // case insensitive version of .equals(String)

Examples:

String course = "CIS 160 Java Programming I"; String s1 = "abc"; String s2 = "ABC"; System.out.println(course.length()); // displays: 26 System.out.println(course.charAt(0)); // displays: C System.out.println(course.charAt(8)); // displays: J System.out.println(course.charAt(0) == 'B'); // displays: false System.out.println(course.charAt(0) == 'C'); // displays: true System.out.println(s1.equals(s2)); // displays: false System.out.println(s1.equalsIgnoreCase(s2)); // displays: true

Sample run

This program counts digit frequencies in the input Enter a string (QUIT to exit): There were 14 students in class today. Enter another string (QUIT to exit): 101 Dalmatians Enter another string (QUIT to exit): 76 trombones in the big parade Enter another string (QUIT to exit): A stitch in time saves 9 Enter another string (QUIT to exit): 2b || !2b Enter another string (QUIT to exit): 123.45 Enter another string (QUIT to exit): 99.87 Enter another string (QUIT to exit): \/\/3 4r3 7h5 1337 h4ck3r5 Enter another string (QUIT to exit): quit The count for digit 0 is 1 The count for digit 1 is 5 The count for digit 2 is 3 The count for digit 3 is 6 The count for digit 4 is 4 The count for digit 5 is 3 The count for digit 6 is 1 The count for digit 7 is 4 The count for digit 8 is 1 The count for digit 9 is 3

Rubric

  • 5 points for proper documentation comments and for correctly following coding conventions (indentation, naming rules, etc.)
  • 5 points for having the input loop run correctly and stop when the user enters: Quit
  • 10 points for correctly counting the digits in the user input
  • 5 points for displaying the array counts using repetition (rather than having a separate print statement specifically for each array element)
  • 15 points for correct use of arrays