Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me code the following in: JAVA Please use many COMMENTS Full points will be awarded, thanks in advance! UsingStackSuitorsLab class: import java.util.LinkedList; import

Please help me code the following in: JAVA

Please use many COMMENTS

Full points will be awarded, thanks in advance!

image text in transcribedimage text in transcribed UsingStackSuitorsLab class:

import java.util.LinkedList;

import java.util.Queue;

public class UsingStacksSuitorsLab implements Runnable {

private static int threadCount = 0;

private String name;

public UsingStacksSuitorsLab() {

name = "#" + threadCount++ + "Thread";

}

public static void main(String[] args) {

String s1 = "food"; /ot a palindrome

String s2 = "racecar"; //a palindrome

System.out.println("String1 is \"" + s1 + "\"");

System.out.println("String2 is \"" + s2 + "\"");

/* System.out.println(s1 + " reversed is: ");

printReverse(s1);

System.out.println(s2 + " reversed is: ");

printReverse(s2);

recPrintReverse(s1);

System.out.println();

recPrintReverse(s2);

System.out.println();

System.out.println(s1 + " is a palindrome: " + isPalindrome(s1));

System.out.println(s2 + " is a palindrome: " + isPalindrome(s2));

System.out.println(s1 + " is a palindrome(recursively): " + isPalindromeRec(s1));

System.out.println(s2 + " is a palindrome(recursively): " + isPalindromeRec(s2));

System.out.println("Did we build a Queue of Threads and start them? " + buildThreadQueue());

int n = 6;

System.out.println("For " + n + " suitors, stand in place:" + findPlaceToStand(n));

n = 10;

System.out.println("For " + n + " suitors, stand in place:" + findPlaceToStand(n));*/

}

public static void printReverse(String target) {

//todo: use a stack

}

public static void recPrintReverse(String target) {

//todo

}

public static boolean isPalindrome(String input) {

//todo: use a stack

}

public static boolean isPalindromeRec(String sentence) {

//todo

}

public static int findPlaceToStand(int numSuitors) {

//todo

return -1;

}

public static boolean buildThreadQueue() { //returns true upon success

Queue q = new LinkedList(); //comment this out and use your own Queue

//when our program starts up, it might create multiple threads to use

//q.enqueue( new Thread(new UsingStacksSuitorsLab()));

System.out.println("Initial Thread order:");

q.toString();

//We need to iterate over our pool of threads and call start() on each one

//Make a loop that dequeues a thread, calls start on it, and enqueues it again

//for(?) {

Thread curreent = q.deque();

current.start();

q.enqueue(current);

/* current = get a thread

current.start();

put the thread back

}*/

System.out.println("Thread order after start()ing:");

q.toString();

return true; //on successful start

}

/*

* No need to edit anything below here,

* unless you'd like to change the

* behaviour of each thread in the thread pool above

*/

@Override

public void run() {

for(int i = 0; i

System.out.println(name + ": " + i + "th iteration");

try {

Thread.sleep(10);

} catch (InterruptedException e) {

//do nothing here

}

}

}

}

Warming Up With Reversing Strings Palindromes are interesting strings in that they are the same when displayed both left-to-right and right to-left. For example, "Do geese see God" or "A man a plan a canal: panama!" are both palindromes, not considering punctuation or case sensitivity. We'll write multiple methods to determine whether a string is a palindrome, and we can make use of Stacks to accomplish this. To get started, download the driver code and first practicing printing a String in reverse using a Stack: Download the driver file UsingStacksSuitorsLab.java and read it Find the main method and comment out everything below the printReversel) function calls in main Find the printReverse() method and declare a Stack inside of it . Use your Stack from the previous labs (the Linked Stack is preferable here) "If your Stack is broken, you can consider using Java's Stack Stack foo new Stack o o Write a loop that iterates over the input string In the loop, push each char from the input string on the stack o Write a second loop that iterates until the stack is empty In the loop, pop off a char from the stack and print it. " This output should be the reverse of the input string Execute the code and observe your output; does it match the sample output below? If not, see if your Stack is the culprit by commenting it out and trying Java's built-in Stack class (java.util). Recursively Reversing Strings Using the Method Call Stack In this section, we'll rewrite the iterative code above as a recursive function. In doing so, we'll remove the looping constructs (for/while is dropped in place of a recursive call), and we'll remove the explicit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions