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
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.
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...
Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
Write a Java program that will add the digits of a person’s birth date to obtain...
Write a Java program that will add the digits of a person’s birth date to obtain a single digit to generate a numerology number. Write one separate method for each of the following tasks (it goes w/o saying that you will have a main() method along with these): date validating date crunching First: Get a Date Numerology has been used since ancient times to shed light on relationships, health, and global events. Each element in a birth date is believed...
Write a program that uses a for loop output a string in reverse printing it character...
Write a program that uses a for loop output a string in reverse printing it character by character. C++ Program Example: "Enter some text:" this is a test "your text in reverse is:" tset a si siht Please note that you can find the length of the string by using the length function: string str = "hello" cout << str.length(); Also you can et a character in a string by providing an index to the function string str = "hello"...
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT