dsa

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

Driver.java (4656B)


      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                 if (isSame) {
     25                     isSame = ui.checkSame(leftSide, rightSide);
     26                 }
     27                 leftSide.dequeue();
     28                 if (!leftSide.isEmpty()) {
     29                     leftSide.dequeueLast();
     30                 }
     31                 rightSide.dequeue();
     32                 if (!rightSide.isEmpty()) {
     33                     rightSide.dequeueLast();
     34                 }
     35             }
     36 
     37             if (sizeDifference > 0) {
     38                 System.out.println("Left side longer");
     39             } else if (sizeDifference < 0) {
     40                 System.out.println("Right side longer");
     41             } else {
     42                 if (isPalindrome && isSame) {
     43                     System.out.println("Same length, same content, palindrome");
     44                 } else if (isSame && !isPalindrome) {
     45                     System.out.println("Same length, same content, not palindrome");
     46                 } else {
     47                     System.out.println("Same length, different content");
     48                 }
     49             }
     50         }
     51     }
     52 
     53     public int insertLeftSide(Deq<Character> charDeq) {
     54         char userInput;
     55         int leftSideSize = 0;
     56         do {
     57             System.out.print("Enter Character: ");
     58             userInput = in.nextLine().charAt(0);
     59             if (userInput != '*') {
     60                 charDeq.enqueue(userInput);
     61                 leftSideSize++;
     62             }
     63             if (userInput == '!') {
     64                 System.out.println("No Star");
     65                 return -1;
     66             }
     67         }while (userInput != '*');
     68         return leftSideSize;
     69     }
     70 
     71     public int insertRightSide(Deq<Character> charDeq) {
     72         char userInput;
     73         int rightSideSize = 0;
     74         do {
     75             System.out.print("Enter Character: ");
     76             userInput = in.nextLine().charAt(0);
     77             if (userInput != '!') {
     78                 charDeq.enqueue(userInput);
     79                 rightSideSize++;
     80             }
     81         }while (userInput != '!');
     82         return rightSideSize;
     83     }
     84 
     85     public int compareSides(int leftSide, int rightSide) {
     86         if (leftSide - rightSide == 0) {
     87             return 0;
     88         }
     89         else if (leftSide - rightSide < 0) {
     90             return -1;
     91         }
     92         else {
     93             return 1;
     94         }
     95     }
     96 
     97     public boolean checkPalindrome(Deq<Character> leftSide, Deq<Character> rightSide) {
     98         if (leftSide.peek() != rightSide.peekLast()) {
     99             return false;
    100         }
    101         else {
    102             return true;
    103         }
    104     }
    105 
    106     public boolean checkSame(Deq<Character> leftSide, Deq<Character> rightSide) {
    107         if (leftSide.peek() != rightSide.peek() || leftSide.peekLast() != rightSide.peekLast()) {
    108             return false;
    109         }
    110         else {
    111             return true;
    112         }
    113     }
    114 
    115     public void insertItem(Deq myQueue, Object item) {
    116         System.out.println("Item " + item + " has been added");
    117         myQueue.enqueue(item);
    118 
    119     }
    120 
    121     public void insertFirst(Deq myQueue, Object item) {
    122         System.out.println("Item " + item + " has been added");
    123         myQueue.enqueueFirst(item);
    124     }
    125 
    126     public void removeItem(Deq myQueue) {
    127         System.out.println("Item " + myQueue.dequeue() + " has been removed");
    128     }
    129 
    130     public void removeLast(Deq myQueue) {
    131         System.out.println("Item " + myQueue.dequeueLast() + " has been removed");
    132     }
    133 
    134     public void peek(Deq myQueue) {
    135         System.out.println(myQueue.peek());
    136     }
    137 
    138     public void peekLast(Deq myQueue) {
    139         System.out.println(myQueue.peekLast());
    140     }
    141 
    142     public void clear(Deq myQueue) {
    143         myQueue.dequeueAll();
    144     }
    145 
    146     public void display(Deq myQueue) {
    147         System.out.println(myQueue.toString());
    148     }
    149 
    150 }