Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 counter
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
starlight




PostPosted: Fri Feb 03, 2006 7:39 pm   Post subject: counter

method returns the value of the digit that is position places from the right in the decimal representation of n.

e.g

Enter Number: 8574

What digit do you want? 2

The digit is 5

This is what i got so far . dont' really understand why it is not working.

code:

class position1 {
   public static void main(String[] args) {
     int n, position;
     System.out.println("number");
     n=In.getInt();
     System.out.println ("position");
     position=In.getInt();
    digit(n,position);
}
   public static int digit(int n, int position) {
     if (position==2) {
     for (int i=1; i<=position; i++){
       
       position=n%10;
   
         n=n/10;
         
   }
     }
return position;
   
   }
   
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Feb 03, 2006 7:45 pm   Post subject: (No subject)

How about properly formatting your code, and using proper naming (class names should be capitalized)?
starlight




PostPosted: Fri Feb 03, 2006 8:00 pm   Post subject: Re: counter

starlight wrote:
method returns the value of the digit that is position places from the right in the decimal representation of n.

e.g

Enter Number: 8574

What digit do you want? 2

The digit is 5

This is what i got so far . dont' really understand why it is not working.

code:

class POSITION {
   public static void main(String[] args) {
     int n, position;
     System.out.println("number");
     n=In.getInt();
     System.out.println ("position");
     position=In.getInt();
    digit(n,position);
}
   public static int digit(int n, int position) {
     if (position==2) {
     for (int i=1; i<=position; i++){
       
       position=n%10;
   
         n=n/10;
         
   }
     }
return position;
   
   }
   
}
[Gandalf]




PostPosted: Fri Feb 03, 2006 9:00 pm   Post subject: (No subject)

No... Something like this would do:
Java:
class Position1
{
    public static void main(String[] args)
    {
        int n, position;
        System.out.println("number");
        n = In.getInt();
        System.out.println("position");
        position = In.getInt();
        digit(n, position);
    }
   
    public static int digit(int n, int position)
    {
        if (position == 2)
        {
            for (int i = 1; i <= position; i++)
            {
                position = n;    %10;   you commented out the semicolon here
                n = n / 10;
            }
        }
        return position;
    }   
}

Now, what is In?
wtd




PostPosted: Fri Feb 03, 2006 9:12 pm   Post subject: (No subject)

[Gandalf] wrote:
Now, what is In?


Something specific to the HSA classes, I imagine.

starlight, step through a call of your method.

digit(8574, 2)
position is 2, so the conditional is met
loop with i = 1
position = 4
n = 857
loop with i = 2
position = 7
n = 85
loop with i = 3
position = 5
n = 8
loop with i = 4
position = 8
n = 0
loop with i = 5
position = 0
n = 0
loop with i = 6, but 6 > 0, so end loop
return 0

That doesn't seem quite right, does it? Smile
[Gandalf]




PostPosted: Fri Feb 03, 2006 9:37 pm   Post subject: (No subject)

Quote:
position = n; %10; you commented out the semicolon here

Ooops, no you didn't. % is the modulus operator in Java... Damn - too much Turing Sad.
starlight




PostPosted: Fri Feb 03, 2006 9:56 pm   Post subject: (No subject)

i don't get it. isn't Gandalf's solution exactly the same as the one i was doing? but it keep returning only the last value. it only run the loop once but i don't understand why.
wtd




PostPosted: Fri Feb 03, 2006 9:59 pm   Post subject: (No subject)

Gandalf changed how the code works slightly.

Think about the code. Think about what's going to happen when you call the method. What happens to the variables involved. You can trace out that behavior on paper.
Sponsor
Sponsor
Sponsor
sponsor
mercuryy




PostPosted: Sat Feb 04, 2006 1:46 pm   Post subject: (No subject)

I did this program long time ago. i use 40 something lines and my teacher said there's a easy way to do it. I think u should use exponent or something. PM me if anyone knows the short way to do it.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: