Skip to content

Instantly share code, notes, and snippets.

@Goddard
Created May 31, 2010 22:20
Show Gist options
  • Save Goddard/420331 to your computer and use it in GitHub Desktop.
Save Goddard/420331 to your computer and use it in GitHub Desktop.
/**
* Description of the class or document.
*
* @author Ryein C. Bowling
* @version CS162 Lab 9, 5.28.2010
*/
import java.io.*;
import java.util.*;
public class palindrome
{
private String PATH = System.getProperty("java.class.path");
private File f = new File(PATH);
private String ROOT = f.getParent();
private String WORD_FILE;
private int Size;
private boolean exists = false;
private BufferedReader brWord = null;
private String lineWord = null;
private String original = "";
private Stack stack = new Stack();
private String stackString = "";
private Queue queue = new LinkedList();
private String queueString = "";
private char[] characters;
/**
* Constructor for objects of class pallendrome
*/
public palindrome()
{
}
public String removePunctuation(String lineWord)
{
lineWord = lineWord.replaceAll("[^A-Za-z]", "");
return lineWord;
}
public char[] stringToChar(String lineWord)
{
characters = lineWord.toCharArray();
return characters;
}
public Stack charToStack(char[] characters)
{
for(int i=0; i<characters.length; i++){
stack.push(characters[i]);
}
return stack;
}
public String printStack()
{
while (!stack.isEmpty())
{
stackString = stack.pop() + stackString;
}
return stackString;
}
public Queue charToQueue(char[] characters)
{
for(int i=0; i<characters.length; i++){
queue.offer(characters[i]);
}
return queue;
}
public String printQueue()
{
while(!queue.isEmpty())
{
queueString = queue.remove() + queueString;
}
return queueString;
}
public boolean isPalindrome()
{
if(stackString.equals(queueString) || Size <= 1)
{
return true;
}else{
return false;
}
}
public int setGetSize(int Size)
{
this.Size = Size;
return Size;
}
public boolean setWordFile(String wordFile) throws IOException
{
//if you run as jar use this line
WORD_FILE = ROOT + "/" + wordFile;
//if you run this in an IDE uncomment this line and comment the above out
//WORD_FILE = wordFile;
File file = new File(WORD_FILE);
exists = file.exists();
brWord = new BufferedReader(new FileReader(WORD_FILE));
return exists;
}
public void build() throws IOException
{
while((lineWord = brWord.readLine()) != null){
original = lineWord;
lineWord = removePunctuation(lineWord);
lineWord = lineWord.toUpperCase();
stringToChar(lineWord);
setGetSize(characters.length);
charToStack(characters);
printStack();
charToQueue(characters);
printQueue();
if (lineWord == null){
brWord.close();
}
}
}
public void question() throws IOException
{
System.out.print("Enter the name of the file you would have us analyze: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String fileName = null;
fileName = br.readLine();
setWordFile(fileName);
}
public static void main(String[] args)
{
palindrome palindrome1 = new palindrome();
try
{
palindrome1.question();
palindrome1.build();
if(palindrome1.isPalindrome() == true)
{
System.out.println("Our sophisticated skills of deduction have indicated through a complex process of computer science ingenuity that the file you had us analyze was indeed a palindrome.");
System.out.println("Original Text: " + palindrome1.original);
System.out.println("Stack Data: " + palindrome1.stackString);
System.out.println("Queue Data: " + palindrome1.queueString);
System.out.println("True / False: true");
}else{
System.out.println("Our sophisticated skills of deduction have indicated through a complex process of computer science ingenuity that the file you had us analyze was not a palindrome.");
System.out.println("Original Text: " + palindrome1.original);
System.out.println("Stack Data: " + palindrome1.stackString);
System.out.println("Queue Data: " + palindrome1.queueString);
System.out.println("True / False: false");
}
}catch(IOException e){
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment