*
The moose likes Beginning Java and the fly likes Reverse Int, char, double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reverse Int, char, double" Watch "Reverse Int, char, double" New topic
Author

Reverse Int, char, double

ed spithaler
Greenhorn

Joined: Feb 29, 2012
Posts: 9
I have a programming project that is due very soon, but I am stuck on the requirements. I am able to figure out how to reverse 5 digits with no problem, but our instructor is including char and doubles as well and that's where I get confused. I have asked for further clarification, but I don't receive clear answers. There is also a reset function that I have no idea how to construct. I have included the requirements and my partially completed program. Thanks to anyone that help me in advance.

You are to write a program that reads in an integer and breaks it into a sequence of individual digits in reverse order.
For example, if the input is 16843, the result would be the sequence of values:
3
4
8
6
1
If the input is ‘A’, the result would be:
5
6
You may NOT assume the user input is positive; use Math.abs to guarantee a positive representation.
1) Write a class called DigitExtractor that performs the necessary operation specified above. It should
minimally contain two instances variables: (1) the original integer value itself and (2) a working variable to
help with place holding (I suggest a positive representation of the original integer value that you can modify as
needed).
2) The class should have the following methods:
• A default constructor that initializes all instance fields.
• A parameterized constructor that takes an input int.
• A parameterized constructor that takes an input double. Type-casting will be required to convert to
an integer.
• A parameterized constructor that takes an input char. Type-casting will be required to convert to an
integer.
• Accessor method for the integer.
• Mutator method for the integer. It the mutator is called, the state of the class should be reset.
• A nextDigit method that will return the integer value of the next digit extracted (as described
above). If you are at the end of the number, 0 should be returned.
• A reset method that will reset the object back to its original state so that nextDigit begins again
at the ones place.
3) Implement a driver program called DigitPrinter to test your DigitExtractor class; thoroughly test
all methods.







[Edit - added code tags - see UseCodeTags for details]
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
If this is an example of how you are asking for clarification, the reason you're not getting clear answers may be because you don't ask specific, clear questions. You've given us a lot of info and have stated that you're confused, but you don't describe your confusion or ask specific questions that we can answer. Without that, we're left with explaining the assignment to you, and that's not likely to happen in great detail.

So, what part of the assignment is confusing you? If you can reverse the digits of an integer, the assignment tells you what to do to turn all problems into one about integers. As for the reset requirement, there are multiple ways to do that from storing a copy of the original value to writing code that simply takes the answer after being reversed and restoring it to the original value, an inverse reverser.

Please come back with better questions. Pick a specific part of the assignment that's confusing you, show your code for that part, describe any problems and confusion you're having with it, and ask for help for that part. Repeat 'til done.

BTW - post your code in code tags and include any error messages you receive exactly as they appear, copied and pasted.

Good luck!


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
Welcome to the Ranch

What makes your instructor think Math.abs guarantees a positive result?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

ed spithaler wrote:I have a programming project that is due very soon, but I am stuck on the requirements. I am able to figure out how to reverse 5 digits with no problem, but our instructor is including char and doubles as well and that's where I get confused. I have asked for further clarification, but I don't receive clear answers. There is also a reset function that I have no idea how to construct. I have included the requirements and my partially completed program. Thanks to anyone that help me in advance.

You are to write a program that reads in an integer and breaks it into a sequence of individual digits in reverse order.
For example, if the input is 16843, the result would be the sequence of values:
3
4
8
6
1
If the input is ‘A’, the result would be:
5
6
You may NOT assume the user input is positive; use Math.abs to guarantee a positive representation.
1) Write a class called DigitExtractor that performs the necessary operation specified above. It should
minimally contain two instances variables: (1) the original integer value itself and (2) a working variable to
help with place holding (I suggest a positive representation of the original integer value that you can modify as
needed).
2) The class should have the following methods:
• A default constructor that initializes all instance fields.
• A parameterized constructor that takes an input int.
• A parameterized constructor that takes an input double. Type-casting will be required to convert to
an integer.
• A parameterized constructor that takes an input char. Type-casting will be required to convert to an
integer.

• Accessor method for the integer.
• Mutator method for the integer. It the mutator is called, the state of the class should be reset.
• A nextDigit method that will return the integer value of the next digit extracted (as described
above). If you are at the end of the number, 0 should be returned.
• A reset method that will reset the object back to its original state so that nextDigit begins again
at the ones place.
3) Implement a driver program called DigitPrinter to test your DigitExtractor class; thoroughly test
all methods.



Based on the requirements, it just looks like a simple cast for double and char. This means, for double, the fractional portion is removed, and for char, the ASCII value is used. This is done by the cast -- all your code cares about is to reverse the int.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reverse Int, char, double
 
Similar Threads
New to java
Loops and prime numbers (Help me, I'm desperate!)
June Newsletter Puzzle
eek, help!
Convert String to primitive int without using Wrapper classes