Tuesday, July 28, 2009

Java help !?

hi guys i am new in java and i want to create class return two thing for example the sum and pro


here the program but i don't know what is wrong :(


=============================


public class SumPro{


public static void main(String[]args){





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


}


static int sumpro (int a , int b , int c){


int sum = a + b + c;


int pro = a * b * c;


OrderedPair po = new OrderedPair(po.getFirst(), po.getSecond());


return po;


}


}





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 help


mode

Java help !?
It looks like you are going about your object creation and instantiation improperly. To create an object, you need to first declare it, like so





SumPro myTest





...then instantiate it like so





myTest = new SumPro(property1, property2)





...where the properties are actual values needed to construct the object. You can also declare and instantiate the object in one statement like so





SumPro myTest = new SumPro()





Now, you shouldn't call methods at the same time you create the object, so you can't say





OrderedPair po = new OrderedPair(po.getFirst(), po.getSecond());





...in one statement. Also, you shouldn't put method calls in the property list for the object, but actual values. So your code could look like this:





OrderedPair po = new OrderedPair(8, 12);





...then po.getFirst() would return 8.





Hope this helps. If you need further assistance, feel free to email me : )


No comments:

Post a Comment