create a tester class that will create a queue of ten names. You will say what t
ID: 651227 • Letter: C
Question
create a tester class that will create a queue of ten names. You will say what the length of the queue is calling the capacity method. You will say what name is in front using the peek method and you will print all the names in the FIFO order.
Use a stack to reverse the words in a sentence. Keep reading words until you have a word that ends in a period, adding them onto a stack. When you have a word with a period, pop the words off and print them. Stop when there are no more words in the input. For example, you should turn the input
Mary had a little lamb. Its fleece was white as snow.
INTO
Lamb little a had mary. Snow as white was fleece its.
Pay attention to capitalization and placement of the period.
help!
JAVA
Explanation / Answer
Java code for the QueueTester class:-
import java.util.LinkedList;
import java.util.Queue;
public class QueueTester {
public static void main(String[] args){
Queue<String> names = new LinkedList<String>();
names.add("abc");
names.add("def");
names.add("ghi");
names.offer("jkl");
names.add("mno");
names.add("pqr");
names.add("stu");
names.offer("vwx");
names.add("yz");
names.add("xxx");
System.out.println("Name in front: "+names.peek());
System.out.println("All the names in FIFO order: ");
for(String element : names){
System.out.println("Element : " + element);
}
}
}
Java code for the stack implementation for word reversing:-
package com.tutai;
import java.util.Scanner;
import java.util.Stack;
public class ReverseWords {
public static void main(String[] args) {
Stack<String> st=new Stack<String>();
Scanner s=new Scanner(System.in);
System.out.println("Enter a line: ");
String input=s.nextLine();
String word="";
for(int i=0;i<input.length();i++){
if(input.charAt(i)!=' '&&input.charAt(i)!='.'){
word+=input.charAt(i);
}else{
st.push(word);
word="";
}
}
int i=0;
String str;
int totalSize=st.size();
for(i=0;i<totalSize;i++){
str=st.pop();
if(i==0){
str=Character.toUpperCase(str.charAt(0))+str.substring(1);
}
if(i==totalSize-1){
str=Character.toLowerCase(str.charAt(0))+str.substring(1);
System.out.print(str+".");
}else{
System.out.print(str+" ");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.