What is wrong with the following program and how should it be fixed?
1 public class MyClassD {
2 public static void main (String args []) {
3 MyClassC m = new MyClassC (23);
4 } // end main
5 } // end class MyClassD

Respuesta :

Answer:

Their is no MyClassC class is found in the code.

The correct code for the given question isto create a MyClassC  class with parameterized constructor.

The code is given below

public class MyClassC

{

int p;

public MyClassC(int v) // hold the integer value

{

p=v;

}

}// end class MyClassc

Explanation:

According to the code as given in the question in the main method it will call parametrized constructor of MyClassC  but  their is no such kind of class be there so we will create a class MyClassC and also create a parametrized constructor with integer parameter.

The final correct program is given below.

public class MyClassC // class

{

int p;

public MyClassC(int v)

{

p=v;

}

}// end class MyClassC

public class MyClassD

{

public static void main (String args []) // main method

{

MyClassC m = new MyClassC (23);  // call the parametrized constructor of class c

} // end main

} // end class MyClassD

Q&A Education