Question #152279

. Find the syntax errors in the definition of the following class:
public class BB
{
private int one;
private int two;
public boolean equal()
{
return (one == two);
}
public print()
{
System.out.println(one + " " + two);
}
public BB(int a, int b)
{
one = a;
two = b;
}
}

Expert's answer

The error is that the return value is not specified in the "Print" method signature. In this case, the method does not return anything, so in the method signature you need to write "void".

The correct code is below:


public class BB

{

    private int one;

    private int two;

    public boolean equal()

    {

        return (one == two);

    }

    public void print() // syntax error was HERE!

    {

        System.out.println(one + " " + two);

    }

    public BB(int a, int b)

    {

        one = a;

        two = b;

    }
    //driver to show how program works (just for example!!!) 
    public static void main(String[] args) {
        BB bb = new BB(1, 2);
        System.out.print("method \"print\": ");
        bb.print();
        System.out.println("\n method \"equal\": " + bb.equal());
    }

}


OUTPUT:


LATEST TUTORIALS
APPROVED BY CLIENTS