CPSC 225, Spring 2019
Lab 1: Eclipse / Spellcheck
For the first lab, you will be writing two programs: A fairly short program that administers an arithmetic quiz to the user and a longer program that spellchecks words entered by the user. In each program, you are asked to use an existing class that is provided to you; this will help you review the use of classes and objects. You do not have to write any classes for this lab except for the two classes that contain the main() routines.
You should turn in your work fo this lab by the beginning of lab next Tuesday. The procedure for doing so is given at the end of this document.
You are strongly encouraged (but not absolutely required) to use Eclipse for programming in this course. An introduction to using Eclipse can be found here:
If you have not already used Eclipse, you should read that page to learn how to use it. Even if you have used Eclipse before, you will probably find some useful information about using it on that page (see, in particular the list of "Other Features" at the end.)
To get started on the lab, you can create an Eclipse project with a name such as "lab1" or "cs225_lab1" to hold the files needed for this lab. (If you are not using Eclipse, you will want to create a directory instead to hold your work.) The files that you need for the lab can be found in /classes/cs225/lab1-files. You should copy the following files into your project: ArithmeticProblem.java, WordList.java, and unsorted_words.txt. Remember that when copying files into an Eclipse project, they should be pasted into the project's src folder. For user input in this lab, you have the option of using Scanner or TextIO. If you want to use TextIO, you should also copy the file TextIO.java.
Remember that all programs will be graded for style as well as for correctness. Programs should follow all the rules in the style guide that was handed out along with this lab. This includes the use of Javadoc comments. In case you are not familiar with Javadoc, it is explained below.
Assignment 1: Arithmetic Quiz
For your first assignment, you should write a program that administers a ten-question arithmetic quiz to the user. The program must be named ArithmeticQuiz.java. The program will depend on ArithmeticProblem.java, which you should have already copied into your project. You should examine ArithmeticProblem.java to see what constructors and methods it contains. You should not make any changes to this file.
Each question will be represented by an object of type ArithmeticProblem. The constructor for this class requires you to specify an operator ('+', '-', '*', '/'), but it selects the numbers in the problem for you. You should select a random operator for each problem.
The program should present each problem to the user and get the user's answer. If the user's answer is correct, give the user 10 points. Otherwise, give the user a second chance to answer the question. If the user gets the answer right on the second try, give the user 5 points. If the user's second answer is also wrong, they get no points for the problem. (They do not get any additional attempts to answer the question.)
This program is meant mostly as a warm-up, and it can be quite short. You can write the entire program as a main() routine. You do not have to write any helper methods. You do not need to store the ten problems in an array to do this assignment. You can read user input using either the nextInt() function from the Scanner class or the getlnInt() function from TextIO. You do not need to worry about the possibility that the user might type in something that is not a legal integer. However, you should provide clear, informative, and reasonably attractive output for the user, and you should include a Javadoc comment on the class explaining clearly what the program does.
Here is a sample run from an acceptable solution. The answers that were entered by the user are shown in bold text:
Question 1: What is 41 * 11 ? 451 Congratulations, that is correct! Question 2: What is 64 - 7 ? 57 Congratulations, that is correct! Question 3: What is 95 - 11 ? 84 Congratulations, that is correct! Question 4: What is 80 / 8 ? 10 Congratulations, that is correct! Question 5: What is 86 + 5 ? 93 Sorry, that is not correct. Try again for half credit. Question 5: What is 86 + 5 ? 91 That is correct! Question 6: What is 55 + 14 ? 69 Congratulations, that is correct! Question 7: What is 72 / 12 ? 8 Sorry, that is not correct. Try again for half credit. Question 7: What is 72 / 12 ? 7 Sorry, that is still not correct You get no credit for this problem. The correct answer is 6. Question 8: What is 132 / 12 ? 11 Congratulations, that is correct! Question 9: What is 56 - 7 ? 49 Congratulations, that is correct! Question 10: What is 94 - 16 ? 78 Congratulations, that is correct! Your score on the quiz was 85 out of a possible 100.
Assignment 2: Spellchecking
Your second assignment is to write a basic "spell checker" program that can check words entered by the user. The program must be named Spellcheck.java. You should start by creating a new class named Spellcheck in your lab1 project. This class should contain the main() method for your program. It will also contain helper methods used by the main() routine.
The program should let the user input words, one at a time, and it will spellcheck each of the words. If the word is valid, the program should say so. If not, it should print out a list of valid words that are similar to the user's input word, as discussed below. For a bit of extra credit, when your program presents a list of alternative words to the user, it should not include any duplicates in the list, and the list should be in alphabetical order. (Hint: Look ahead in the textbook for the "TreeSet" class!) The program should be nice to the user: Prompt for inputs, label the output, and format the output nicely. The program should break up its task into subroutines. And follow all the other rules of good style! Remember that programs are graded partially for style!
The program requires the files WordList.java and unsorted_words.txt, which you should have already added to your lab1 project. An object of type WordList represents the list of correctly spelled words from the file unsorted_words.txt. (There are 72875 words in the list.) Your program will use an object of type WordList. Your program should only create one WordList object, and use the same object for all tests. An object of type WordList has an instance method
public boolean contains(String lowerCaseWord)
that tests whether a given word is in the list. The parameter must be in lowercase for the test to work correctly. The file unsorted_words.txt contains the list of words; this file automatically read when you create a WordList object.
The hard part of the program is finding correctly spelled words that are "similar" to an incorrectly spelled word. The idea is to modify the incorrectly spelled word in certain ways, and look up the result in the list of correctly spelled words. Any correctly spelled words that you can find in this way go into the output list of possible corrections.
You should implement the following ways of modifying the incorrectly spelled word:
- Delete a character. Try this for each character in the original word.
- Add a character. Try putting each of the 26 letters of the alphabet in each of the possible positions in the original word.
- Change a character. Try substituting each of the 26 letters of the alphabet for each of the characters in the original word.
- Swap two characters. Try reversing the order of each consecutive pair of characters in the original word.
- Insert a space. Try inserting a space into each possible position in the original word, breaking it into two words. In this case, you have to check that both of the words that you make are in the list of correctly spelled words.
In class, as an example, I will show how to do one of these tests. A few more hints: Consider using the StringBuilder class instead of String for some operations. Remember that a char variable can be used as the loop control variable in a for loop. And review the String/StringBuilder API for working with substrings.
Javadoc is a standard syntax for comments in Java source code. Javadoc was introduced in Section 4.5.4 of the textbook, and you can read about it there. Javadoc comments can be processed to produce documentation of your code's API in the form of web pages.
Javadoc is thoroughly integrated into Eclipse. If you highlight the name of a class, method, or variable, its Javadoc documentation (if any) appears in the "Javadoc" view. If you just hover your mouse over a name, the Javadoc documentation appears in a pop-up window. When Content Assist shows a list of variable or method names, it will also show Javadoc documentation for items in the list. This makes it very worthwhile to use Javadoc comments in your code, even if you don't plan to generate web pages from the comments.
Furthermore, Eclipse will generate the outline of a Javadoc comment for you. To have it do this, just position the cursor on the line before a declaration, type /** (the first three characters of a Javadoc comment), and press return. An outline of the comment, including any appropriate Javadoc tags (such as @author, @param, @return, and @throws) will appear.
Some things to keep in mind: A Javadoc comment always comes before the declaration that is being commented. Javadoc comments should only be used for classes, methods, and instance variables. Any Javadoc tags, such as @param and @return, must come at the end of the comment, after any other text in the comment. Javadoc comments can contain HTML markup tags such as <p> or <b>...</b>. This means that you should not use the characters "&" or "<" in a Javadoc comment; write them as "&" or "<" instead.
In this course, you are required to use Javadoc comments for any non-private classes, member variables, and methods that you write. This requirement starts now.
Turning in Your Work
You have a homework folder in the directory /classes/cs225/homework, identified by your last name. You will turn in most assignments by copying them into that folder.
For this lab, you should create a directory named "lab1" inside your homework folder to hold the files that you turn in. You should turn in all the files that are needed to compile and run your programs. That includes: ArithmeticQuiz.java, ArithmeticProblem.java, Spellcheck.java, WordList.java, and unsorted_words.txt. If your programs use TextIO, then you should also turn in TextIO.java.
You can turn in your work using either the GUI or the command line. To turn in your work using the GUI, you should open a file browser window and navigate to your homework folder, which has a name such as /classes/cs225/homework/Smith. You should create a folder in that directory named lab1. You can then copy-and-paste the files from the src folder in the Eclipse window into the new lab1 directory. If you want to use the command line, you can create the folder with a command similar to
(Of course, you should substitute your own name for "Smith".) Then navigate to your project's src folder and copy the files. Depending on the name of your Eclipse workspace and your Eclipse project, this would look something like this:
cd eclipse-workspace-225/lab1/src cp * /classes/cs225/homework/Smith/lab1