public class Calculate 
{
    Calculate()
    {
    int x,y,sum, multiply,devide;
    x=12;
    y=5;
    sum=x+y;
    multiply=x*y;
    devide=x/y;
    }
    public static void main(String[] args)
    {
        System.out.println("the sum of two numbers:" + sum);
        System.out.println("the multiple of two numbers:" + multiply);
        System.out.println("the devition of two numbers:" + divide);
    }
}

Recommended Answers

All 8 Replies

What is your problem? It does not help us if you just put the heading "HELP" and a bunch of code. Let us know what your problem is and we will try to help out.

Your class format is incorrect. When you create a class with main() method and you want to test the class out, you should have the class constructor. Then you can implement other methods. Before you start coding, this youtube video (I searched on Google and it looks quite OK) may help you a bit to understand the syntax of the language.

Why you have define all three variable in constructor,you can also define it in main method also.There is actually no need of writing all this thing into constructor you can write it in main method also.

or

You can write those print statement in constructor also and call constructor from main method as follow:

public class Calculate 
{

Calculate()
    {
    int x,y,sum, multiply,devide;    
    x=12;
    y=5;
    sum=x+y;
    multiply=x*y;
    devide=x/y;
System.out.println("the sum of two numbers:" + sum);
        System.out.println("the multiple of two numbers:" + multiply);
        System.out.println("the devition of two numbers:" + devide);
    }
    public static void main(String[] args)
    {
    Calculate c1=new Calculate();

    }
}

now think what you want to do...

@jalpesh, I'm not sure whether the compiler will allow line 4 to pass. It may throw illegal starting statement or something like that...

@Taywin:

I have executed this source code, it doesn't throw any exception and working perfectly.
i have created constructor with same name as class name, and when you create object of this class inside main method as line - 18, constructor is called(Executed).

Thank you.

public class Cal
{
Cal()
    {
    int x,y,sum, multiply,devide;    
    x=12;
    y=5;
    sum=x+y;
    multiply=x*y;
    devide=x/y;
        System.out.println("the sum of two numbers:" + sum);
        System.out.println("the multiple of two numbers:" + multiply);
        System.out.println("the devition of two numbers:" + devide);
    }
    public static void main(String[] args)
    {
    Cal c1;
        c1 = new Cal();
    }
}

//Thanks

@jalpesh, OK. Thanks for clarify and I am sorry for the wrong information. I didn't realise that Java would give public modifier to the constructor by default if there is none. Anyway for beginners, one should explicitly give a modifier to the class constructor. In the future, the one would get an idea when sees other modifiers besides public (protected and private) for constructor.

I didn't realise that Java would give public modifier to the constructor by default if there is none

You were right not top realise that, because it's not true!

Java Language Specification section 8.8.4:

If no access modifier is specified for the constructor of a normal class, the constructor has default access.

Default access means it can be accessed from any code inside the same package. That's not the same as public (can be accessed from outside the package).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.