01: Variables and Scope

Submit this homework using Gradescope. Do not email us your homework or bring a paper copy to class: we will not accept it.

Logging into Gradescope

Go to the Gradescope web site (https://gradescope.com/) and sign up using the email address that you have listed in SPIRE. When you sign up in Gradescope, you’ll need a course entry code. Ours is on the course Moodle page.

Do not wait until the due date to start this process. If the Gradescope site tells you it does not recognize your email address, you should contact the course staff immediately to create an account for you. “I added the course late and didn’t know I needed to ask for a Gradescope account”, “I forgot my Gradescope password” and the like will not be accepted as an excuse for not submitting this or any other assignment on time.

The homework

  1. (1 point each) Consider a linear function f(x) = mx + b.

    a. Write a method to compute the value of such a function, given m, x, and b. The method should have the signature double compute(double m, double x, double b). Feel free to write this method in an IDE.

    b. How much space (in terms of the size of double values) does the JVM lay out on the stack when this method is invoked? In other words, space for how many doubles must be set aside when this method is invoked?

  2. (2 points) Read the following methods, and state why each won’t compile. Be specific!

    Try to do this by eye – that is, without actually running them through a Java compiler.

int foo(int x) {
  if (x < 0) {
    int y = -x;
  }
  return y;
}
int bar(String x) {
  return x + 2;
}