Question

In: Computer Science

java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based...

java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based stack to read a sentence from the keyboard, and reverse the order of the letters in each word. Print the initial sentence, as well as the result of the reversal operation. Your Tasks: Using the information given in your textbook, create a class named ArrayBasedStack, that will help you solve the problem given above by providing the stack operations you need for this application. Create a class ReverseCharacters that solves the problem mentioned above using an array-based stack, implemented in the class required above. Note that the order of the words in the sentence remains the same, the scope being to reverse the characters in each word. If you implement all the required methods correctly, the driver program should generate outputs similar to the following:

Solutions

Expert Solution

Short Summary:

  • Implemented the program using stack
  • Attached source code and output

**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

ArrayBasedStack.java

package com.core.java;

//This is an array based stack providing stack operations
public class ArrayBasedStack {

   //Instance variables
   private int top;
   private int capacity;
   private char[] array;
   //Constructor
   public ArrayBasedStack(int capacity) {
       this.capacity = capacity;
       this.array = new char[capacity];
       this.top = -1;
   }
   //Checks if array is full
   public boolean isFull() {
       return this.top == this.capacity - 1;
   }
   //Checks if array is empty
   public boolean isEmpty() {
       return this.top == -1;
   }

   //Pushes an element to stack
   public void push(char value) {
       if(!this.isFull()) {
           this.array[++this.top] = value;
       }
   }
   //Pops an element out of stack

   public char pop() {
       return this.isEmpty()?'\u0000':this.array[this.top--];
   }

   //Reverses the characters in the array
   public String reverseCharacters(char[] str) {

       int n=str.length;
       int i;
       for(i = 0; i < n; ++i) {
           push(str[i]);
       }

       for(i = 0; i < n; ++i) {
           str[i] = pop();
       }

       return String.valueOf(str);
   }

}

ReverseCharacters.java

package com.core.java;

import java.util.Scanner;

//This class is used to reverse the characters in each word in the given sentence
public class ReverseCharacters {

   public static void main(String[] args) {
       //Get user input using scanner
       Scanner input=new Scanner(System.in);
       System.out.println("Enter your sentence below:");
       String str = input.nextLine();
       //Split the sentence by words
       String[] strArr=str.split(" ");
       //Pass every word to reverse
       for(int i=0;i<strArr.length;i++)
       {
           char[] charArr = strArr[i].toCharArray();
           int size = charArr.length;
           //Create ArrayBasedStack instance
           ArrayBasedStack stack = new ArrayBasedStack(str.length());
           System.out.print( stack.reverseCharacters(charArr)+" ");
       }
       input.close();
   }
}

Code Screenshot:

Output:

**************Please do upvote to appreciate our time. Thank you!******************


Related Solutions

Create a project plan on the game or application you are creating. Using java programming. The...
Create a project plan on the game or application you are creating. Using java programming. The project plan should include the following: A description of the game or application The IDE or game engine your plan to use to create the game or app and information on how you are going to develop the game or app If you choose to create a game, how are you going to approach the game design and game development process or if you...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The program will validate the characters in the array to see if they meets a set of requirements. Requirements: Must be at least 6 characters long (but not limited to 6, can be greater) Contains one upper case letter Contains one lower case letter Contains one digit Complete the code attached. Must use pointer while checking the characters. Download Source Lab 3 File: #include using...
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
c++ problem: Use an STL stack to reverse a line of input characters that are read...
c++ problem: Use an STL stack to reverse a line of input characters that are read into a string. Your stack should contain the characters of the user's string. Use getline() for input – it needs to be part of your C++ tool inventory. A note on getline: Suppose I am doing the following - This program reverses a string using the STL stack Enter your string of less than 80 characters followed by an ENTER a string input Enter...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters (a-z|A-Z) and store each in the array. If an invalid character is entered, re-prompt until a valid char is entered. After 10 characters are stored in the array, use a loop to print out all characters in the array from the first to the last element. Then, use another loop starting at the last element to print all characters in the array in reverse...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same nodes, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2 In this exercise, you will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT