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.
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.
Step by step
Solved in 2 steps