Question

In: Computer Science

Please write in Java and have two methods: the main method and the reverse word Write...

Please write in Java and have two methods: the main method and the reverse word

Write a method that reads a line and reverses the words in the line (not the characters) using a stack. For example, given the following input:

The quick brown fox jumps over the lazy dog

you should get the following output:

dog lazy the over jumps fox brown quick The

Then create a main method to prompt the user to enter a line of words to be reversed using the previous reverse method.

Solutions

Expert Solution

The required code and corresponding output are as follows. The code is well commented for better understanding.

//Java program to reverse sentence
import java.util.*;
class reverseSentence{
 
// Function to reverse the words
// of the given String
// using stack. 
static void reverseWord(String s)
{
  // Create an empty String stack
  Stack<String> stk = new Stack<String>();
 
  // Create an empty temporary String
  String temp = "";
 
  // Traversing the entire String
  for(int i = 0; i < s.length(); i++)
  {
    if(s.charAt(i) == ' ')
    {
       
      // Push the temporary 
      // variable into the stack
      stk.add(temp); 
       
      // Assigning temporary 
      // variable as empty
      temp = "";          
    }
    else
    {
      temp = temp + s.charAt(i);
    }
 
  }
 
  // For the last word 
  // of the String
  stk.add(temp);
 
  while(!stk.isEmpty()) 
  {
    // Get the words in 
    // reverse order 
    temp = stk.peek();
    System.out.print(temp + " ");
    stk.pop();
  }
   
  System.out.println();
}
 
//Driver code
public static void main(String[] args)
{
  String s;
  Scanner sc = new Scanner(System.in);
  System.out.println("\nEnter a line of words to be reversed:\n");
  s=sc.nextLine();//reads sentence 
  System.out.println("\nThe reversed line is:\n");
  reverseWord(s); //calls function
}
}
 

Output:


Related Solutions

Write a java program with 3 recursive methods that reverse the order of a string. The...
Write a java program with 3 recursive methods that reverse the order of a string. The first recursive method should reverse the order starting from the left selecting the leftmost character as the head and the remaining part of the string as its tail., the second starting from the right selecting the rightmost character as the head and the remaining part of the string on its left as the tail, and the last a recursive method starting from the middle...
Write a java program to reverse element of a stack. For any given word (from input),...
Write a java program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be the same as the input. Your program should be using a stack and a queue to complete this process. 1. Push into stack 2. Pop from stack 3. Enqueue into queue 4. Dequeue from queue 5. Push into stack 6. Pop from stack and display java
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should...
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator. B) Write methods to implement the multiply, subtract and divide operations for integers. The results of all of these are integers. Use only the add operator.
write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
PLEASE USE ARRAYS IN JAVA TO ANSWER THIS Write a 'main' method that examines its command-line...
PLEASE USE ARRAYS IN JAVA TO ANSWER THIS Write a 'main' method that examines its command-line arguments and calls the (add) method if the first parameter is a "+" calls the (subtract) method if the first parameter is a "-" calls the (doubled) method if the first parameter is a "&" add should add the 2 numbers and print out the result. subtract should subtract the 2 numbers and print out the results. Double should add the number to itself...
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
Java coding: 2. Write a method which takes a list list of int , and reverse...
Java coding: 2. Write a method which takes a list list of int , and reverse it. // recursion 3.Write a method which takes a list list of strings , and reverse it. // in different way than the previous 3. Write a two methods which take a list and find the largest integer number in it.
Design a complete JAVA program with the following methods:- In the main method : 3 variables...
Design a complete JAVA program with the following methods:- In the main method : 3 variables are declared :- employee id, salary and status. A method that will allow user to input employee id and salary. A method that will assign status to “Taxable’ if employee’s salary is more than 2500RM or “Not Taxable” if salary is less and equal to 2500RM. A method that will display the employee’s id , salary and its status. Write the above Using Java.
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT