dsa

Data Structures & Algorithms - Spring 2018
Log | Files | Refs | README

Driver.sync-conflict-20180301-225031-QVHBCPO.java (4557B)


      1 package lab6;
      2 
      3 import java.util.Scanner;
      4 
      5 public class Driver {
      6 
      7     Scanner in = new Scanner(System.in);
      8 
      9     public static void main(String[] args) {
     10         int leftSideSize, rightSideSize, sizeDifference;    /* For comparing sizes of words. */
     11         boolean isPalindrome = false, isSame = true;        /* isSame initialized to true for the case where
     12                                                                no characters are entered. */
     13         Driver ui = new Driver();
     14         Deq<Character> leftSide = new Deq<>();
     15         Deq<Character> rightSide = new Deq<>();
     16 
     17         leftSideSize = ui.insertLeftSide(leftSide);
     18 
     19         if (leftSideSize != -1) {
     20             rightSideSize = ui.insertRightSide(rightSide);
     21             sizeDifference = ui.compareSides(leftSideSize, rightSideSize);
     22             while (!leftSide.isEmpty() && !rightSide.isEmpty()) {
     23                 isPalindrome = ui.checkPalindrome(leftSide, rightSide);
     24                 isSame = ui.checkSame(leftSide, rightSide);
     25                 leftSide.dequeue();
     26                 if (!leftSide.isEmpty()) {
     27                     leftSide.dequeueLast();
     28                 }
     29                 rightSide.dequeue();
     30                 if (!rightSide.isEmpty()) {
     31                     rightSide.dequeueLast();
     32                 }
     33             }
     34 
     35             if (sizeDifference > 0) {
     36                 System.out.println("Left side longer");
     37             } else if (sizeDifference < 0) {
     38                 System.out.println("Right side longer");
     39             } else {
     40                 if (isPalindrome && isSame) {
     41                     System.out.println("Same length, same content, palindrome");
     42                 } else if (isSame && !isPalindrome) {
     43                     System.out.println("Same length, same content, not palindrome");
     44                 } else {
     45                     System.out.println("Same length, different content");
     46                 }
     47             }
     48         }
     49     }
     50 
     51     public int insertLeftSide(Deq<Character> charDeq) {
     52         char userInput;
     53         int leftSideSize = 0;
     54         do {
     55             System.out.print("Enter Character: ");
     56             userInput = in.nextLine().charAt(0);
     57             if (userInput != '*') {
     58                 charDeq.enqueue(userInput);
     59                 leftSideSize++;
     60             }
     61             if (userInput == '!') {
     62                 System.out.println("No Star");
     63                 return -1;
     64             }
     65         }while (userInput != '*');
     66         return leftSideSize;
     67     }
     68 
     69     public int insertRightSide(Deq<Character> charDeq) {
     70         char userInput;
     71         int rightSideSize = 0;
     72         do {
     73             System.out.print("Enter Character: ");
     74             userInput = in.nextLine().charAt(0);
     75             if (userInput != '!') {
     76                 charDeq.enqueue(userInput);
     77                 rightSideSize++;
     78             }
     79         }while (userInput != '!');
     80         return rightSideSize;
     81     }
     82 
     83     public int compareSides(int leftSide, int rightSide) {
     84         if (leftSide - rightSide == 0) {
     85             return 0;
     86         }
     87         else if (leftSide - rightSide < 0) {
     88             return -1;
     89         }
     90         else {
     91             return 1;
     92         }
     93     }
     94 
     95     public boolean checkPalindrome(Deq<Character> leftSide, Deq<Character> rightSide) {
     96         if (leftSide.peek() != rightSide.peekLast()) {
     97             return false;
     98         }
     99         else {
    100             return true;
    101         }
    102     }
    103 
    104     public boolean checkSame(Deq<Character> leftSide, Deq<Character> rightSide) {
    105         if (leftSide.peek() != rightSide.peek()) {
    106             return false;
    107         }
    108         else {
    109             return true;
    110         }
    111     }
    112 
    113     public void insertItem(Deq myQueue, Object item) {
    114         System.out.println("Item " + item + " has been added");
    115         myQueue.enqueue(item);
    116 
    117     }
    118 
    119     public void insertFirst(Deq myQueue, Object item) {
    120         System.out.println("Item " + item + " has been added");
    121         myQueue.enqueueFirst(item);
    122     }
    123 
    124     public void removeItem(Deq myQueue) {
    125         System.out.println("Item " + myQueue.dequeue() + " has been removed");
    126     }
    127 
    128     public void removeLast(Deq myQueue) {
    129         System.out.println("Item " + myQueue.dequeueLast() + " has been removed");
    130     }
    131 
    132     public void peek(Deq myQueue) {
    133         System.out.println(myQueue.peek());
    134     }
    135 
    136     public void peekLast(Deq myQueue) {
    137         System.out.println(myQueue.peekLast());
    138     }
    139 
    140     public void clear(Deq myQueue) {
    141         myQueue.dequeueAll();
    142     }
    143 
    144     public void display(Deq myQueue) {
    145         System.out.println(myQueue.toString());
    146     }
    147 
    148 }