RPG Test

  1. Read through the code below, which involves manipulating Strings and ArrayLists. If a line results in an exception, write next to the line what sort of exception was caused, and assume that nothing in that line happened when you go on to figure out the later lines. It would be clever to keep track of the contents of each String and ArrayList as you go.

    String s = "ABCDE".substring(3);

    ArrayList a = new ArrayList();


    a.add(0, new Integer(a.size());

    s += a.get(0) + "14";

    s = s.substring(s.indexOf("1"));

    int i = Integer.parseInt(s);

    a.set(i, "Bob");


    a.add(2, "Fred");

    i = ((Integer)a.get(0)).intValue();

    i = ((Integer)a.get(i)).intValue();


    s = "T" + s + i;

    i = Integer.parseInt(s);

    What are the values of s, a, and i now?

  2. Read through the method below.

    public int big(String string) {
       int big = 0;
       while(string.substring(",") != -1) {
          int i = string.indexOf(",");
          int n = Integer.parseInt(string.substring(0, i));
          string = string.substring(i + 1);
          if(n > big) {
             big = n;
       return big;

    a) What is the purpose of this method?

    b) Describe what the string must look like.

  3. Read through the class below.

     * An Timecard represents a time period that a person has worked.
     * We need to keep track of this to figure out what to pay them.

    public class Timecard {
       private int hours;
       private String name;
       private String getName() {
          return name;
       private int getHours() {
          return hours;

    Write a constructor for Timecard that takes one parameter, a String. This String will contain both the name and the hours in a format like this:


    So, I should be able to do this:

    Timecard c = new TimeCard("Russell:12");
    c.getName() -> "Russell" (String)
    c.getHours() -> 12 (int)

    To repeat: your code should break up the String at the colon, storing the first part in the String variable name and the second part, as an int, in the variable hours.

  4. Suppose that I want to read one line at a time out of a BufferedReader and use each line to create a Timecard, using the constructor that you made in the previous problem. I will read until I reach the end of the file, and every line is a valid string to make a Timecard out of.

    ---------- start of file ----------
    ----------- end of file -----------

    Fill in the contents of the readFile() method below. It should return an ArrayList of Timecard objects. So, for example, if file is the file above, I should be able to do this:

    ((TimeCard)readFile(file).get(2)).getHours() -> 8

    So, to sum it all up, your method should create the ArrayList, then read up to the end of the file. With each line of the file, it shoud make a Timecard out of that line, and add that Timecard to the list. Then, it should return the list.

    public ArrayList readFile() {

  5. Write a method that will read through an ArrayList of Timecard objects, and count how many total hours one particular person worked. In other words, you will keep a count of hours, and for each Timecard in the list, if its name is equal to the name you are given, you will add the hours on that timecard to the count. Then, you will return that count.

    public int countHours(ArrayList list, string name) {