CS 124, Spring 2013
Lab 9: Objects and ArrayList
In this lab, you will be working with classes and objects. In the first exercise, you will create a class and then use that class to create an object. In the second exercise, you will create several objects that are instances of the same class. The second exercise uses an ArrayList. Although ArrayLists are not covered until Chapter 7 in the textbook, we discussed them in class, and there is enough information in this lab to enable you to use one.
You should create a lab9 project in Eclipse. You will need copies of TextIO.java and ArithmeticProblem.java. You can find copies in /classes/cs124.
As usual, this lab is due next Thursday.
Exercise 1: Writing and Using a Class
For the first exercise of the lab, you will write a small class and a short main program that uses it. This should be very quick! To keep this exercise quick, you are not required to add comments to the code that you write for this exercise.
First, create a class named Person. To start the class, right-click your lab9 project in the Eclipse window, and select "New" / "Class" from the pop-up menu. Name the class Person. This is just like creating a program, but the class that you create will not have a main() routine. The class should have public instance variables of type String to hold the person's name and telephone number. It should have a public instance variable of type int to hold the person's age.
Now, write a main program to use the Person class. Create another class in your lab9 project, and add a main() routine to that class. The main routine should do the following:
- Create a variable and an object of type Person.
- Ask the user for their name, age, and telephone number, and store the user's answers in the Person object.
- Print out the values that have been stored in the Person object.
For example, when the run the program, it might look like this, with the user responses underlined:
What is your name? Fred Smith How old are you? 27 What is your telephone number? 315-555-1212 You have entered the following information: Your name: Fred Smith Your age: 27 Your telephone number: 315-555-1212
You should not worry about any kind of error checking of the user's input. Remember that the point is to create and use an object of type Person.
(Note: As I said in class, many people consider any public instance variable to be a bad idea. They would say to use private instance variables with getter and setter methods. You are not required to do that for this exercise, but you can do so if you want. If you do, you can get Eclipse to write the getter and setter methods for you. Just add your private instance variables to the class, right-click inside the editor window where you are writing Person.java, and then select the "Generate Getters and Setters" command from the "Source" submenu. In the dialog box, check all three instance variables, and click "OK". (There is also a "Source" menu in the Eclipse menubar that you can use.)
About ArrayList
An ArrayList is a list of objects. ArrayList is a standard Java class in the package java.util. This means that if you want to use it in a .java file, you should put
import java.util.ArrayList; or import java.util.*;
at the top of the file. ArrayList is a so-called "parameterized class." Use the type ArrayList<String> for a list that can hold objects of type String. Use ArrayList<Person> for a list that can hold objects of type Person, and so on. (Unfortunately, you can't have an ArrayList that contains primitive type values; it only works for objects.) For example, a list of strings could created as follows:
ArrayList<String> list; // Declare the variable to point to the list. list = new ArrayList<String>(); // Create the ArrayList object and assign it.
The following methods are defined for an ArrayList list:
list.size()
-- a function that returns the number of items currently in the list. Items are numbered from 0 up to list.size() - 1.list.add(item)
-- adds an item to the end of the list. The size of the list goes up by 1. The class of the item must match the type of item that the ArrayList can hold.list.get(i)
-- a function that returns item number i from the list, where i must be an integer in the range 0 to list.size() - 1.list.set(i,item)
-- sets the value at position number i to item, replacing the value that was there previously, where i must be an integer in the range 0 to list.size() - 1.list.remove(i)
-- removes the i-th element from the list, where i must be an integer in the range 0 to list.size() - 1. The size of the list decreases by 1. Items in the list after position i move up one space to fill the space vacated by the removed item. list.remove() is a function that returns the removed item; compare it to list.get(), which returns the same item but does not delete that item from the list.
Note that ArrayLists are often processed with for loops. For example, to print out all the strings in an ArrayList of strings, you could use the method:
static void printAll( ArrayList<String> list ) { for ( int i = 0; i < list.size(); i++ ) { String str = list.get(i); System.out.println(str); } }
You should compare this to printing out all the characters in a string.
Exercise 2: Math Quiz Redux
As an exercise in using objects and ArrayLists, we will return to a problem from Lab 3: administering a simple arithmetic quiz. There are a few changes in the new version. First of all, instead of just working with addition, an arithmetic problem can be addition, subtraction, multiplication, or division. Secondly, instead of telling the user the correct answers immediately after administering the question, the program will print a report at the end containing all the questions and their correct answers. This means that you will have to save the questions from the time they are created to the end of the program. To implement this, you will use objects to represent the questions, and you will store those objects in an ArrayList. The basic algorithm for the program is:
Create the ArrayList Create 10 questions and store them in the ArrayList Administer the quiz Report the score Print all the questions and their correct answers.
You should write a main() method to implement this outline, and you should write static methods to implement at least some of the steps in this outline. Part of your grade for this exercise will be based on how you design the methods and, of course, the comments that you write for them. Here is an example of what a program run might look like:
Question 1: Compute 30 + 21 Please enter your answer: 51 Congratulations! That is correct. Question 2: Compute 26 * 10 Please enter your answer: 260 Congratulations! That is correct. Question 3: Compute 12 * 6 Please enter your answer: 72 Congratulations! That is correct. Question 4: Compute 33 - 28 Please enter your answer: 5 Congratulations! That is correct. Question 5: Compute 140 / 4 Please enter your answer: 32 Sorry, that is not correct. Question 6: Compute 87 + 11 Please enter your answer: 98 Congratulations! That is correct. Question 7: Compute 31 * 5 Please enter your answer: 155 Congratulations! That is correct. Question 8: Compute 28 - 5 Please enter your answer: 23 Congratulations! That is correct. Question 9: Compute 16 + 21 Please enter your answer: 37 Congratulations! That is correct. Question 10: Compute 24 * 1 Please enter your answer: 25 Sorry, that is not correct. You had 8 correct answers out of 10 questions. Your score on this quiz is 80% Here are the correct answers to all the question on the quiz: Question 1. 30 + 21 = 51 Question 2. 26 * 10 = 260 Question 3. 12 * 6 = 72 Question 4. 33 - 28 = 5 Question 5. 140 / 4 = 35 Question 6. 87 + 11 = 98 Question 7. 31 * 5 = 155 Question 8. 28 - 5 = 23 Question 9. 16 + 21 = 37 Question 10. 24 * 1 = 24
Note that for this lab, you are not required to give the user a second chance to answer the problem, as you were in Lab 3, although you can do that if you want.
You should use the class ArithmeticProblem, defined in the file ArithmeticProblem.java, to represent the questions on the quiz. Note that this class has a constructor that requires you to specify the operator and the two operands for the problem. When you create a question in the main program, you should first select one of the operations -- addition, subtraction, multiplication, or division -- at random. You should then choose the operands at random. Once you have the operator and operands, you can create an ArithmeticProblem, which should then be added to an ArrayList that holds the ten questions for the quiz. The ArrayList will be of type ArrayList<ArithmeticProblem>. There are a few requirements for the problems that you create:
- For multiplication, one of the operands should have just one digit.
- Subtraction problems should not have a negative answer. This means that the second operand must be less than or equal to the first.
- Division problems should not require division by zero.
- For division problems, the answer should be an integer. That is, the second operand should evenly divide the first operand. An easy way to do this is to choose the second operand and a multiplier at random, then compute the first operand by multiplying the second operand by the multiplier. Don't make the second operand and multiplier too big! You want to keep the problems reasonable.