Question

In: Computer Science

Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...

Java and please have screenshots with source code and output

\item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate

\item[(2)] Let Q be a non-empty queue, and let S be an empty stack. Using only the stack and queue ADT functions and a single element variable X, write an algorithm to reverse the order of the elements in Q.

\item[(3)] Use singly linked lists to implement integers of unlimited size. Each node of the list should store one digit of the integer. You should implement addition, subtraction, multiplication, and exponentiation operations. Limit exponents to be positive integers. What is the asymptotic running time for each of your operations, expressed in terms of the number of digits for the two operands of each function?

\item[(4)] Implement a program that can input an expression in postfix notation and output its value.

Solutions

Expert Solution

Here is the code for the above problem,

These are 4 different questions and not a single problem to answer. Our guidelines don't allow us to answer more than 1 question at a time. But I have answered 3 problems leaving 3rd only.

Go through all and even if then you face any problem let me know in the comments, I would try to guide you but not copy-paste the code. I hope you understand.

Codes.java

import java.util.*;

public class Codes {
    private static final Scanner sc = new Scanner(System.in);

    // CODE 1
    private static boolean isPalindrome(StringBuilder sb) {
        int mid = sb.length() / 2;
        int begin = sb.length() % 2 == 0 ? mid - 1 : mid;
        int end = mid;
        while (begin >= 0 && end < sb.length()) {
            if (sb.charAt(begin) != sb.charAt(end)) return false;
            begin--;
            end++;
        }
        return true;
    }

    public static void reverseQueueItems(Queue<Integer> queue) {
        Stack<Integer> stack = new Stack<>();
        while (!queue.isEmpty()) stack.push(queue.poll());
        while (!stack.isEmpty()) queue.add(stack.pop());
    }

    public static int evaluatePostfix(String exp) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < exp.length(); i++) {
            char c = exp.charAt(i);
            if (Character.isDigit(c))
                stack.push(c - '0');
            else {
                int val1 = stack.pop();
                int val2 = stack.pop();
                switch (c) {
                    case '+':
                        stack.push(val2 + val1);
                        break;
                    case '-':
                        stack.push(val2 - val1);
                        break;
                    case '/':
                        stack.push(val2 / val1);
                        break;
                    case '*':
                        stack.push(val2 * val1);
                        break;
                }
            }
        }
        return stack.pop();
    }

    public static String printQueue(Queue<Integer> queue) {
        if (queue.isEmpty()) return "{}";
        StringBuilder sb = new StringBuilder();
        for (int value : queue) sb.append(value).append(", ");
        sb.setLength(sb.length() - 2);
        sb.insert(0, "{");
        sb.append("}");
        return sb.toString();
    }

    public static void main(String[] args) {

        // CODE 1 : Uncomment code to test
        /*
        StringBuilder sb = new StringBuilder();
        System.out.println("Enter $ to quit the program. Else continue entering the character line by line.");
        while (true){
            char ch = sc.next().charAt(0);
            if(ch == '$') break;
            sb.append(ch);
            boolean isPalindrome = isPalindrome(sb);
            System.out.println("isPalindrome? : " + isPalindrome);
        }
         */


        // CODE 2 : Uncomment code to test
        /*
        Queue<Integer> queue = new LinkedList<>();
        System.out.println("Enter -1 to quit and reverse the order of elements in queue. " +
                "\nElse continue entering the elements line by line.");
        while (true){
            int element = sc.nextInt();
            if(element == -1){
                System.out.println("Elements in queue Before : "+printQueue(queue));
                reverseQueueItems(queue);
                System.out.println("Elements in queue After : "+printQueue(queue));
                break;
            }
            queue.add(element);
        }
        */
        

        // CODE 4 : Uncomment code to test
        /*
        System.out.println("Enter correct postfix expression : ");
        String postfixExp = sc.next();
        int value = evaluatePostfix(postfixExp);
        System.out.println("Postfix value for :"+postfixExp+" is : "+value);
         */
    }
}

OUTPUTS:

Also, please do upvote the solution if I have answered your problem.


Related Solutions

Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards....
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives •...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives • Be able to write methods • Be able to call methods Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct...
Please attach the output screenshots, narrative descriptions, or paste the Python codes. Please write code to...
Please attach the output screenshots, narrative descriptions, or paste the Python codes. Please write code to print the type of the following variables. Please write down the codes and the output as well. x = 3.5 y = '3.5' z = {1:'John', 2:'Wick', 3:'Barry', 4:'Allen'}
Please show fully functioning Java code and screenshots of outputs. Please separate by 1a and 1b....
Please show fully functioning Java code and screenshots of outputs. Please separate by 1a and 1b. #1. Design a Java JProduct class for a product which implements both cloneable and comparable interfaces The class should have the following private member variables: m_id: an integer that holds the product ID m_name: a string that holds the product name m_wholesaleprice: a double that holds the wholesale price m_retailers: a String array that holds all retailers who sell the product and the class...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This algorithm is performed by moving the R/W head back-and-forth to the innermost and outermost track. As it scans the tracks from end to end, it process all the requests found in the direction it is headed. This will ensure that all track requests, whether in the outermost, middle or innermost location, will be traversed by the access arm thereby finding all the requests. This...
Please hurry, will upvote if correct! This is the problem in Java: [(1)] A palindrome is...
Please hurry, will upvote if correct! This is the problem in Java: [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or...
Meant to be written in Java JDK 14.0 A String object is called palindrome if it...
Meant to be written in Java JDK 14.0 A String object is called palindrome if it is same when read from the front and the end. For example, String “abcba” is palindrome, while “12322” is not. Write Java program to check a String object of 5 characters is palindrome or not: (a) The comparison is case sensitive (b) The comparison is case insensitive
1.   What is the output of the following code: string s1 = “X”; string s2 =...
1.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s2; 2.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s[0] + s[3]; 3.   What is the output of the following code: string s1 = “X”; string...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT