Discussion questions for week 9

advertisement
Questions for Discussion (Week 9)
Question 1
Given the following 2 classes:
public class MyClass {
private double n, n2;
public MyClass(double n) {
this(n, n*n);
}
public MyClass(int n, int offset) {
this(n + offset);
}
private MyClass(double n, double n2) {
this.n = n;
this.n2 = n2;
}
}
public class MyClassDriver {
public static void main(String[] args) {
// create a MyClass object here
. . .
}
}
Which of the following lines are valid when used to replace the comment above? Why?
a.
b.
c.
d.
MyClass
MyClass
MyClass
MyClass
a
b
c
d
=
=
=
=
new
new
new
new
MyClass(10, 3);
MyClass(10);
MyClass(10, 1.5);
MyClass();
- 1 of 5 -
Question 2
Given the following class:
public class Value {
private int myValue;
public Value(int x)
myValue = x;
}
{
public int getValue() {
return myValue;
}
public void setValue(int x) {
myValue = x;
}
public void copyValueA(Value u, Value v) {
u = new Value(v.getValue());
}
public void copyValueB(Value u, Value v) {
u.myValue = v.myValue;
}
}
What is the output of the following code fragment?
Value a = new Value(10);
Value b = a;
b.setValue(11);
System.out.println(a.getValue() + " " + b.getValue());
b = new Value(15);
a.setValue(9);
System.out.println(a.getValue() + " " + b.getValue());
b.copyValueA(a, b);
System.out.println(a.getValue() + " " + b.getValue());
a.copyValueB(b, a);
System.out.println(a.getValue() + " " + b.getValue());
- 2 of 5 -
Question 3
Consider the following class.
public class SomeClass {
private int num;
public int change() {
return num++;
}
public void change(int x) {
num = ++x;
}
public void change(SomeClass x) {
num = x.change();
x.change(num);
}
}
What is the output of the following code?
int x1;
SomeClass x2 = new SomeClass();
SomeClass x3 = new SomeClass();
x1 = 10;
x2.change(x1);
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2.change());
x1 = x2.change();
x2.change(x1);
x3.change(x2);
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2.change());
System.out.println("x3 = " + x3.change());
- 3 of 5 -
Question 4
Suppose we are given the following class.
public class Robot {
private String name;
private String serialNumber;
private int cost;
public Robot(String name, String serialNo, int cost) {
this.name = name;
this.serialNumber = serialNumber;
this.cost = cost;
}
public void changeName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public Robot getRobot() {
return this;
}
}
a. Identify the method-calls below that are invalid. Do you know the reason why they are
invalid?
b. Some of the valid statements include redundant call. Identify them and rewrite the
statements.
c. Remove the invalid method calls. What will be the values returned from a.getName() and
b.getName()?
public static void main (String [] args) {
Robot a = new Robot("Alpha", "AL1001-A", 10000);
Robot b = new Robot("Beta", "BE1001-B", 2500);
a.getRobot().changeName("Delta");
b.changeName("Delta").getName();
a = b.getRobot().changeName("Epsilon");
a.getName().substring(0,5).indexOf("Alp");
b.changeName(a.getRobot().getName());
}
- 4 of 5 -
Question 5
The Point class is referred to many times in past years’ paper, so let’s explore it. Look up the API
page http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Point.html for the Point class.
The Point class has two public data members x and y which are both integers. It has three
overloaded constructors, and a number of methods.
To use the Point class, you must import the package java.awt.* in your program.
Study the code FurtherPoint.java that asks the user for data of two distinct points, and then
print the point that is further from the origin. (The origin is at (0, 0).)
Note how the program prints a Point object.
The method isFurther(Point, Point) is a stub. Complete it.
Further exploration: Can you create a class MyTriangle that consists of three data members pt1,
pt2 and pt3 which are Point objects representing the three vertices of a triangle, and write
suitable constructors, accessors and mutators for MyTriangle? Can you write a driver program
to test out the newly created MyTriangle class?
- 5 of 5 -
Download