CS 112
Spring 2018

Old version

This is the CS 112 site as it appeared on May 11, 2018.

Problem Set 1 FAQ

If you don’t see your question here, post it on Piazza or come to office hours! See the links in the navigation bar for both of those options.

Part I

  1. For problem 1, part 2, I’m getting an error when I try to enter my method in the Interactions Pane. It says “Modifier static is not allowed here”. What am I doing wrong?

    This appears to be a limitation of the Interactions Pane. To get around it, you should omit the words “public static” when entering the method in the Interactions Pane. Don’t forget that you can also test your method by adding it to a class, saving that class, compiling it, and making method calls using the class name. See Lab 1 for an example of how to do this.

  2. For problem 1, part 2, I’m getting a different error when I try to test my method in Dr Java. It says “Bad return type: given double, expected int”.

    This probably means that the return type that you specified in the method header is incorrect. Make sure that it reflects the type of the value that you are attempting to return.

  3. For problem 4, part 2, I’m trying to test out possible expressions using the Interactions Pane in DrJava. However, when I enter an expression, no result is produced. What am I doing wrong?

    It’s possible that you’re putting a semi-colon at the end of your expressions, which prevents them from being evaluated. Try removing the semi-colon and the expression should be evaluated.

  4. For problem 4, part 2, one of my expressions is giving me the error No method in char has name... Why is this happening?

    When you chain method calls together, you need to make sure that you do so in the correct order.

    For example, let’s say that we had the following String:

    > String s = "howdy!";
    

    If we wanted an expression to produce an uppercase 'H', the following expression would not work:

    > s.charAt(0).toUpperCase()
    

    That’s because the method calls are evaluated from left to right, and the result of the first call (the one to charAt()) replaces that call in the expression to give:

    'h'.toUpperCase()
    

    This doesn’t work, because the return value of charAt() (the 'h') is a char, not a String, and char values do not have methods inside them, so this produces the error you are seeing.

    Swapping the order of the calls fixes things:

    > s.toUpperCase().charAt(0)
    'H'
    

    This works, because the left-to-right evaluation now looks like this:

        s.toUpperCase().charAt(0)
    ==> "HOWDY!".charAt(0)
    ==> 'H'
    

Part II

  1. In one of my methods for Part II, I’m getting a StringIndexOutOfBoundsException. What am I doing wrong?

    Don’t forget that the characters in a string have index values that go from 0 to length - 1. The exception means that your code is using an index from outside that range.

  2. Can the methods that we write for Part II read values from the user?

    No! You should write your methods so that the necessary values are passed in as parameters.

  3. For problems 5 and 6, should our classes have a main method? If not, how do we run it and test it?

    We are not requiring you to add a main method to these classes. Without one, you won’t be able to run it. Rather, these classes will simply serve as collections of static methods.

    To test the individual methods as you write them, you can make method calls from the Interactions Pane. See the info in part 0 of problem 5 for a reminder about how to do this.

    Once all of the methods for a given problem have been written, you can test them using the separate test programs that we have provided. See the assignment for more detail. Those test programs do have a main method, so you will be able to run them.

  4. For the middleChar method in problem 5, I’m trying to use the substring method to pull out the middle character, but I’m getting an error message that says something like “incompatible types: found: java.lang.String, required: char”. What am I doing wrong?

    The middleChar method is supposed to return a single character (i.e., a char), whereas the substring method produces a String. Instead of using substring, use a different method that allows you to extract a single character from a string. (Hint: We covered the method in lecture!)

  5. For middleChar, you mention that we should be able to perform the same computation regardless of whether the string has an odd or even number of characters. Is it okay if we use conditional execution to handle the two cases separately?

    Yes!

  6. In the material about String objects in Problem 4, you mention that we need to use an equals method to compare two String objects to see if they are equivalent. What about comparisons of char values?

    char values are primitives, so they are compared using the == operator.