Java Calculator Using Stack Data Structure

May 21, 2014

My assignment is to design a simple GUI calculator using the stack data structure to perform additions, subtractions, multiplications and divisions. But i having error while i press the action there.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.Stack;
public class JCalculator implements ActionListener {

[Code] ...

View Replies


ADVERTISEMENT

Design Calculator Using Stack Data Structure To Perform Additions / Subtractions / Multiplications And Divisions

Jul 18, 2014

I've beginning a new assignment and would like some feedback on my outline for the program. You are to design a simple calculator using the stack data structure to perform additions, subtractions, multiplications and divisions. The user may enter an arithmetic expression in infix using numbers, parentheses and arithmetic operations (+, -, *, /). After an expression is entered, its postfix or prefix notation is displayed and then the result of the expression. You are required to design your own infix to postfix and infix to prefix conversions.

The professors example includes: SinglyLinkedList implementation, a Stack that implements Stack interface and extends SinglyLinkedList, a Node class, and StackApp -- in addition to the main class and the prefix/post-fix classes.

(The equation is entered/stored as a string variable)
System.out.println("Enter equation: ");
equation = input.next();
equation.trim();

(1.) Get the user input:
- evaluate the user input and use a stack to determine the correct prefix/post-fix notation.
- from the stack, pop each item:
a. account for '(' and ')' to push all operators and write all operands until ')', then pop/write
b. store new equation into a list (in prefix/postfix notation)

(2.) Display the list in both notations to the user.

(3.) Perform the calculation.Do you think it will be alright to calculate the original equation that was entered? It seems too much to then design a class that will then cycle through the polish notation and calculate according to that format.

View Replies View Related

Checking Parentheses Using Stack Data Structure

May 4, 2015

I am trying to Implement a method matchBracket(String exp), which should take a String expression consisting of only symbols (,[,{,<,),],},> , and checks if the brackets in the expression are matching using stack data structure.

I know how the methods of stack class like push,pop,peek are for however how to do the checking for opening and closing brackets iteratively ?

public static void matchBracket(String str){
// creating a stack of character at first
Stack <Character> stack = new Stack<Character>() ;
// starting loop to scan the expression below inside main method
for(int i = 0; i<str.length(); i++){

[Code] ....

/**Expected Output:
*The parentheses are not matching
*The parentheses are matching

View Replies View Related

Trie Data Structure In Java

Sep 27, 2014

Problem in inserting,removing and searching in trie data structure.

View Replies View Related

Having Stack Calculator Error

Jun 3, 2014

I was cannot do the action with + - * /.so i cannot have an output.where is the problem ?

Java Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.Stack;

[code]....

View Replies View Related

Simple Data Classes - Serialize More Complex Data Structure

Oct 6, 2014

I have written several simple data classes that I serialized manually by converting to text. At this point I need to serialize a more complex data structure. which will include lists of the simpler elements. Can serialize store and reconstitute this type of structure automatically or do I need to do this one manually as well? Consider the pseudocode below for a clarification of my question;

Java Code:

Class Hops{
String Name;
float Alpha Acid;
float Beta Acid;
};

Class Malt{
String Name;
float extract;
};

Class Recipie{
String Name;
CList HopList;
CList MaltList;
};

CList RecipieList; mh_sh_highlight_all('java');

I read the article on Serialization presented at tutorial point, but the example only showed a simple class, not lists of class members. What I want to do is serialize RecipieList, which consists of a CList of Recipies, which in turn consist of CLists of various ingredients.

View Replies View Related

Defining Map Data Structure

May 27, 2014

I want to define this data structure , I show you an example

"first street",{{1,40},{43,45},{34,26}},
"second street",{{14,41},{42,5},{3,46}},
............

I wrote in this way but it doesnt work :

Map<String,ArrayList<int[]>> Street=new HashMap<String, ArrayList<int[2]>>();

View Replies View Related

Data Structure Construction

Feb 27, 2014

I'm trying to fill a List<String>, named overlappedGrid, from another List<String>, named listGrid. I'm facing an exception:Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unexpected type

required: variable
found: value
at arab2004_d.ProcessingLayer.printShape(Arab2004_D.j ava:101)
at arab2004_d.Arab2004_D.main(Arab2004_D.java:30)

Java Result: 1

Here is my source code (the exception line is commented "here is the exception"):

package arab2004_d;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

[code]....

I tried to copy listGrid in another List<String> but the exception remain the same.

View Replies View Related

Display Data Structure With JTree In A Swing Application

Aug 28, 2014

I have a tree-based data structure that looks somewhat like this:image1.png..Every tree always has a height of no more then 3, and each of these classes has a very specific use, generalization is not possible between them.Now, I want to display my data structure with a JTree in a Swing application. A JTree needs a TreeModel and so I created a custom TreeModel for my classes, it looks like this:

public class CustomTreeModel implements TreeModel {
private Root root;
public Object getRoot() {
return root;

[code]....

But then I would introduce this interface to my Model classes just for the purpose of writing the View. So my data model has to be altered to make the View work, this sounds completely against MVC to me and also like something I dont really like.On the other hand, the CustomTreeModel would be much simpler, easier to understand, easier to maintain and just in general more pleasing to the eye.Should I change my Model to improve the View? Is instanceof okay to be used in View classes?

View Replies View Related

Peterson Lock Implemented For N Threads Using Binary Tree Data Structure

Feb 8, 2014

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
public class tree_lock_test{
int total_instances;
int thread_instances = 0;
int N;

[Code] .....

this is compiled with another Peterson class which has implemeted peterson lock for two threads ...

View Replies View Related

Method That Print Data Of Single Linked List Backward Using Stack

Apr 23, 2015

I am trying out solving the question but i am stuck.The problem is to write a method that print data of single linked list backward using stack.The question is as follow

public class Stack{
public boolean isEmpty(){};
public void push(int n){};
public int peek(){};
public int pop(){};
}

public class node{
int data;
node next;
}

public class list{
node first;
}

View Replies View Related

How To Structure Simple Java Programs

May 8, 2015

how to structure simple programs (i.e. one-method algorithms for a simple purpose). There are a few different ways that I have been doing it, but I want to be consistent. Should I put both the one-method algorithm (e.g. a factorial function) and the main method that executes the algorithm into the same class and then export it to an executable .jar file for use? Or should I create one class for the algorithm and another for the main method that executes the algorithm? In addition, is there any reason that I should out these classes in a package before I export it?

As another similar question, if I have constructed two distinct classes with two separate purposes, and they are both used in the construction of a single program, then would it be best to just put the main method in a third, separate class or should I put it in one of the two classes?

View Replies View Related

Parsing Tree Structure In Java

Jun 26, 2015

I have an excel file with data listed as the following, i m trying to parse down using POI
 
A
     B
          C
               D1
               D2
          F
               G1
               G2
               G3
          M
               S1
     R
          T             
     U
L
  X
     Y
         Z
 
is it possible to generate an output like the following
 
A
A-->B
A-->B-->C
A-->B-->C-->D1
A-->B-->C-->D2
A-->B-->F
A-->B-->F-->G1
A-->B-->F-->G2
A-->B-->F-->G3
A-->B-->M
A-->B-->M-->S1
A-->R
A-->R-->T
A-->U
L
L-->X
L-->X-->Y
L-->X-->Y-->Z
 
I have been trying from quite some time but havent figured out the logic ...

View Replies View Related

Build Tree Structure From Array In Java

May 31, 2014

I never used tree, node etc. Consider an array of strings that come from html tags. This needs to be turned into a tree structure. Here, any Hn is the child of the most recent Hn-1

String[] Headers = {"H1", "H1", "H2", "H3", "H3", "H2", "H2", "H3",
"H4", "H2", "H2", "H2", "H1", "H2", "H2", "H3", "H4", "H4", "H2" };

Desired output -

ROOT
H1
H1
...H2
......H3
......H3
...H2
...H2
......H3
.........H4
...H2
...H2
...H2
H1
...H2
...H2
......H3
.........H4
.........H4
...H2

ANSWER -

class Example {
static class Node {
final String name;
final int indent;
Collection<Node> children = new LinkedList<> ();

[Code] .....

View Replies View Related

Java Program To Implement A Single Linked List Structure

Jul 27, 2014

I'm trying to build a program that contains the ability to:

(1) insert new node at head,
(2) print out contents of the list in order,
(3) remove first node from head,
(4) remove last node from tail,
(5) find a target value,
(6) give total number of occurrences of a target value, and
(7) give total number of items in list.

The areas I'm struggling with implementing are: (

- remove from tail - I know how to find the final node but I can't quite figure out how to set it to null since its initial type is an integer.
- find a target value - how to make the parameters quite workout so the user can simply input an integer value.
- The solution is probably really simple but I can't figure out how to print out the results of these methods when I call them.

public class Node
{
private int data;
private Node link;
// Node Constructor 1
public Node()
{
data = 0;
link = null;

[code]....

View Replies View Related

Implementation Of Stack Methods In Java

Jun 3, 2014

I have to implement all the stack methods in java such as push, pop empty, not using the ready methods but have to create them and to execute an exercise but is sth wrong with it

public class Stiva {
/** the problem is here how to declare the stack 1 and stack 2 and kreu(head) gjmax(size)*/
int Gjmax;
int array[] = new int[Gjmax];
int kreu;
private Stiva stiva1;
private Stiva stiva2;

[Code] .....

View Replies View Related

Implementation Of All Stack Methods In Java

Jun 3, 2014

I have to implement all the stack methods in java such as push, pop empty, not using the ready methods but have to create them and to execute an exercise but something wrong with it....

public class Stiva {
/** the problem is here how to declare the stack 1 and stack 2 and kreu(head) gjmax(size)*/
int Gjmax;
int array[] = new int[Gjmax];
int kreu;
private Stiva stiva1;
private Stiva stiva2;

[Code] .....

View Replies View Related

Java Computing Height Of Stack

Jun 2, 2015

Compute the height of a stack. Then determine whether that value is found in the stack element .

View Replies View Related

Java QuickSort Stack Overflow Error

Jan 19, 2014

I have an error using quicksort and this is a project ... The error occurs for numbers such as 7500 and bigger ...

Exception in thread "main" java.lang.StackOverflowError
at QuickSort.QuickSort(QuickSort.java:45)
at QuickSort.QuickSort(QuickSort.java:46)
at QuickSort.QuickSort(QuickSort.java:46) ...

Java Code:

import javax.swing.JOptionPane;
public class QuickSort{
public static void main(String[] args){
int p=new Integer(JOptionPane.showInputDialog("Jepni numrin e kufizave: "));
int[] ListaNumrave= new int[p];
//QuickSort Zbrites

[Code] .....

View Replies View Related

Palindrome Program - Java Stack And Queues

Aug 13, 2014

I am trying to figure out stacks and queues and was trying to get this Palindrome program working so I could then play with it and use the Java visualizer site but for some reason the program isn't working correctly. It always states that the input is a palindrome no matter what the user input is.

The book that I got the code from is a little old so I changed a couple small things that I thought needed updating like adding scanner. I wanna use one with a custom array based stack and queue class rather than the java.util.Stack and Queue interface, just for understanding stacks and queues better hopefully.

import java.util.Scanner;
import javax.imageio.IIOException;
public class PalTest {
public static void main(String[]args) throws IIOException
{
Scanner scan = new Scanner(System.in);
PalindromeTesting x = new PalindromeTesting();

[Code] .....

View Replies View Related

Java Tree Structure - Build Tree Based On Traversal Results

Jun 17, 2014

How to do draw the original binary tree based on traversal results?

A binary tree has this pre-order traversal result: A,B,D,H,I,E,F,C,G,K,J (TreeNodes) And the same tree gives the following in-order traversal: B,H,I,D,A,C,F,E,K,G,J. Can you draw the tree structure?

View Replies View Related

Calculator In Java

Oct 28, 2014

I can not this to work correctly.

public static void main(String[] args) {
FloatWrapper x1=new FloatWrapper(0);
FloatWrapper x2=new FloatWrapper(0);
do{
switch(menu()){
case 'a': if(dataInput(x1,x2)) add(x1,x2); break;
case 'b': if(dataInput(x1,x2)) subtract(x1,x2); break;

[code]....

View Replies View Related

Make A Calculator Using Java GUI

Jul 29, 2014

I am trying to make a calculator using Java GUI. I've managed to make an ActionListener and add it to a button, but I've made an error in my code that I'm unsure of how to solve. Because of how I've written the code, only one number can be placed in the text field. For example, the an ActionListener for the three button on the calculator was added to the button, but no matter how many times the user presses the button, only one 3 will appear in the text field. The code is below:

import javax.swing.*;//import the packages needed for gui
import java.awt.*;
import java.awt.event.*;
public class Calculator {
public static void main(String[] args) {
JFrame window = new JFrame("Window");//makes a JFrame
window.setSize(300,350);

[code].....

As you can see, because the compiler forces the String variable to be final, so when the user presses the button, the code simply shows how a space character and three character would look like, because the String variable can't change. How do I write my code so that every time the user presses the button, a character is added to the text field?

View Replies View Related

Build A Calculator In Java?

Aug 28, 2014

I wanted to build a calculator in java but I have a problem I wanted to when I type "plus" , go to This IF but don't go

import java.util.Scanner;
public class Session {
static String amal;

[code]....

View Replies View Related

Grading Calculator In Java Program?

Sep 26, 2014

i have a similar problem but what i need to do is to detect an exused absent of which in my notepad is represented by 00 and 0 being an unexcused absent, anyway if a student has an excused absent example a quiz for that day will be deducted from the total quizzes example if i was the teacher and had 2 quizzes of 10 items each and she was absent in one of the quizes instead of let say her score was 9 on the first quiz it should go 9/10 and if it was an unexcused absent 9+0/20 anyway this is my java code and notepad example

import java.io.*;
import java.util.*;
import java.lang.*; 
public class GradingSystem {
public static void main (String[] args) throws FileNotFoundException

[code]...

View Replies View Related

GPA Calculator In Java Using Loops And Switch

Apr 25, 2015

I am trying to make GPA calculator. I decided to use only switch statement and make it as simple as possible. The point is to enter as many classes and students as user wants. I keep getting credit points 0 at the end and also text format is not good too. Here is the code.

import java.util.Scanner;
import java.io.*;
public class SwitchExercise{
public static void main(String[] args) {
String grade = "";
String grades="";
String coursename="";
String headingleft="COURSES";

[code]....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved