Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

9, Find the Error 1. Find the error in the following class: public class MyClass

ID: 3540931 • Letter: 9

Question

9, Find the Error

1. Find the error in the following class:


   public class MyClass

   {


      private int x;

      private double y;

      public int MyClass()


      {

         x = 0;

         y = 0.0;

      }


   }



2, Assume that the following method is a member of a class. Find the error.


   public int total(double value; double value2)

   {


      return (value + value2);

   }



3, The following statement attempts to create a rectangle object.Find the    error.


   Recetangle box = recetangle() new;


4, Find the error in the following class:


   public class MyClass


   {


       private int x, y;

       public int MyClass()

       {


          x = 0;

       }

       public void MyClass()

       {


          x = 0;

          y = 0;


       }


   }




5, Find the error in the following class:


   public class FindTheError


   {


      public square(int number)

   

      {


         return number * number;

      }

     

      public square(double number)


      {

         return number * number;


      }


   }

Explanation / Answer

1. Find the error in the following class:


public class MyClass

{


private int x;

private double y;

public int MyClass()


{

x = 0;

y = 0.0;

}


}



Error : Constructor in java does not have a return type.

So in place of public int MyClass() it ll be public MyClass()


After correction




public class MyClass

{


private int x;

private double y;

public int MyClass()


{

x = 0;

y = 0.0;

}


}

}








2, Assume that the following method is a member of a class. Find the error.


public int total(double value; double value2)

{


return (value + value2);

}


Error : value and value2 are of return type double. but the return type of the function total is integer type.

This will throw compile time error. So It is to be typecasted into integer type.

return (int) (value + value2);


After correction



public int total(double value, double value2)

{

return (int) (value + value2);


}











3, The following statement attempts to create a rectangle object.Find the error.


Recetangle box = recetangle() new;




An object will be made in this way


Recetangle box = new recetangle();



4, Find the error in the following class:


public class MyClass


{


private int x, y;

public int MyClass()

{


x = 0;

}

public void MyClass()

{


x = 0;

y = 0;


}


}



Error : Already mentioned in the above example, that constructor in java has no return type.

Also we are making duplicate method MyClass() which is not possible. Though the return type is different.

Until we add some parameter, code can not be made executable.




5, Find the error in the following class:


public class FindTheError


{


public square(int number)

{


return number * number;

}

public square(double number)


{

return number * number;


}


}



Here, in both the methods, we do not have a return type but we are returning an integer type value in first method and a double type value in second method.

So, after adding the return type. The code ll look like this



public class FindTheError


{


public int square(int number)

{


return number * number;

}

public double square(double number)


{

return number * number;


}


}