4. (10 pts) What is wrong with the following program and how should it be fixed?
ID: 3731484 • Letter: 4
Question
4. (10 pts) What is wrong with the following program and how should it be fixed? 1 public class MyClassE 2 public static void main (String args ) 3 MyClassF m = new MyclassF (23); 4 //end main 5 //end class MyClassE 7 class MyClassF 8 int v = 12; 10 private MyClassF (int pV) 11 v=pv; 12 13 14 /end class MyClassF 5. (10 pts) Given all the problems identified in problems 1 through 4, explain in detail why the following code works, ie, compiles without errors or warnings. 1 public class MyClassG 2 public static void main (String args ) 3 MyClassH m = new MyClass H (23, true); 4 )//end main 5 // end class MyClassG 7 class MyClassH ( 8 int v = 12; 10 public MyClassH (int x, boolean b) 11 this (x); 12 13 14 private MyClassH (int pv) 15 v=pv; 16 17 18 // end class MyClassHExplanation / Answer
4)
In the line 10 the Parameterized constructor scope must be public .So that we can create the object for that Class in the main() method.If it is private we can able to create the object for that class.
public class MyClassE {
public static void main(String[] args) {
MyClassF m = new MyClassF(23);
}
}
class MyClassF {
int v = 12;
MyClassF(int pV) {
v = pV;
}
}
__________________
5)
Line 3:Creating an instance for the class MyClassH using the parameterized Constructor with 2 Arguments
Line 10:This will be called while creating an Object for the class.
Line 11: this(x); This statement will call the parameterized constructor with 1- argument
Line14: This will be called to initialize the instance variable
_____________________
public class MyClassG{
public static void main(String[] args) {
//Creating an instance for the class MyClassH using the parameterized Constructor with 2 Arguments
MyClassH m = new MyClassH(23,true);
}
}
class MyClassH {
int v = 12;
//Parameterized constructor with 2 Arguments
public MyClassH(int x,boolean b) {
this(x);//This statement will call the parameterized constructor with one argument
}
//Parameterized constructor with 1-Argument
private MyClassH(int pV)
{
v=pV;
}
}
___________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.