Lab 2: Dictionary File Reading Program
Due Date and Submission Requirements
- Due Date: Thursday, September 5th at 11:59 p.m.
- Partner Information: This is an individual assignment. You are allowed to collaborate with other students, but each student must submit their individual, independent solution.
- Submission Instructions: Upload your solution (.java file), entitled Dictionary.java to the BrightSpace(D2L) Lab 2 Dropbox. Do not rename your .java files
The goal of this lab is:
- Read from a file using Java
- Gain practice writing different kinds of methods
- Use if statements to check conditions
- Use loops and arrays
Background and Directions
In this assignment, you will be forming a dictionary in your program from reading from a file. You will use words.txt. To fill your dictionary. This file has most of the words in the english dictionary.
Using Lab2Demo.Java, as a starting point, you will supply the necessary classes and methods to get the correct output. You can download this file, or copy and paste it into your own Lab2Demo.java.
You first need to define the instance fields and constructor for the Dictionary class. There will be at least one instance field. You will need to keep track of all the words from the input file, which will be a String[].
You will only read from the file once. I would recommend writing a method, perhaps called readFromFile() which will open the file, and then return the filled array of Strings.
public Dictionary() {
this.words = readFromFile();
}
There are 41193 words in the input file. You can hardcode this value in your code when creating the array. The words are not in alphabetical order. You will need to make sure the words are sorted alphabetically in your dictionary before moving to the next part.
In the Dictionary class, you will need to define the following methods:
- firstWord()- returns the first word of the dictionary.
- lastWord()- returns the last word of the dictionary.
- longestWord()- computes and returns the longest word in the dictionary. Hint: there is a .length() method that will return the length of a string.
- countByLetter(char inputLetter)- given an input letter (a char), this will count and return the number of words in the dictionary that start with that letter. Hint: there is a .charAt(X) that will return the character at index X of a string. You can assume the use will always enter a valid, lowercase letter.
Rules
You are NOT allowed to modify Lab2Demo.java. If you modify Lab2Demo.Java in any way, then you will lose significant points! The TAs will grade with the same demo class, and same input file.
Starting Code
Required Output
When your program is run, your output should look exactly the same as seen in this screenshot .
Grading (10 points)
- 2 points - Your Dictionary class is correctly defined
- 2 points - Your program reads from the input file and reads each word in
- 1 point - firstWord() is correct
- 1 points- lastWord is correct
- 2 point- longestWord() is correct
- 2 point- countByLetter() is correct