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


ADVERTISEMENT

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 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

Modify Internal Object Structure Using Any Design Pattern

Mar 31, 2014

I have a design scenario here which is quite interesting and complex. I have a Java class structure as follows,

class A
{
     class B;
     innerClass B
     {
          List<class C> listofC;
          innerClass C
          {
               String attribute1;
               String attribute2; // Their getter setters
          }
     }
}               

So I have this as an API. Now my challenge is that I need to add one more property to inner class C.  i.e attribute3 in innerClass C. I need to do this without disturbing the code in class A by extending these classes or writing a new wrapper, so I can use class C with new properties .

I hope this should be achievable through any design pattern either at runtime or design time.

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

Design Layout Of Calculator And Add One More Button To Clear Textbox Strings One By One

Mar 30, 2014

I need to design the layout of this calculator and also add one more button that clears textbox strings one by one instead of whole.

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
public class Calculator extends JFrame{
double value1;
double value2;
String operator;
double result;

[Code] .....

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

Trie Data Structure In Java

Sep 27, 2014

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

View Replies View Related

How To Perform Row Operation - Store Data Into 2D Matrix

Sep 18, 2014

I am able to perform column operation but not able to perform row operation because i am not able to store data say a 2 matrix [][]. I need to store the data into a 2-D matrix. Here is my code:

Java Code:

import java.awt.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class colRowRead {

[Code] ....

I tried something like that:

for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++){
matrix [i][j]=textFile.get(i).split(" ");
//System.out.println(matrix[i][j]);
}
}
*/ mh_sh_highlight_all('java');

File col.txt is like this:

Java Code:
5 9 7 1 5
3 6 8 6 8
4 6 7 8 9
9 8 3 5 7 mh_sh_highlight_all('java');

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

Design - How Map Request Parameters Data To Domain Model

May 24, 2014

This is a design question is the same problem in any language.as you do to map the controller to the domain model?We have situations in general larger than ... consider the example objects .

situation.1 - We have a request that has all the parameters of the account ;{ " id" : " 1 " , "name " : "test " , "some " : " xxx " } ............. and other fields .

situation.2 - can request that has to have a certain account parameters , for example in the case of an update;{" id" , " 1" , "name " , " testUpdated "}

situation.3 - We have a request that has some parameters of the account , others have more like id as user together;{ " id" : " 1 " , "user " : " xxx " , "service " : " yyy " } in which case each piece of the request will turn an object .

Java Code:

public class Account {

private Long id;
private String name ;
private String some ;

} mh_sh_highlight_all('java');

I see a few options ;

1 - Can I get AccountForm in the controller and set the properties for the Account object and others in CONTROLLER ;

+ ok for situation.1 situations 2, and situation.3

+ Separates the requisition of the object domain

- Pollutes the controller with code conversion

- Controller is full of setters .. if a higher class as a large object as a request is very confusing .

Java Code:

controller ( AccountForm from ) {
Account account = new Account ( )
account.setNome form.getNome = ();
account.setSome form.getSome = ();
Other outher = new Other ( ) ;
other.setSome ( form.getSome ( ) ) ;
} mh_sh_highlight_all('java');

2 - Can I get AccountRequest in the controller and have a method in itself as AccountRequest.getAccount ( ) to return a mapped model , in this case the mapping is at own Request object .

+ Separates the requisition of the object domain

+ Encapsulates the conversion in a place with easy access .

+ Meets situation.1 situation.2 and situation3 ;

- Request object has two responsibilities represent the request and map to a valid model .

Java Code:

controller ( AccountForm accountRequest ) {
Account account = accountRequest.getAccount ( ) ;
Outher outher accountRequest.getOther = ( )
} mh_sh_highlight_all('java');

3 - Can I get the controller Direct Account which had been filled with nulls .

+ Eliminate object request

- Serves only situation.1 situation.2 .

Java Code:

controller (Account account ) {
account.someMethod ();
} mh_sh_highlight_all('java');

4 - Outsource this mapping request parameters to another object mapper for request ..

+ Isolates logic mapping

- Until complexity for simpler cases are used as standard for all such a find by id .

- One more class for each request ;

In the case of API gets worse response has two further classes. speaking in terms of request for response .... AccountRequest, AccountRequestMapper, Account, AccountResponseMapper, AccountResponse .....I'm doing more testing the Hybrid option 3 for simple cases (find ID or updates) .... with option 2 for example for more complex cases ..

View Replies View Related

Using Loops To Do Multiple Divisions

Feb 2, 2015

I am trying to write a program that will take two inputted numbers (a and b) and determine how many divisions can be done by dividing by b until the quotient is less than b. For example if a=64 and b=2, then you would be able to do:

64/2=32
32/2=16
16/2=8
8/2=4
4/2=2
2/2=1

1<2

so there is 6 divisions.

How would I use a loop to create this type of program?

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 Perform Soft Delete

Feb 25, 2014

how to do soft delete?

View Replies View Related

How To Print A Different Statement In If Else Structure

Feb 6, 2015

Implement a Las Vegas slot machine! The machine works as follows. First, it generates three random integers (use import java.lang.Math.*; then call Math.random()*7 to generate a random number between [a,b)) that are in the range of 0-7. Once the numbers are generated, the following rules are used to determine the prize:

- If all three numbers are equal to 7, you are winning $1,000,
- If all three numbers are equal, but not equal to 7, you are winning $500,
- If two of the numbers are equal to 7 and the third one is six, you are winning $400,
- If two numbers are equal, you are winning $100,
- Otherwise you are not winning anything.

And for that I wrote:

import java.lang.Math;

public class Assn1_2150130 {
public static void main(String[] args) {
// Generate three random signle-digit integar from 0-7.
int n1 = (int)(Math.random()*7);
int n2 = (int)(Math.random()*7);
int n3 = (int)(Math.random()*7);

[code]...

But I just can't figure out a way to print out the "YOU WON NOTHING." independently.If I say that n1!=n2 && n2!=n3 && n3!=n1, and then write another line of println. It gives out the number as well as the "NOTHING".

View Replies View Related

Tree Structure Of Folders

Mar 21, 2014

I have been trying to make a good tree structure for my Electrical Distribution network.. How to create two "subfolder" of my Voltage level folder.

Here is the code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;

[Code] ....

View Replies View Related

How To Modify If Else Structure To Get Output

Nov 3, 2014

I'm having trouble with this program:

//********************************************************************
// Demonstrates the existence of separate data space in multiple instantiations of a programmer-defined class.
//********************************************************************

[code]....

Basically i'm trying to add one more "coin" to flip. My problem is that my if-else structure isn't working correctly here's what it looks like:

if (count1 < GOAL)
if (count2 < GOAL)
System.out.println("Coin 3 Wins!");

[Code] .....

It only works correctly when "coin2" wins.How would I modify my if else structure to get the output I am looking for?

View Replies View Related

JSF :: Possible To Map URL With Independent View Dir Structure?

Feb 16, 2015

Is it possible to map URL's with independent view dir structure? If so how?

My goal is to simply point an url to a specific view file:

i.e.

[URL] ....

to
not/default/path/views/main/index.xhtml
and
www.myserver.com/application/admin/
to
not/default/path/views/admin/index.xhtml

A couple solutions I found so far where:

Solution 1

PrettyFaces

I just didn't wanted to use a third party solution. This is my best solution so far.

Solution 2(a JSF solution):

navigation rule entries in the faces-config.xml file.

A bean is required, not a bad thing in above example but not great in combination with static navigation like:<h:commandButton action="index"/>

Solution 3 (another JSF solution)

Resource Library Contracts
This forces me to work in the contracts file.

None of the solutions gave me a clear solution except prettyfaces.

Are there some elegant native JSF solutions ? Something flexible and reusable? Something like a mvc controller i used to use in php applications. In this controller I was able to add a template file in the constructor and an content file in specific function. Custom paths where no problem. I didn't want to use mvc nor the php language in this project.

View Replies View Related

JavaFX 2.0 :: Using CSS Sub-structure On Own Nodes

May 26, 2014

I have noticed that you can style JavaFX controls using their sub-structure.

E.g. to style the label in a table column header to be left justified I can do this :
 
.table-view .column-header-background .label {    -fx-alignment: center-left ;  } 
 
How can I style my own nodes/controls doing this? Basically it amounts to asking how the selectors like ".column-header-background" are associated with sub-structures of my Java control/node objects. So suppose I have something like
 
.fancy-node .fred .label { ... }
 
How does JavaFX associate fancy-node and fred with something in my implementation of a Node/Control?

View Replies View Related

While Loop To Perform Multiple Steps

Mar 20, 2014

Write a program that uses a while loop to perform the following steps: Comment by labelling each part: //Part A, //Part B, etc...

A.)Prompt the user to input 2 integers: firstNum and secondNum. Use 10 and 20.
B.)Output all odd numbers between firstNum and secondNum.
C.)Output the sum of all even numbers between firstNum and secondNum.
D.)Output the numbers and their square between 1 and 10.
E.)Output the sum of the square of odd numbers between firstNum and secondNum.
F.)Output all uppercase letters.

Again I am new to while loops and I am totally lost. I have just completed 8 other programs using if else statements and now trying to get the hang of loops.

import java.util.Scanner;
public class whileLoop {
public static void main(String[] args)

[code]...

View Replies View Related

Write A Method That Uses For-loop To Perform?

May 19, 2014

which when user enter a number then will display how many * in a line .Produce a line: ***** But my code only show 1 * there... what wrong with it ?

import java.util.Scanner;
public class display {
public static void main(String args[]) {
int i,n = 0 ;

[Code] ...

View Replies View Related

How To Get One JButton To Perform Sequential Actions

Aug 18, 2014

How to get my program to display a piece of text (upside down) on the first button press, and then display it right-side up on the second button press. I've looked all over the internet and I can't seem to find a example that fits my situation.

Here is the code:

package org.CIS407.Lab10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JUpsideDown extends JFrame
implements ActionListener

[Code] ....

View Replies View Related

How To Perform Calculations Using Stacks Java

Nov 23, 2014

I am doing a calculator using stacks but when i try to calculate I getting the wrong data example stack contains 8 and user enter -3 stack should change to 5.

package comp10152.lab5;
import java.util.Scanner;
import java.util.Stack;

[Code].....

View Replies View Related







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