Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-12-2008, 06:06 AM
Member
 
Join Date: Oct 2008
Posts: 2
yadster101 is on a distinguished road
New to CompSci and Java
Hi i just started my CompSci class and for hw my teacher gave us the question:

"Write an expression that, given a positive integer n, computes a new integer
in which the units and tens digits have swapped places. For example, if
n = 123, the result should be 132; if n = 3, the tens digit is zero and the result should be 30."

Im not really sure how i would do this but my guess is that it is more of a math question than a java question. If you have any ideas that might help please let me know.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-12-2008, 06:52 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 883
Fubarable is on a distinguished road
There are two ways to approach this question.
A) Convert the int into a String and manipulate the String chars using the String method charAt(int i).
B) Mathematically isolate your tens and ones digits and all the digits from the hundreds on up. This is probably what your teacher wants you to do. To do this, look up "integer division" and also the modulus operator. HTH.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-12-2008, 08:04 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Here is a the solution in the first way that Fubarable explain,

Code:
Scanner scn = new Scanner(System.in); String result = null; System.out.println("Enter the value: "); String str = scn.nextLine(); if(str.length() == 1) result = str + "0"; else result = str.substring(0, (str.length() - 2)) + str.charAt(str.length() - 1) + str.charAt(str.length() - 2);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-12-2008, 12:07 PM
Member
 
Join Date: Oct 2008
Location: UK
Posts: 30
Paul Richards is on a distinguished road
And here is the other way:

Code:
public int swapTensAndUnits(int n) { int tens = (n/10)%10; int units = n%10; int rest = n-n%100; return rest+units*10+tens; }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-12-2008, 05:18 PM
Member
 
Join Date: Oct 2008
Posts: 21
ianjedi is on a distinguished road
Quote:
Originally Posted by yadster101 View Post

Hi i just started my CompSci class and for hw my
Im not really sure how i would do this but my guess is that it is more of a math question than a java question. If you have any ideas that might help please let me know.
I would always have a go first, cause its going to get harder you need the basics. Good luck in your studies.

cheers

Ian J.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-12-2008, 06:04 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 883
Fubarable is on a distinguished road
Quote:
Originally Posted by ianjedi View Post
I would always have a go first, cause its going to get harder you need the basics. Good luck in your studies.
AMEN, Brother!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-13-2008, 05:42 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by ianjedi View Post
I would always have a go first, cause its going to get harder you need the basics. Good luck in your studies.

cheers

Ian J.
Yep, nice thoughts! Thanks.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-13-2008, 06:02 AM
Member
 
Join Date: Oct 2008
Posts: 19
gnarly hogie is on a distinguished road
Heh, that question totally blew my mind, you guys thought of the answer pretty quickly, do questions like that get easier over time?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-13-2008, 06:04 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
I'm not clear what you are trying to say here.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-13-2008, 06:09 AM
Member
 
Join Date: Oct 2008
Posts: 19
gnarly hogie is on a distinguished road
We'll I was pretty lost as that guy about the question because that isn't a normal math question and you have to think differently then normally does it get easier. I don't know if I can explain it any easier, maybe it's a dumb question?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 10-13-2008, 06:27 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
If you are looking in programing way, post #3 have the solution. Or else if you are looking on mathematical way, post #4 is the solution.

I don't know what you mean by normal math question. There are no single pattern in maths. In different applications it takes in different ways. Quite similar thing is done in image processing, when converting an image into black and white, and many more usages are there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 10-13-2008, 06:31 AM
Member
 
Join Date: Oct 2008
Posts: 19
gnarly hogie is on a distinguished road
Well what I meant is that when you switch the tens with the ones normally you would think just switch the places but that program for post 4 is not what you would normally think to do just to switch the integer.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CompSci 1 Novice, need some help annoyingzhang New To Java 9 10-12-2008 11:02 PM


All times are GMT +3. The time now is 11:10 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org