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 entera 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 l'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 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 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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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.

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
Java Code for NumberReverse class

Computer Engineering homework question answer, step 1, image 1

Code text so you don't have to type it. But make sure to match it with above image after copying in your editor.

import java.util.Scanner;
public class NumberReverse {
    public static void main (String[] args) {
        while(true) {
            StackArray stackArray = new StackArray(20);
            Scanner console = new Scanner(System.in);
            System.out.print("Enter your 8-15 digit number: ");
            long number = 0;
            number = console.nextLong();
            if (number == -1) {
                System.out.println("Bye!");
                break;
            }
            if (number < 10000000L || number > 999999999999999L) {
                System.out.println("Your number is not 8-15 digits...");
                continue;
            }
            long doubleNumber = number*2;
            while(doubleNumber > 0) {
                stackArray.push((int)doubleNumber%10);
                doubleNumber = doubleNumber/10;
            }
            long result = 0;
            long power = 0;
            while( !(stackArray.empty()) ) {  // do till stack array is not empty
                long temp = stackArray.pop();
                temp = temp * (long)Math.pow(10, power);
                result = result + temp;
                power++;
            }
            System.out.printf("Reverse of double of your number is %,d\n", result);
        }
    }
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY