Find the Error 1. Find the error in the following class: public class MyClass {
ID: 3559055 • Letter: F
Question
Find the Error
1. Find the error in the following class:
public class MyClass
{
private int x;
private double y;
public void MyClass(int a, double b)
{
x = a;
y = b;
}
}
2. Assume that the following method is a member of a class. Find the error.
public void total(int value 1, value2, value3)
{
return value1 + value2 + value3;
}
3. The following statement attempts to create a rectangle object. Find the Error.
Rectangle box = new Rectangle;
4. Find the error in the following class.
public class TwoValues
{
private int x, y;
public TwoValues( )
{
x = 0;
}
public TwoValues( )
{
x = 0;
y = 0;
}
}
5. Find the error in the following class:
public class FindTheError
{
public int square(int number)
{
return number * number;
}
public double square(int number)
{
return number * number;
}
}
Explanation / Answer
1. Find the error in the following class:
public class MyClass
{
private int x;
private double y;
public void MyClass(int a, double b) // constructor dont have return type remove void here.
{
x = a;
y = b;
}
}
// CORRECT SYNTAX IS public MyClass(int a, double b)
2. Assume that the following method is a member of a class. Find the error.
public void total(int value 1, value2, value3) // it should return int not void .and arguments need data type.
{
return value1 + value2 + value3;
}
// CORRECT SYNTAX IS public int total(int value 1, int value2, int value3)
3. The following statement attempts to create a rectangle object. Find the Error.
Rectangle box = new Rectangle; // braces missising.
// CORRECT SYNTAX IS Rectangle box = new Rectangle();
4. Find the error in the following class.
public class TwoValues
{
private int x, y;
public TwoValues( ) // there should be only one no argument constructor in a class....
{
x = 0;
}
public TwoValues( ) // so this constrcutor is invalid.
{
x = 0;
y = 0;
}
}
5. Find the error in the following class:
public class FindTheError
{
public int square(int number) // overloading should be based on argument data type not on return data type.
{
return number * number;
}
public double square(int number) // so this method is invalid.
{
return number * number;
}
}
//we cant have two methods with same signature i.e same arguments type with return type different in same class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.