Anda di halaman 1dari 3

package assign2;

//David Philbin - 13487992 - 2BCT - CT229


import javax.swing.JOptionPane;
//this class will contain our palindrome operation
public class Test1 {
public static void Testcase(String testword, int stackLength, int queueLength)
//pass in word, stack and queue
{
Stack s = new ArrayStack(stackLength); //make a stack
Queue e = new ArrayQueue(queueLength); //make a queue
String d = ""; //initializes strings
String r = "";
int count=0;
int invalidCount=0;
String word = testword.toUpperCase(); //get user to input string and
converts to upper-case
for (int i=0; i<word.length(); i++)
{
char c = word.charAt(i); //gets the character at position i
/*
* Checks if character being entered is a letter or whitespace
* If it's not a letter or a whitespace increase count
*/
if
(Character.isLetter(c)==true&&Character.isWhitespace(c)==false)
{
s.push(c); //pushes this into the stack
e.enqueue(c); //enqueues letter in Queue
}
else
{
invalidCount++; //we're keeping track of all the instances
where's it's an invalid char
}
}
JOptionPane.showMessageDialog(null, "About to show if word is
palindrome...");
while(!s.isEmpty()&&!e.isEmpty())
{
char f = (Character)s.pop(); //casts to character
char g = (Character)e.dequeue(); //casts to character
d+=f; //just adds f to the string (Stack)
r+=g; //just adds g to the string (Queue)
if (f==g) //if the character f is equal to the character g
{
count++; //count should equal the length of the word
}
}

if (word.length()==invalidCount) //if there's no valid characters


entered it'll equal count2
{
JOptionPane.showMessageDialog(null, "You didn't enter any valid
characters");
return;
}
if (count==word.length()-invalidCount) //count will equal the word
length - invalid chars
{
JOptionPane.showMessageDialog(null, "The word " + d + " is a
palindrome"); //prints
}
else
{
JOptionPane.showMessageDialog(null, "The word " +r + " is not a
palindrome"); //prints
}
System.exit(0);
}
}

package assign2;
/**
* M Madden, Nov 2005: Test the ArrayStack ADT
*/

import javax.swing.JOptionPane;
public class Test //testing the program
{
public static void main(String[] args)
{
int stackLength=0;
int queueLength=0;
String stackL = JOptionPane.showInputDialog("Input a stack capacity:");
//input stack capacity
try
{
stackLength = Integer.parseInt(stackL); //parse to int
}
catch(NumberFormatException e)
{
System.out.println("You have not entered a valid number!");
return;
}
try
{
String queueL = JOptionPane.showInputDialog("Input a queue capacity:");
//input queue capacity
queueLength = Integer.parseInt(queueL); //parse to int
}
catch(NumberFormatException e)
{
System.out.println("You have not entered a valid number!");
return;
}
String testword = JOptionPane.showInputDialog("Please enter a word:");
//receive input
Test1.Testcase(testword, stackLength, queueLength); //pass in the word,
the stack and the queue
}

Anda mungkin juga menyukai