Question

In: Computer Science

Assume all methods in the Stack class are available to you, request from the user a...

Assume all methods in the Stack class are available to you, request from the user a set of numbers (terminated by -1) and print them in reverse order . write code in java.

Solutions

Expert Solution

Explanation:

Here is the code which creates a Stack s and then push all the elements entered by the user until the user presses -1 , to the stack.

Then, it keeps popping the elements one by one and they get printed in the reverse direction.

Code:

import java.util.Stack;
import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       Stack<Integer> s = new Stack<Integer>();
      
       while(true)
       {
       System.out.print("Enter Number: ");
       int n = sc.nextInt();
      
       if(n==-1)
       break;
      
       s.push(n);
       }
      
      
       while(!s.empty())
       {
       System.out.print(s.peek()+" ");
      
       s.pop();
       }
      
      
   }
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
#data structures Assume you are given an implementation of the Stack class. Recall that it has...
#data structures Assume you are given an implementation of the Stack class. Recall that it has methods push(), pop(), top(), isEmpty() and isFull(). Write an Integer function with prototype: public static Integer popBottom(Stack<Integer> stk); Note that it has one parameter, a single stack object stk. The function should delete the item at the bottom of the stack and return this item as the value of the function. If the stack is empty return null. When the function returns the stk...
a-Assume you have a stack of integers. You want to organize it such that all numbers...
a-Assume you have a stack of integers. You want to organize it such that all numbers that are smaller than -100 go to the bottom, numbers between -100 and +100 in the middle and numbers larger than 100 in the top. Write a Java code that uses no more than three additional Stacks to solve the problem. NB: You do not need to write the code for Stacks, you are using a Stack from the library with a name SStack...
• 2. Get all the information from the user using methods Java • B. if the...
• 2. Get all the information from the user using methods Java • B. if the inputs are not given in the proper format the program should prompt user to give the proper input (eg. Name cannot be numbers, age cannot be String)
In this project you will implement the DataFrame class along with the all the methods that...
In this project you will implement the DataFrame class along with the all the methods that are represented in the class definition. DataFrame is a table with rows and columns – columns and rows have names associated with them also. For this project we will assume that all the values that are stored in the columns and rows are integers. After you have tested your class, you have to execute the main program that is also given below. DataFrame Class...
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
For this question you need to write some methods and class headers. (a) Assume that you...
For this question you need to write some methods and class headers. (a) Assume that you have written a Rectangle class with instance variables length and width. You have already written all set and get methods and area and perimeter methods. Write an equals() method that takes Object o as a parameter. The method should return true when the Object o is a rectangle with the same length and width. Answer: (b) A class named Fruit implements an interface called...
You are to create a program to request user input and store the data in an...
You are to create a program to request user input and store the data in an array of structures, and then display the data as requested. The data you are collecting refers to a set of images. The images are OCT images taken of the lining of the bladder. The data you provide will help diagnose the image as cancerous or not, but your code does not need to do that at the moment. First, Define a global structure that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT