passing value problem

Hello,

this is my code

Hi,

i edited the code :

ReverseTest.java

import java.util.Scanner;

publicclass ReverseTest{

publicstaticint num,newnum,count,countDigit,rightDigit;

publicstaticvoid main(String args[]){

Reverse test =new Reverse();

Scanner input =new Scanner( System.in );

System.out.println("Enter integer : " );

num = input.nextInt();

System.out.println("How many digits does it has : " );

countDigit = input.nextInt();

System.out.println("Integer value : " + num);

System.out.println("");

System.out.println(test.reverseOrder() );

System.out.println("");

System.out.println(test.reverseLargest() );

System.out.println("");

System.out.println(test.reverseSmallest() );

}

}

Reverse.java

import java.util.Scanner;

publicclass Reverse{

publicstaticint num,newnum,counter,countDigit,rightDigit,largest,smallest,number;

Scanner scanner =new Scanner(System.in);

publicint reverseOrder(){

for(int count=1; count <= countDigit; count++){

rightDigit = num % 10;

newnum = newnum * 10 + rightDigit;

num = num / 10;

}

System.out.println("Reversed is : ");

return newnum;

}

publicint reverseLargest(){

if ( number > largest )

largest = number;

System.out.printf("Largest number is : ");

return largest;

}

publicint reverseSmallest(){

if ( number < largest )

smallest = number;

System.out.printf("Smallest number is : ");

return smallest;

}

}

the output is :

- Capture Output -

"C:\jdk\bin\java.exe" ReverseTest

Enter integer :

12345

How many digits does it has :

5

Integer value : 12345

Reversed is :

0

Largest number is : 0

Smallest number is : 0

Terminated with exit code 0

looks like it doesnt pass the value to Reverse class.

how to fix this ?

[3711 byte] By [vearnsa] at [2007-11-27 11:40:29]
# 1

you need to add a mutator and retriever method to the 2nd source file. for example, if you have an integer called a and want to change its value you would do this..

int a = 0;

public void setA(int a) {

this.a = a;

}

And to get the value of a back...

public int getA() {

return a;

}

That will help you pass values between your classes. I learned this trick from "Beginning Java 5 Game Programming", but it works in all kinds of situations.

1cMas5_cowa at 2007-7-29 17:32:08 > top of Java-index,Java Essentials,Java Programming...
# 2

> I learned this trick from "Beginning Java 5 Game

> Programming", but it works in all kinds of situations.

If you call this a trick, you're in for plenty of surprising "tricks".

-Kayaman-a at 2007-7-29 17:32:08 > top of Java-index,Java Essentials,Java Programming...
# 3

I know...

It was a "miswording".

I sometimes type faster than I think...

I know its nothing special, I don't know why I said that....

But if it helps it doesn't really matter.

1cMas5_cowa at 2007-7-29 17:32:08 > top of Java-index,Java Essentials,Java Programming...
# 4

> looks like it doesnt pass the value to Reverse class.

Yes, you do not pass the values of "num" and "countDigit" to the Reverse class. So how it is supposed to know their values ?!?

The best is to pass these values in the constructor of Reverse (in this case you obviously must construct the Reverse object after the user's input of "num" and "countDigit").

@ 1cMas5_cow: your trick is called "Java programming".

java_knighta at 2007-7-29 17:32:08 > top of Java-index,Java Essentials,Java Programming...
# 5

> you need to add a mutator and retriever method

By the way, they're called "setter" and "getter" methods. In this case you would not even need them because the relevant variables are declared as public fields in ReverseTest (not a good programming habit).

java_knighta at 2007-7-29 17:32:08 > top of Java-index,Java Essentials,Java Programming...
# 6

> > you need to add a mutator and retriever method

>

> By the way, they're called "setter" and "getter" methods.

They're also commonly called "mutator" and "accessor" methods.

~

yawmarka at 2007-7-29 17:32:08 > top of Java-index,Java Essentials,Java Programming...
# 7

And what's the "retriever" in all this? A dog? ;-)

java_knighta at 2007-7-29 17:32:08 > top of Java-index,Java Essentials,Java Programming...
# 8

> And what's the "retriever" in all this? A dog? ;-)

I didn't find "retriever" different enough from "accessor" to warrant any confusion.

~

yawmarka at 2007-7-29 17:32:08 > top of Java-index,Java Essentials,Java Programming...
# 9

it's a matter of symmantics.

But this is away from the point of the thread.

1cMas5_cowa at 2007-7-29 17:32:08 > top of Java-index,Java Essentials,Java Programming...