Question

Please help with program this. Thank you so much in advance!

Create a Java program which implements a simple stack machine. The machine has 6 instructions Push operand Puts a value on thHints Javas Stack class works great for this. Google Java stack class for documentation and examples You can use the seconPush M1 Push 1.0 Sub Pop M2 Push 3.14 Push M1 Mul Pop M3 Push M3 Push 2.0 Div Pop M4

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Example.java

package abc4;

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import java.util.Stack;

public class Example {
  
   public static void main(String args[]) throws Exception{
       File file = new File("siva.txt");
       Scanner br = new Scanner(new FileReader(file));
      
       Stack<Float> stack = new Stack<Float>();
      
       float Memory[] = new float[10];

       while(br.hasNext()){
           String cmd = br.nextLine();
           if(!cmd.equals("")){
               if(cmd.startsWith("Push")){
                   if(cmd.charAt(5) == 'M'){
                       stack.push(Memory[Character.getNumericValue(cmd.charAt(6))]);
                   }else{
                       stack.push(Float.parseFloat(cmd.substring(5)));
                   }
               }
               else if(cmd.startsWith("Pop")){
                   if(cmd.charAt(4) == 'M'){
                       Memory[Character.getNumericValue(cmd.charAt(5))] = stack.pop();
                       System.out.println(Memory[Character.getNumericValue(cmd.charAt(5))]+" Stored in location M"+Character.getNumericValue(cmd.charAt(5)));
                   }else
                       stack.pop();
               }
               else if(cmd.startsWith("Add")){
                   float t2 = stack.pop();
                   float t1 = stack.pop();
                   stack.push(t1+t2);
               }
               else if(cmd.startsWith("Sub")){
                   float t2 = stack.pop();
                   float t1 = stack.pop();
                   stack.push(t1-t2);
               }
               else if(cmd.startsWith("Mul")){
                   float t2 = stack.pop();
                   float t1 = stack.pop();
                   stack.push(t1*t2);
               }
               else if(cmd.startsWith("Div")){
                   float t2 = stack.pop();
                   float t1 = stack.pop();
                   stack.push(t1/t2);
               }
           }
       }
   }

}

In this program I have used java's stack class.

Add a comment
Know the answer?
Add Answer to:
Please help with program this. Thank you so much in advance! Create a Java program which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • in java please withe comments & check output Write a program that contains: Client Thread class...

    in java please withe comments & check output Write a program that contains: Client Thread class that send full operation ("1+2", "1-2", "1*2", " 1/2") to CalculaterServer Server Thread class named CalculaterServer, the server create thread to receives the request and decode the operation and call the appropriate method (add: sub, mul div) which are in class named Calculator to do the operation, and then sends back the result to client. Client show the following window: After choosing operation ask...

  • Assignment 4 File “quad_sol.s” contains a quadratic polynomial solver, which calculates the integer solution of a quadratic polynomial equation. 1. Rewrite the program using instructions reordering to...

    Assignment 4 File “quad_sol.s” contains a quadratic polynomial solver, which calculates the integer solution of a quadratic polynomial equation. 1. Rewrite the program using instructions reordering to reduce the number of cycles needed to execute the program. Indicate the number of cycle reduction. 2. Describe how forwarding would affect the execution of the program. CODE # quad_sol.s # This assembly program calculates the integer solutions of a quadratic polynomial. # Inputs : The coefficients a,b,c of the equation a*x^2 +...

  • Need help with java In this program you will use a Stack to implement backtracking to solve Sudok...

    need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class.  Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop()  Test your class thoroughly before using it within the soduku program Part II. Create...

  • Please help JAVA Create the following in Stack.java: An array of ints set to size 10...

    Please help JAVA Create the following in Stack.java: An array of ints set to size 10 to represent the Stack, an int variable for the index A constructor that sets the index to -1 A method called isEmpty (returns a boolean) that has no parameters. This method returns true if the Stack is empty and false if the Stack is not empty. true A method called push(returns a Boolean) that takes an int as a parameter. This method pushes, or...

  • C++ programming help. The only change you have to make is in current program, where it...

    C++ programming help. The only change you have to make is in current program, where it says "your code goes here", do not make any changes to any other files, they are just there to show you. This code should be written for arbitrary data, not specifically for this sequence of data. The only place you need to write a code is marked YOUR CODE GOES HERE. You have been supplied a file with data in it (data2.txt). The line...

  • JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array...

    JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...

  • In a sensible language ( any of C#/Java/Python) write a program which correctly calculates add, subtract,...

    In a sensible language ( any of C#/Java/Python) write a program which correctly calculates add, subtract, multiply and divide using our ‘minifloat’ binary format using an algorithm you code yourself.  Some details: Your program only needs to work on two ‘numbers’ at a time, read those in from a text file – failure to read those values from a text file will result in a grade of 0.   For each ‘number’ store the sign, exponent and mantissa separately.   You can hard code your...

  • Hi, anyone can help me? thank you so much Step 7K: Create a Program Create a...

    Hi, anyone can help me? thank you so much Step 7K: Create a Program Create a small program to turn on and off the conveyor utilizing buttons/tags on the HMI, not the hardwired switch Remember you SHOULD NEVER energize an "Input" data file bit, or use it as an internal relay. You will be utilizing your "binary" data file to create internal relays Use two buttons, 1 for start, 1 for stop Latching of the operation is required, but not...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT