Thursday, July 30, 2009

JAVA,, problem with....?

hi


i have the source code for program that i want it to print for me the answer of sum and multiply 3 numbers in the same time


but when i compile it i get this answer


OrderedPair@9b49e6


any idea


the codes


++++++++++++++++++++++++++++++++++++++...


public class MinMax {


static OrderedPair mm (int a , int b , int c) {


int sum = a + b + c;


int pro = a * b * c;


OrderedPair po = new OrderedPair(sum,pro);


return po;


}


public static void main(String[] args){


System.out.println (mm(1,2,3));


}


}


class OrderedPair{


int first;


int second;


OrderedPair(int f,int s){


first = f;


second = s;


}


int getFirst(){


return first;


}


int getSecond(){


return second;


}


void setFirst(int f){


first = f;


}


void setSecond(int s){


second =s;


}


}


thx for hlp

JAVA,, problem with....?
Ok, I looked thru your code, and I found no syntax errors whatsoever, however I did see a funny looking statement, so I decided to compile it myself and see what happened. This resulted in javac not giving any errors, however:





$ java MinMax


OrderedPair@3e25a5





And thus, I came to realization of what happened.





System.out.println() doesn't know how to handle OrderedPair objects, so what you are seeing there is the return type and the location of the object (that was the funny looking statement I mentioned early). You need to give System.out.println() something it can understand. See attached image for details.
Reply:The message is the class name of the object and the address of that object. If you want to print out a meaningful message, you should override the toString method e.g





public String toString()


{


return x+" "+y....;


}


No comments:

Post a Comment