Week 7 Exercise 4 I am currently using Ec;ipse to write in Java Both code and sc
ID: 673858 • Letter: W
Question
Week 7 Exercise 4
I am currently using Ec;ipse to write in Java
Both code and screen shot of the output are required to verify that the program is working correctly. Thanks in advance for both your time and effort.
Here is the stack data structure in JCF:
/******************************
* Week 7 lab - exercise 1: *
* using the java.util Stack *
*******************************/
import java.util.*;
/**
* Class to test the java.util.Stack class.
*/
public class Main
{
public static void main(String[] args)
{
Stack<Character> s = new Stack<Character>();
System.out.println("Insertion of 10 characters in s");
for (int i = 0; i < 10; i++)
{
int x = 32 + (int) (Math.random() * 95);
System.out.println(x + " --> " + (char) x);
s.push((char) x);
}
System.out.println(" Displaying and deleting elements");
for (int i = 0; i < 10; i++)
{
System.out.println("Item at the top: " + s.peek());
s.pop();
}
}
}
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Palindrome
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
String text = "abba";
Stack<Character> stackCharacter = new Stack<Character>();
for (int i = 0; i < text.length(); i++) {
stackCharacter.push(text.charAt(i));
}
String reversetText = "";
while (!stackCharacter.isEmpty()) {
reversetText += stackCharacter.pop();
}
if (text.equals(reversetText))
System.out.println("Yes! that is a palindrome.");
else
System.out.println("No! that isn't a palindrome.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.