Spring Semester 2002 |
Please write your name or username here: ___________________________
This exam is open-book, open-notes, but individual work. It lasts 120 minutes (5-7pm).
For each of the following questions please write your answers in the spaces provided.
1. Implement a class Square that has methods
getArea() and getPerimeter(). In
the constructor supply the width of the square.
Here's a possible answer:
2. Explain what each of the following two program segments compute:
class Square {
private double width;
Square(double width) {
this.width = width;
}
double getArea() {
return this.width *
this.width;
}
double getPerimeter() {
return 4 * this.width;
}
public static void main(String[] args) {
Square a = new Square(10);
double area = a.getArea();
double perim = a.getPerimeter();
System.out.println(a.width + "\n" + area + "\n" + perim);
}
}
andx = 2; y = x + x;
s = "2"; t = s + s;
The answer is:
4.
Strings,
producing 22.
3. How do you get the first character of a string? The last character? How do you remove the first character? The last character?
Assume
You can't remove characters from a
All you can do is create copies of the relevant parts, like so:
then we have:
String a;
a.charAt(0) for the first character, and
a.charAt(a.length() - 1) for the last
String.
a.substring(1) (a copy of the
String without the first character)
a.substring(0, a.length() - 1)
(copy leaves out last char)
4. What is a purpose of a graphics context and where in an applet is it used?
paint method.
5. Explain the difference between
ands = 0; if (x > 0) s++; if (y > 0) s++;
s = 0; if (x > 0) s++; else if (y > 0) s++;
if statement is always executed. x
and y positive the first one sets s to
2 while the second sets it to 1.
6. Explain the difference between the == operator and
the equals method when comparing strings.
7. Rewrite the following frilled.cs.indiana.edu%cat Example.java
class Example {
public static void main(String[] args) {
String a = "one",
b = args[0];
System.out.println("a = " + a + " and b = " + b);
System.out.println("a.equals(b) is: " + a.equals(b));
System.out.println("a == b is: " + (a == b));
}
}
frilled.cs.indiana.edu%javac Example.java
frilled.cs.indiana.edu%java Example one
a = one and b = one
a.equals(b) is: true
a == b is: false
frilled.cs.indiana.edu%for loop
using a while loop.
int s = 0; for (int i = 1; i <= 10; i++) s = s + i;
int s = 0, i = 1;
while (i <= 10) {
s = s + i;
i++;
}
8. You compile and run the following program. What's its output?
class One {
public static void main(String[] args) {
double a = 1.0, b = 6.0;
System.out.println("(" + a + ", " + b + ")");
One.swap(a, b);
System.out.println("(" + a + ", " + b + ")");
}
public static void swap(double a, double b) {
double temp = a;
a = b;
b = temp;
}
}
Please briefly explain the answer.
Since parameters are passed by value the swap is local to the method.
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
double a = 1.0, b = 6.0;
System.out.println("(" + a + ", " + b + ")");
One.swap(a, b);
System.out.println("(" + a + ", " + b + ")");
}
public static void swap(double a, double b) {
double temp = a;
a = b;
b = temp;
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
(1.0, 6.0)
(1.0, 6.0)
frilled.cs.indiana.edu%
9. Here's a program that is intended to compute the average of two numbers:
public class Two {
public static void main(String[] args) {
int x1 = 3,
x2 = 4;
System.out.println((x1 + x2) / 2);
}
}
What's the output if you run it, and what needs to be done to obtain the
correct result.
3.
3.5 one needs to cast at least one
of the three values in the formula to a double.
10. You are writing an applet like the one you did for Homework Five (the two eyes are watching the mouse). What steps do you need to take to make your program respond to mouse movement as it did then? Be succinct (i.e., short, complete, and precise) in your answer. There are three steps.
MouseMotionListener
MouseMotionListener
toString() method, and what is special
about it?
One can define such a method to
obtain custom String representations of objects.
Evaluating an object reference in a String context
amounts to an invocation of this method on the object reference.
12. You compile and run the following program. What is its output. Explain.
class Horse {
int fun() { return -1; }
}
class Unicorn extends Horse {
int fun() { return 1; }
}
class Three {
public static void main(String[] args) {
Horse a = new Horse();
Unicorn b = new Unicorn();
Horse c = new Unicorn();
System.out.println(a.fun() +
b.fun() +
c.fun());
}
}
The answer is
It is the type of the object and not that of the reference that matters.
-1 + 1 + 1 which amounts to 1.
13. Write a method that takes in an array of ints
and returns the largest element in the array.
Take a look at the program presented with the next problem.
14. Write a method that takes in an array of ints
and sorts it in ascending order.
Here's an answer to both
13 and 14.
15. What's the difference between a class Methods {
public static void main(String[] args) {
int[] a = {1, 6, 2, 5, 3, 4};
show(a);
System.out.println(largest(a));
sort(a);
show(a);
}
public static int largest(int[] a) {
int max = a[0];
for (int i = 1; i < a.length; i++)
if (max < a[i])
max = a[i];
return max;
}
public static void sort(int[] a) {
for (int i = 0; i < a.length - 1; i++)
for (int j = i; j < a.length; j++)
if (a[i] > a[j]) {
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
public static void show(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}Vector and an array of
Objects? What are the similarities between the two? What is the
difference between a Vector and any array? What similarities do
you see between a Vector and an array?
A
Vector
By the same token: Objects, except that
Objects cannot grow and shrink automatically.
16. What's the difference between a Vectors have automatic memory management, arrays do not. Vectors and arrays use integer indices to refer to their elements. Vector and a
Hashtable? What are the similarities, if any?
17. What's the difference between an instance variable and
a Objects, to refer to their elements.
static one? Can a static method
invoke an instance method (directly or indirectly)? How?
A static variable is global to all (if any) instances
of that class. An instance variable is global to only the instance methods (if any) of (the
blueprint) of that class. No matter how many instances are created there will be only one copy
of any static variable that the class defines. Instance variables, however, will
be located inside instances and therefore there will be as many instance variables as instances
(if any). No, a static method cannot call an instance method directly,
it needs to use an object reference for that. See for example the following:
frilled.cs.indiana.edu%cat Test.java
class Test {
public static void main(String[] args) {
fun();
}
void fun() {
System.out.println("Hello, world!");
}
}
frilled.cs.indiana.edu%javac Test.java
Test.java:3: non-static method fun() cannot be referenced from a static context
fun();
^
1 error
frilled.cs.indiana.edu%
18. What's recursion? Give an example of a recursive method.
Recursion is the ability to describe an operation in terms of itself (with a base
case).
For example:
To calculate the sum of the first int sum(int n) {
if (n == 1) return 1;
else return n + sum(n - 1);
}n integers you add n to the
sum of the first n -1 integers. The sum of the first 1
integer(s) is 1 (assuming that we are taking into account only strictly positive numbers).