home study engineering computer science questions and an
home / study / engineering / computer science / questions and answers / problem 1: using java implement an application ...
Your question has been answered! Rate it below.
Let us know if you got a helpful answer.
Question: Problem 1: Using java Implement an application tha...
Bookmark
Problem 1: Using java Implement an application that reads a sentence from user and reverses the sentence. Use a Queue to reverse the sentence.
Sample Output :
Enter a sentence : Hello How are you ?Reversed Sentence : olleH woH era ouy ?Do you have another sentence to be reversed (Y/N) : YEnter a sentence : I am taking my middyReversed Sentence : I ma gnikat ym yddimDo you have another sentence to be reversed (Y/N) : N
2.Write a stack Java application that determines if a string is a palindrome or not ?
Enter a string: mommom is a palindrome.Do you want to test another string (Y/N) : YEnter a string : hellohello is not a palindrome.Do you want to test another string (Y/N) : N
3.
Perform an experimental analysis of the two algorithms given below
public static double[] prefixAverage1(double[] x)
{
int n = x.length;
double [] a = new double[n];
for (int j =0; j < n; j++)
{
double total =0;
for (int i =0; i <=j ; i++)
total += x[i];
a[j] = total / (j + 1);
}
return a;
}
public static double[] prefixAverage2(double[] x)
{
int n = x.length;
double [] a = new double[n];
double total = 0;
for (int j =0; j < n; j++)
{
total += x[i];
a[j] = total / (j + 1);
}
return a;
}
E
make sure the output works thanks everyone
Solution
1)
import java.util.*;
class Queuetest {
public static void main(String[] args) {
String s;
do{
System.out.print(\"Enter Your string:\");
Scanner in=new Scanner(System.in);
String input = in.nextLine();
Queue queue = new LinkedList();
for (int i = input.length()-1; i >=0; i--) {
queue.add(input.charAt(i));
}
String reverse = \"\";
while (!queue.isEmpty()) {
reverse = reverse+queue.remove();
}
System.out.println(reverse);
System.out.println(\"Enter y/n to continue:\");
s=in.next();
}while(s.equalsIgnoreCase(\"y\"));
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
2)
import java.util.*;
class Stacktest {
public static void main(String[] args) {
String str;
do{
System.out.print(\"Enter any string:\");
Scanner in=new Scanner(System.in);
String input = in.nextLine();
Stack stack = new Stack();
for (int i = 0; i < input.length(); i++) {
stack.push(input.charAt(i));
}
String reverse = \"\";
while (!stack.isEmpty()) {
reverse= reverse+stack.pop();
}
if (input.equals(reverse))
System.out.println(\"The input String is a palindrome.\");
else
System.out.println(\"The input String is not a palindrome.\");
System.out.println(\"Enter y/n to continue or not\");
str=in.next();
}while(str.equalsIgnoreCase(\"y\"));
}
}


