(5 points) You have two classes named DummyClass and DummyClassRunner. The codes
ID: 3872854 • Letter: #
Question
(5 points) You have two classes named DummyClass and DummyClassRunner. The codes for these two classes are provided below.
//DummyClass.java
public class DummyClass {
double x, y;
DummyClass(int xx){
x=xx;
}
DummyClass(int xx, int yy){
x=xx;
y=yy;
}
}
//DummyClassRunner.java
public class DummyClassRunner {
public static void main(String[] args){
DummyClass[] d= new DummyClass[5];
for (int i=0; i<d.length;i++){
d[i]=new DummyClass(i, i);
}
for (int i=d.length; i<2*d.length;i++){
d[i]=new DummyClass(i);
}
for (int i=2*d.length; i<3*d.length;i++){
d[i]=new DummyClass();
}
}
}
Do you expect any compiler error from any of the two java files above? If your answer is “yes”, then (i) Mark the line(s) in the code above that might be causing the error, and (ii) Briefly describe the error. If your answer is “No”, please explain the role of a default constructor in the program.
Explanation / Answer
//DummyClassRunner.java
public class DummyClassRunner {
public static void main(String[] args){
DummyClass[] d= new DummyClass[5];
for (int i=0; i<d.length;i++){
d[i]=new DummyClass(i, i);
}
System.out.println(" Length :"+d.length);
for (int i=d.length; i<2*d.length;i++){
//Error1: Here exception ArrayIndexOutOfBounds arises
// since i value ranges 5 to 9
d[i]=new DummyClass(i);
}
//Error2: Here exception ArrayIndexOutOfBounds arises
// since i value ranges 10 to 14
//Error3: Here parameterised constructor are defined
//but default constructor is not defined. Now implicit
// default constructor will not invoke during creating of object
for (int i=2*d.length; i<3*d.length;i++){
d[i]=new DummyClass(); //calling default constructor
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.