Create a new project named NumberReverse, with classes NumberReverseand the StackArray class we developed in class. You will not need to make any changes to the StackArray class at all. The NumberReverseclass will have the main method in it. The program will run a loop in which you will prompt the user to enter a very long integer number (8-15 digits). You will read it using a Scanner method nextLong. Once you have the input, you will compute a new value which is two times your input. For example, if you read in 1234, your doubled value would be 2468. You will push each digit from the doubled number (right to left) onto a stack. So in our example, you would push 8, 6, 4, and finally 2. Your stack should logically look like the diagram below. Recall that n%10 returns a number’s 1’s digit. Also n/10 returns the number without the 1’s digit. For example, 2468%10 returns 8, while 2468/10 returns 246. In a loop you, will strip off the 1’s digit, then reduce the number using n/10, until your number n is zero. At that point your number’s digits will all be pushed onto the stack (as shown below). Once your digis are all pushed onto the stack, you will create a new long integer, result, initialized to 0. You will also create an integer called power initialized to 0. Then you will pop off the digits from the stack until it is empty. The process is as follows. As you pop off a digit, you will multiply it by 10power . Increment power by one at the end of each iteration. For example, when you pop off the 2, it will be multiplied by 100 , the 4 will be multiplied by 101 , the 6 by 102 and the 8 by 103 . Each time you pop off and multiply by a power of 10, you will accumulate that value into the result. You can use the Math.pow() method to raise to a power. Since that method return a double, you may need to cast the returning value to the type you need. Once the result has been calculated you will output it, and the program will prompt for another input, and the process will repeat for the next long integer input. Your program should be able to process as many long integers as you want. When you enter a -1, that indicates that you want to quit, so the program will end. Notice that the output of the numbers requires a comma (,) separator. The easiest way to do this is to use the printf method with a integer conversion %,d. The comma indicates that comma separators will be added whenever needed for the integer display.

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter12: Using Controls
Section: Chapter Questions
Problem 12E
icon
Related questions
Question
100%

public class StackArray 
{

    private int [] elements;
    private int top;
    private int size;
    
    public StackArray (int size)
    {
        elements = new int[size];
        top = -1;
        this.size = size;
    }
    
    public boolean empty()
    {
        return top == -1;
    }
    
    public boolean full()
    {
        return top == size - 1;
    }
    
    public boolean push(int el)
    {
        if (full())
            return false;
        else
        {
            top++;
            elements[top] = el;
            return true;
        }
    }
    
    public int pop()
    {
        int el = elements[top];
        top--;
        return el; 
    }
    
    
}

Create a new project named NumberReverse, with classes NumberReverseand the StackArray class we developed in class. You will not need to make any changes to the StackArray class at all. The NumberReverseclass will have the main method in it. The program will run a loop in which you will prompt the user to enter a very long integer number (8-15 digits). You will read it using a Scanner method nextLong. Once you have the input, you will compute a new value which is two times your input. For example, if you read in 1234, your doubled value would be 2468. You will push each digit from the doubled number (right to left) onto a stack. So in our example, you would push 8, 6, 4, and finally 2. Your stack should logically look like the diagram below. Recall that n%10 returns a number’s 1’s digit. Also n/10 returns the number without the 1’s digit. For example, 2468%10 returns 8, while 2468/10 returns 246. In a loop you, will strip off the 1’s digit, then reduce the number using n/10, until your number n is zero. At that point your number’s digits will all be pushed onto the stack (as shown below). Once your digis are all pushed onto the stack, you will create a new long integer, result, initialized to 0. You will also create an integer called power initialized to 0. Then you will pop off the digits from the stack until it is empty. The process is as follows. As you pop off a digit, you will multiply it by 10power . Increment power by one at the end of each iteration. For example, when you pop off the 2, it will be multiplied by 100 , the 4 will be multiplied by 101 , the 6 by 102 and the 8 by 103 . Each time you pop off and multiply by a power of 10, you will accumulate that value into the result. You can use the Math.pow() method to raise to a power. Since that method return a double, you may need to cast the returning value to the type you need. Once the result has been calculated you will output it, and the program will prompt for another input, and the process will repeat for the next long integer input. Your program should be able to process as many long integers as you want. When you enter a -1, that indicates that you want to quit, so the program will end. Notice that the output of the numbers requires a comma (,) separator. The easiest way to do this is to use the printf method with a integer conversion %,d. The comma indicates that comma separators will be added whenever needed for the integer display.

Programoutput
Enter a positive long integer with 8 to 15 digits: 11223344
You entered 11,223,344
Doubled the value is 22,446,688
Result of reverse is 88,664,422
Enter a positive long integer with 10 to 15 digits (-1 to quit): 123456781234567
You entered 123,456,781,234,567
Doubled the value is 246,913,562,469,134
Result of reverse is 431,964, 265,319,642
Enter a positive long integer with 10 to 15 digits (-1 to quit): 222211112222
You entered 222,211,112,222
Doubled the value is 444,422,224,444
Result of reverse is 444,422,224,444
Enter a positive long integer with 10 to 15 digits (-1 to quit): 132435465768798
You entered 132,435,465,768,798
Doubled the value is 264,870,931,537,596
Result of reverse is 695,735,139,078,462
Enter a positive long integer with 10 to 15 digits (-1 to quit): 1567
You entered 1,567
Doubled the value is 3,134
Result of reverse is 4,313
Enter a positive long integer with 10 to 15 digits (-1 to quit): -1
Program Finished
Transcribed Image Text:Programoutput Enter a positive long integer with 8 to 15 digits: 11223344 You entered 11,223,344 Doubled the value is 22,446,688 Result of reverse is 88,664,422 Enter a positive long integer with 10 to 15 digits (-1 to quit): 123456781234567 You entered 123,456,781,234,567 Doubled the value is 246,913,562,469,134 Result of reverse is 431,964, 265,319,642 Enter a positive long integer with 10 to 15 digits (-1 to quit): 222211112222 You entered 222,211,112,222 Doubled the value is 444,422,224,444 Result of reverse is 444,422,224,444 Enter a positive long integer with 10 to 15 digits (-1 to quit): 132435465768798 You entered 132,435,465,768,798 Doubled the value is 264,870,931,537,596 Result of reverse is 695,735,139,078,462 Enter a positive long integer with 10 to 15 digits (-1 to quit): 1567 You entered 1,567 Doubled the value is 3,134 Result of reverse is 4,313 Enter a positive long integer with 10 to 15 digits (-1 to quit): -1 Program Finished
Create a new project named NumberReverse, with classes NumberReverseand the StackArray
class we developed in class. You will not need to make any changes to the StackArray class at all.
The NumberReverseclass will have the main method in it. The program will run a loop in which you
will prompt the user to enter a very long integer number (8-15 digits). You will read it using a Scanner
method nextLong. Once you have the input, you will compute a new value which is two times your
input. For example, if you readin 1234, your doubled value would be 2468.
You will push each digit from the doubled number (right to left) onto a stack. So in our example, you
would push 8, 6, 4, and finally 2. Your stack should logically look like the diagram below. Recall that
n%10 returns a number's 1's digit. Also n/10 returns the number without the 1's digit. For example,
2468%10 returns 8, while 2468/10 returns 246. In a loop you, will strip off the 1's digit, then reduce the
number using n/10, until your number n is zero. At that point your number's digits will all be pushed
onto the stack (as shown below).
top
Once your digis are all pushed onto the stack, you will create a new long integer, result, initialized to
0. You will also create aninteger called power initialized to 0. Then you will pop off the digits from the
stack until it is empty. The process is as follows. As you pop off a digit, you will multiply it by 10Power.
Increment power by one at the end of each iteration. For example, when you pop off the 2, it will be
multiplied by 10°, the 4 will be multiplied by 10', the 6 by 10² and the 8 by 10°. Each time you pop off
and multiply by a power of 10, you will accumulate that value into the result. You can use the
Math.pow() method to raise to a power. Since that method return a double, you may need to cast the
returning value to the type you need. Once the result has been calculated you will output it, and the
program will prompt for another input, and the process will repeat for the next long integer input. Your
program should be able to process as many long integers as you want. When you enter a -1, that
indicates that you want to quit, so the program will end.
Notice that the output of the numbers requires a comma (,) separator. The easiest way to do this is to
use the printfmethod with a integer conversion %,d. The comma indicates that comma separators
will be added whenever needed for the integer dis play.
Transcribed Image Text:Create a new project named NumberReverse, with classes NumberReverseand the StackArray class we developed in class. You will not need to make any changes to the StackArray class at all. The NumberReverseclass will have the main method in it. The program will run a loop in which you will prompt the user to enter a very long integer number (8-15 digits). You will read it using a Scanner method nextLong. Once you have the input, you will compute a new value which is two times your input. For example, if you readin 1234, your doubled value would be 2468. You will push each digit from the doubled number (right to left) onto a stack. So in our example, you would push 8, 6, 4, and finally 2. Your stack should logically look like the diagram below. Recall that n%10 returns a number's 1's digit. Also n/10 returns the number without the 1's digit. For example, 2468%10 returns 8, while 2468/10 returns 246. In a loop you, will strip off the 1's digit, then reduce the number using n/10, until your number n is zero. At that point your number's digits will all be pushed onto the stack (as shown below). top Once your digis are all pushed onto the stack, you will create a new long integer, result, initialized to 0. You will also create aninteger called power initialized to 0. Then you will pop off the digits from the stack until it is empty. The process is as follows. As you pop off a digit, you will multiply it by 10Power. Increment power by one at the end of each iteration. For example, when you pop off the 2, it will be multiplied by 10°, the 4 will be multiplied by 10', the 6 by 10² and the 8 by 10°. Each time you pop off and multiply by a power of 10, you will accumulate that value into the result. You can use the Math.pow() method to raise to a power. Since that method return a double, you may need to cast the returning value to the type you need. Once the result has been calculated you will output it, and the program will prompt for another input, and the process will repeat for the next long integer input. Your program should be able to process as many long integers as you want. When you enter a -1, that indicates that you want to quit, so the program will end. Notice that the output of the numbers requires a comma (,) separator. The easiest way to do this is to use the printfmethod with a integer conversion %,d. The comma indicates that comma separators will be added whenever needed for the integer dis play.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,