Discussion questions for week 10

advertisement
Questions for Discussion (Week 10)
Question 1
What is the output of the following program? Explain your answer.
public class Q1 {
static Person b = new Person();
public static void main(String[]args) {
Person a = new Person();
a.setName("Fred");
b.setName("Peter");
changeName1(a);
System.out.println(a.getName() + " " + b.getName());
changeName2(a);
System.out.println(a.getName() + " " + b.getName());
}
public static void changeName1(Person a) {
b = new Person();
b.setName("David");
a = b;
}
public static void changeName2(Person a) {
a.setName("John");
}
}
class Person {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
- 1 of 5 -
Question 2
Consider the following program:
public class Q2 {
private int i;
private static int j;
public static void staticMethod() {
i = 100;
j = 1000;
}
public void nonStaticMethod() {
i = 100;
j = 1000;
}
public static void main(String[] args) {
i = 100;
j = 1000;
nonStaticMethod();
staticMethod();
Q2 t = new Q2();
t.staticMethod();
t.nonStaticMethod();
Q2.staticMethod();
Q2.nonStaticMethod();
}
}
(a) Determine which lines of code result in compilation errors.
(b) For each line that results in compilation error, explain why.
(c) Remove all those lines that result in compilation error. For the remaining lines,
wherever appropriate, rewrite the lines using the dot notation (i.e., add ‘this.’ or ‘Q2.’)
- 2 of 5 -
Question 3
In this question, you are required to write a class ArrayUtil and implement some static methods
for the class as described below:

printArray(): which takes in an integer array and prints out all the elements.

sortArray(): which takes in an integer array and sorts the array in increasing order.

copyArray(): which takes in an integer array and creates another array which has same
content as the input array.
Each of the methods specified above is overloaded with two versions: one has just the integer
array parameter, and the other has an additional integer parameter called size. If the size is not
specified, then by default we should use the length of the array for its size. (Therefore, you
should have two methods for printArray(): printArray(int[] arr) and printArray(int[] arr, int size))
Next, create a class called ArrayUtilDriver that asks the user to:

Input the number of elements of an integer array.

Input the values for the array elements.
You should then use the methods you have written in ArrayUtil to:

Create a copy of the input array.

Sort the input array.

And finally print the copy and the sorted array.
A sample run is shown below:
Input number of elements in array: 8
Input elements for array: 8 5 12 3 -2 5 3 10
The input array:
8
5
12
3
-2
5
3
The copy of input array:
8
5
12
3
-2
5
3
The sorted array:
-2
3
3
5
5
8
10
- 3 of 5 -
Question 4
Study the program below.
public class SomeNumber {
private
private
private
private
static boolean initialized = false; // Question e
static int maxNumber; // Question d
static int minNumber; // Question d
int num;
public SomeNumber(int num) {
this.num = num;
checkMax();
checkMin();
initialized = true;
}
private void checkMax() {
if (!initialized)
maxNumber = num;
else if (num > maxNumber)
maxNumber = num;
}
private void checkMin() {
if (!initialized)
minNumber = num;
else if (num < minNumber)
minNumber = num;
}
public static int getMax() {
return maxNumber;
}
public static int getMin() {
return minNumber;
}
public static void main (String[] args) {
int[] arr = {-12, 5, -7, 0, 13, 20, 5, 24};
for (int i = 0; i < arr.length; i++) { // Question b
new SomeNumber(arr[i]);
}
System.out.println(SomeNumber.getMax() + " " +
SomeNumber.getMin()); // Question a
}
}
- 4 of 5 -
(a) What is the output of the program?
(b) What is the result of executing the for-loop?
(c) What are the method checkMax() and checkMin() declared as private?
(d) Why are the variable maxNumber and minNumber declared as private? What are they
initialized to?
(e) In conjunction with part (d), what do you think is the purpose of the boolean variable
initialized?
(f) Give some suggestion of how to remove the boolean variable initialized without affecting
the correctness of the code. Hint: consider class Integer, which is a wrapper class that contains
int value.
- 5 of 5 -
Download