Question

In: Computer Science

Write a java program that uses a stack to reverse an integer of three digits. The...

Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse.
- Your program must include the Stack class, the Reverse class, and the Test class.
- In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse.
- Class Reverse must use the Stack class to reverse the number.
Note: the input must be of type integer.
Here is a sample run:
Enter a 3-digit integer: 123
The reversed number: 321

Solutions

Expert Solution

Here is the complete java code with comments:

NOTE: Not only it works for 3 digits, but for even more number of digits :)


//To get input
import java.util.*;
//To use power function
import java.lang.Math;

//This is the stack class
class StackClass {
    // This arraylist will work as a stack
    public ArrayList<Integer> mystack = new ArrayList<Integer>();

    // This will add to the stack till the number becomes 0
    public StackClass(int n) {
        while (n != 0) {
            mystack.add(n % 10);
            n = n / 10;
        }
    }

}

// Reverse Class
class Reverse {
    int reversedNum;

    Reverse(StackClass st) {
        reversedNum = 0;
        for (int i = 0; i < st.mystack.size(); i++) {
            /*
             * The numeber gets converted like this: If the number is 3 2 1 in the stack
             * then it will be converted like this revNum = 3*(100) + 2*(10) + 1(1) = 321
             * which is reverse of 123
             */
            reversedNum = (int) (reversedNum + st.mystack.get(i) * (Math.pow(10, st.mystack.size() - i - 1)));
        }
    }
}

public class Test {
    // This is the main function
    public static void main(String[] args) {
        // The scanner is used to take the input
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a 3 digit number: ");
        int n = sc.nextInt();
        StackClass stobj = new StackClass(n);
        Reverse obj = new Reverse(stobj);
        System.out.println("The reversed number: " + obj.reversedNum);
        sc.close();
    }
}

Here is the output for the above code:


Related Solutions

Write a java program to reverse element of a stack. For any given word (from input),...
Write a java program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be the same as the input. Your program should be using a stack and a queue to complete this process. 1. Push into stack 2. Pop from stack 3. Enqueue into queue 4. Dequeue from queue 5. Push into stack 6. Pop from stack and display java
in .java Write a program that reads an integer with 3 digits and prints each digit...
in .java Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, ... Enter an integer of exactly 3 digits(e.g. 538): 319 9 1 3 Hint: consider what you get from these operations: 319%10 319/10 31%10
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack...
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack (or your own implementation) for this problem. Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack s using a Queue. Test your method using some example stacks. In java
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT