Homework 01: Variables and Scope
Submit this homework on paper at the start of lecture. Future homeworks will submitted using Gradescope.
-
(1 point each) Consider the problem of computing a slope of a line, defined by two points (x1, y1) and (x2, y2).
a. Write a method to compute the slope of a line given two points. The method should have the signature
double slope(double x1, double y1, double x2, double y2)
.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? -
(2 points) Read the following methods, and state why each won’t compile. Try to do this by eye – that is, without actually running them through a Java compiler.
1 2 3 4 5 6 7 8 9 | int foo(int x) {
if (x % 2 == 0) {
int y = x * 2;
}
return y;
}
int bar(String x) {
return x + 2;
}
|