Counting Letters with methods: Fall 2025

02 Aug 2025 - 21:58 | Version 1 |

Learning Objective(s)

  • Learn to use methods
  • Abstraction

Overview of the Program

Modify the previous program so that it uses methods

Specifics

  • The program is to be called CountLettersMethods.
  • The program should prompt for the name of the file to be processed.
  • Exceptions must be caught and properly handled.
  • If a letter does not occur on a line (or in the file), it should not be displayed.
  • You may assume the file only contains lowercase letters.
  • You should have a method called countString that is sent a character and a line (as a string). It returns the number of times that character appears in that string.
  • You should have another method called countFile that is sent a character and an open file. It outputs how many times that character occurs on each line in the file. Because it does not open the file, it should not close the file.
  • You may use the posted solution from the previous lab.

Sample dialogue

Below is a sample dialogue. The prompts may vary but the input given by the user is to be the same format. Your output can vary somewhat as long as the required data is nicely displayed in the order specified.
What file would you liked to have processed? small.txt
Line 1 has 1 a's.
Line 2 has 1 a's.
Line 4 has 1 a's.
Line 4 has 1 d's.
Line 2 has 1 e's.
Line 4 has 1 e's.
Line 2 has 1 f's.
Line 4 has 1 f's.
Line 1 has 1 h's.
Line 3 has 1 h's.
Line 1 has 2 i's.
Line 2 has 1 i's.
Line 3 has 1 i's.
Line 3 has 1 j's.
Line 2 has 3 l's.
Line 2 has 1 m's.
Line 4 has 1 o's.
Line 4 has 1 r's.
Line 1 has 2 s's.
Line 2 has 1 s's.
Line 3 has 1 s's.
Line 4 has 1 s's.
Line 1 has 1 t's.
Line 3 has 2 t's.
Line 3 has 1 u's.
Line 3 has 1 w's.
Line 4 has 2 w's.
You can also run a solution for this program if you want. See classroom instructions for more details.

Lab Hints

Submission

  • Create a software development report
    • For the system design, you need to make your own flowchart.
    • For testing report,
      • make sure your tests ensure that each statement is executed at least one.
      • your expected output CANNOT be pasted from the dialogue of a running. Only the actual output may be pasted
      • It should contain at least one different input that is an "error"
    • For the improvement section, specify how many points were lost from the last lab that you should not lose points for on this lab because you corrected the problem
    • create a pdf document from your software development report
    • create an empty zip folder
    • place only CountLettersMethods.java, your own testing files (not mine) and your pdf document into it
    • submit the zip folder on canvas

Optional improvements

Optional improvements should ONLY be worked on once the program is completing working. In addition, the work is to be done on your own with little if any help for lab assistants or the instructor.
  1. Treat uppercase letters as lowercase letters
  2. For each letter in the file, print the first line and last line that contain that letter (after printing how many letters in each line).
  3. Submission instructions
    1. Call this program CountLettersMethods2 add it to the zip folder that contains the original solution.
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback
This website is using cookies. More info. That's Fine