Untitled
package problem1; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; /** * This class aims to solve the first problem of "General Problems" work sheet * provided for the Distributed Systems course */ public class Problem1 { Stack<Character> stack = new Stack<>(); Queue<Character> fifo = new LinkedList<>(); Scanner scanner = new Scanner(System.in); public void start() { String word = ""; while (!word.equals("EXIT")) { System.out.println("New word: "); word = scanner.nextLine(); System.out.println("The word is " + word); parseWord(word); System.out.println(isPalindrome() ? "This is a palindrome" : "This is NOT a palindrome"); cleanStructures(); System.out.println(); } } public void parseWord(String word){ for (int i = 0; i < word.length(); i++){ stack.push(word.charAt(i)); fifo.add(word.charAt(i)); } } public Boolean isPalindrome(){ if (stack.size() != fifo.size()){ return false; } for (int i = 0; i < stack.size(); i++){ if (stack.peek() != fifo.peek()){ return false; } } return true; } public void cleanStructures(){ stack.clear(); fifo.clear(); } }
Leave a Comment