SOMETHING IS WRONG WITH MY CODE, IT GETS COMPILED BUT ONLY PROBLEM IS WHEN I INPUT THE NUMBERS THE OUTPUT COMES AS "Area@9664a1" !

// Implement a class Area which has three attributes length, breadth and Area. use attributes constructor to initilize length and breath. implement a method calcArea to calculate Area of Rectangle main body should instantiate object, callthe calcArea method and display the returned value.
class Area
{
    double length;
    double breadth;
    double area;
    Area(double l, double b)
    {
    length=l;
    breadth=b;
    }
    void calcArea()
    {
    area=length*breadth;
    }
}

class AreaTest
{
    public static void main(String args[])
    {
    double length=Double.parseDouble(args[0]);
    double breadth=Double.parseDouble(args[1]);
    Area a=new Area(length, breadth);
    a.calcArea();
    System.out.println(a);
    }
}

The caps are unnecessary, don't use them.

Secondly, there is nothing wrong with the code, it works perfectly, what you have is a logic error. The problem is in your line

System.out.println(a);

You need to either override Area's toString() method, which it inherits from Object, or you need to right your own print method for Area.

After your write your new method for Area your pintln statement should look like this

System.out.println(a.printMethod());

Overwrite method

public String toString() {...

inside class Area.

Raik.48,
welcome to the forum. Please next time use code tags when you post code.

When you make a call to calcArea() method the area is calculated as it says in the method. When you call println() method you print the location on memory of object a. This is all your programm does.

Just think for a while what the a object means... the a object has a length property, a breath property and an area property. Additionally the a object knows how to calculate its area property.

I'm guessing you are trying to print the area property of the a object. To do so you may:
a. make a call to println() method inside the calcArea() method passing area as the argument as in System.out.println(area); , or

b. make a call to println() method as you do in the main() method passing a.area as the argument (not the a object) as in System.out.println(a.area); , or

c. change the return type of calcArea() method to double; assign the returned value to a variable in main and print this variable

Good luck, please next time use code tags and mark the thread as solved if you found the answer.

Of the various options proposed so far, the best to start with is quuba's to override the inherited toString() method in your class. As well as allowing you to simply say System.out.println(a); and get the expected rseult, this will also work if, for example, you want to display the value in a GUI.

toString() is a method defined in Object, and which all objects therefore inherit. Many methods in the Java API call toString, eg prrintln(...) The inherited method just returns a String containing the class name and the object reference - not very useful. It's a recommended practice to supply your own public toString() method for any class you write that returns a more informative Sttring - it's up to you to decide what info you want to see for your class. You'll see that the majority of the classes in the Java API do exactly that.

Hi!

Thanks for the help.

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.