• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reverse Int, char, double

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What makes your instructor think Math.abs guarantees a positive result?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic