CPSC 124, Fall 2005
Programming Assignment 1: Random Math Quiz
In addition to the labs, there will be three or four longer programming assignments, including a final programming project of your choice. The first of the assignments is due on Friday, October 7. We will be covering material from Chapter 4 on subroutines before the assignment is due, but you should do the assignment using only material from Chapters 2 and 3. That is, you should write the entire program as a single main() routine.
This is an individual assignment. You should not give help to or receive help from any other member of the class. You should not discuss your program or show your work to other people in the class. You can get help from me or from the computer science TA's, but not from anyone else. You should not copy code from the Internet or from any other source. This is a matter of academic honesty and is covered by the Colleges' academic honesty policy.
The assignment is to write a more advanced version of the math quiz program from the second lab. In the new version, you will create random questions of several types, so that no two people taking the quiz will see the same questions.
You should ask ten questions. There should be at least four different types of questions (such as addition, subtraction, multiplication, and division problems). For each of the ten questions, you should pick the type of question at random. You should also pick the numbers used in the question at random. For example, for an addition problem, you need to pick two integers at random from some reasonable range of values (say 0 to 99). You should present the question to the user, read the user's answer, and tell the user whether or not their answer is correct. If the answer is not correct, tell the user the correct answer. At the end of the quiz, tell the user how many questions were answered correctly. (You will not be able to print out a table of questions at the end, as you were asked to do in Lab 3, since you have no reasonable way of remembering what all the questions were.)
For full credit, you should improve on the basic program by giving the user a second chance to answer a question, if their first answer is incorrect. The user should get only two chances to answer the question. If neither answer is correct, you should tell the user the answer. At the end of the quiz, tell the user how many questions were answered correctly on the first try and how many were answered correctly on the second try.
Use a for loop to ask the questions. Do not simply copy-and-paste the same code segment ten times. It should be possible to change the number of questions in the quiz by changing just one or two lines in the program.
If you want to do something more interesting than simple +, -, *, and /, you can use other types of problems. One idea is a simple algebra quiz where the user has to solve equations of the forms x + A= B, x - A= B, A * x= B, and A * x + B = C, where A, B, and C are randomly chosen constants and the objective is to solve for x.
I strongly suggest that you write a pseudocode algorithm for the program before you begin and that you discuss your algorithm with me if have any doubts about how to proceed. Remember that part of the assignment is for you to make sure that you understand what you are being asked to do.
Since an assignment is a more serious sort of thing than a lab exercise, I will expect you to follow the rules of good programming style in your program, and I will take off points when you violate those rules. More rules will come up as we cover new features of Java, but here are some rules that should be applied now:
- Formatting: Your program should be indented to show its structure. (Eclipse will do this for you!) The program should use blank lines and spaces to make it easier to read. You should try not to have any line that is longer than about 80 characters, so that the line will not be split when the program is printed.
- Variable names: Use meaningful names for your variable names. For example, numberCorrect is a better name than count. Not every variable needs a meaningful name. For example, i is often used for the name of a variable in a for loop. In general, a variable name should start with a lower-case letter, and sub-words such as "Correct" in "numberCorrect" should be capitalized.
- Comments: There should be a multi-line comment at the beginning of the program that describes the purpose of the program and contains any information that a user would need to know to use the program correctly. Each important variable should have a comment that explains its purpose in the program. Other than that, comments should be fairly rare, although you might need an occasional comment to help a reader understand the design or logic of the program. Never use comments that explain how Java works. For example, a comment that says "declare the variable x" or "output the value of y" is useless.