Postfix Calculator Doesn't Compute Negative Numbers

Nov 16, 2014

My verify method also always returns false. So I'm given three classes to begin with. Calculator, Expression, and InfixExpression and they are listed below.

The goal is to create a class called PostfixExpression that extends Expression and can read and calculate postfix expressions.

My evaluate() method works for most calculations but when it needs to return a negative value it just returns the positive equivalent.

Also, my verify method always returns false and I can't pinpoint why.

Here's my current code. Some things are commented out for debugging purposes.

import java.util.Scanner;
/**
* Simple calculator that reads infix expressions and evaluates them.
*/
public class Calculator
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

[Code] .....

View Replies


ADVERTISEMENT

PostFix Calculator Project?

Apr 2, 2015

So I am working on a PostFix calculator that is used in command line for a class project, and I am having a little trouble on developing a memory for it. I have been told to create a hashMap and I have researched it and understand the basics of it. I have the calculating method working, but what I am having trouble trying to implement a way for the user to declare variables. For example this what the user should be able to do:

> a = 3 5 + 1 -
7
> bee = a 3 *
21
> a bee +
28

[code]....

I just need to be able to save the answers as a variable the User names, such as the example and let the user be able to use the variables later.

View Replies View Related

Generate Two Arrays - One With All Positive Numbers And Another With Negative Numbers

Mar 10, 2015

Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.

public class problem3 {
public static void main(String[]args){
int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5};
for (int i = 0; i<numbers.length;){
if(i>0){
System.out.println(numbers);
}
else
System.out.println(numbers);
}
}
}

View Replies View Related

Compute Standard Deviation For A Set Of Numbers - ClassCastException Being Thrown?

Apr 20, 2015

Basically I'm trying to compute the standard deviation for a set of numbers and I'm using previously know data to compute this. The data I'm working with is here (note all of this is fake as its for an assignment):

Id,First Name,Last Name,Quiz1,Quiz2,Quiz3,Lab1,Lab2,Assignment1,Exam1,Exam2
58136,Wade,Andrews,5,8,2,2,20,47,20,40
59759,Jacqueline,Simmons,9,4,1,16,13,2,82,41
28056,Frederick,Castro,8,0,6,3,6,29,53,63
15532,Annette,Myers,2,6,0,14,13,4,77,43

Next this is the code that I know is producing the error:

/**
* Pass in original data from files, and pass in computed grades as arraylist. this returns all of the standard deviations to be printed
*/
public static ArrayList calcStandDev(String data1[], ArrayList<Double> grades, ArrayList<Integer> averages)
{
double standDev = 0;
int j = 0;
//gets rid of first element in string array, makes processing MUCH easier
String[] data = Arrays.copyOfRange(data1, 1, data1.length);
 
[Code] ....

Look for the line with all the ***'s, it is producing the "java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer".

The reason I dont undersand why its producing this error is because temp.get(i) should be a integer - averages.get(i) (is type double), I do believe automatic up casting is present here making them both a double then I multiply it by itself producing a double? I dont understand where the casing is occurring. And just incase you want the formula I'm using for standard deviation here it is:

((x1 - ave)^2 + (x2 - ave) ^2 + (xn - ave) ^ 2) / (n-1)

then squareroot above

Why I'm getting this error! I tried changing a few dataTypes but I just cant avoid exceptions being thrown everywhere .

View Replies View Related

Negative Numbers In Binary

Mar 7, 2014

I need understanding why

1111 1101 = -3

View Replies View Related

Finding Binary Of Negative Numbers?

Mar 22, 2014

How do u find the binary of negative numbers? I already did it for positive numbers,?

View Replies View Related

How To Avoid Getting Negative Numbers Of Coins

Oct 18, 2014

how to avoid getting negative numbers of coins, use casting and mod to show how many quarters, dimes, nickels, and pennies there are?

import java.util.Scanner;

public class VM
{
public static void main(String[] args)
{
//money deposit
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Input Validation For Negative Numbers

Feb 20, 2014

I am very new to Java. I have been working for a couple months on a program for school. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative. I have included my current code, the program works perfectly, but what to do about the negative numbers.

Java Code:

package gradplanner;
import java.util.Scanner;
public class GradPlanner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numofclasses = 0;
int totalCUs = 0;

[Code] ....

View Replies View Related

Input Validation For Negative Numbers

Feb 20, 2014

I am very new to Java. I have been working on a program. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative.I have included my current code, the program works perfectly, but what to do about the negative numbers.

package gradplanner;
import java.util.Scanner;
public class GradPlanner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numofclasses = 0;

[Code] ....

View Replies View Related

Code Allows To Enter Negative Numbers -1

Apr 4, 2015

double CashInsert = Double.parseDouble(CashAmounttxt.getText());
System.out.println("Cash Received: " + "£" + CashInsert);
if (CashInsert<=0 &&(CashInsert <TotalPrice)){
ChangeLeft = CashInsert - TotalPrice;
System.out.println("Total: £" + TotalPrice);
System.out.println("Change Due: " + "£" + ChangeLeft);}
else {
System.out.println("Insuffient funds, please enter £"+ TotalPrice +" or More");}
}

#################################################

Cash Received: £-1.0
Total: £4.85
Change Due: £-5.85

It allows me to enter -1 I've already coded it so the person cannot enter less but -1 works.

View Replies View Related

Coin Toss (For Loop) - Reject Negative Numbers

Apr 3, 2014

I'm having some issues getting this code to reject negative numbers. What I'm doing wrong.

import java.util.Random;
import java.util.Scanner;
public class ForLoop
{
public static void main (String [] args) {
Random randomNumber = new Random();

[Code] ....

This is what I have so far..

View Replies View Related

Fraction Class - Calculating Less Than Percentage GCD On Negative Numbers

Feb 15, 2014

I've been writing a fraction class code below that does a number of arithmetic calcs and when I run it these are the results I get. My gcd doesn't work when it comes to negative fractions and I'm not quite sure how to print.out the boolean methods ((greaterthan)), ((equals))and ((negative)). I'm also not sure if I have implemented those 3 methods properly. I'm still learning how to do unit testing.

Enter numerator; then denominator.
-5
10
-5/10
Enter numerator; then denominator.
3
9
1/3
Sum:
-5/30
-0.16666666666666666
Product:
-5/30
-0.16666666666666666
Devide:
-15/30
-0.5
subtract:
-45/90
-0.5
negative:
1/6
0.16666666666666666
Lessthan:
1/6
0.16666666666666666
greaterthan:
1/6
0.16666666666666666

FRACTION CLASS

import java.util.Scanner;
public class Fraction
{
private int numerator; //numerator
private int denominator; //denominator

[Code] ....

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings?

Feb 3, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

This is what i have but it only adds positive numbers and it doesn't subtract problems like 7.05-8.96. Also some of it was what our teacher gave us like alignwhole method

import java.util.Scanner; 
public class Number{
private String whole;
private String decimal;
private String sign;
  public static void main (String[] args){
 System.out.println("Enter two numbers");

[code]....

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings

Feb 5, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

The below code is what our teacher gave us to start with, but it needs to add and subtract positive or negative numbers of any length. This code only adds positive numbers. Need to write code for subtraction .

Java Code:

import java.util.Scanner;
public class Number{
private String whole;
private String decimal;
private String sign;

[Code] .....

View Replies View Related

Averaging Grades Program Throwing Exception With Negative Numbers

Nov 17, 2014

So I have re-written the code but it is still not running correctly. Any number i type in it throws an exception, also i need the program to add the totals that i type in and then once i type -1 into the prompt button list all the number i typed in and give me the average.

import java.awt.*;
import java.awt.event.*;
import javax.swing .*;
import javax.swing.text.*;
public class Averages extends JFrame
{
//construct components
JLabel sortPrompt = new JLabel("Sort By:");

[Code] .....

View Replies View Related

How To Modify Text So That It Doesn't Contain Two Equal Numbers

May 3, 2014

I would like to create a program that takes some files and modifies them in this way: The files should contain text formatted in this way:

##############
#various comments#
##############
something{
identifier=1234
anotherIdentifier=1235
anotherOne=12345
//and so on...
}//I need only this
 
#Comments are sometimes made this way
 
somethingAgain{ 
#comments that explains what's below them
I:dentifier:something, I, do, not, need
#see ^that
A:notherIdentifier:boolean
//and so on..
}

And I have to make so that the numbers contained in something{} in all the files don't match. I could ask to make so that the input file is only one, and it is formatted this way:

identifier=1234
anotherIdentifier=1235
anotherOne=12345
//and so on...

but I don't know how to do the rest of the program... That's what I've done (the names of the classes, package etc. are in Italian and there's some useless code that NetBeans prevents me from deleting):

package confronto;
import java.awt.Color;
import java.io.*;
import javax.swing.*;
public class confrontoTesti extends javax.swing.JFrame {
 
[Code] ....

View Replies View Related

Java Calculator - Numbers Are Not Adding

Jul 12, 2014

So I was making a Java Calculator that only adds. The problem is that the numbers are not adding. I think it is because whatever has been clicked is not being saved to be added.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

[Code] .....

View Replies View Related

JSP Calculator - Performing Basic Operations On Numbers

Jul 8, 2014

I want a simple jsp page that performs basic operations on numbers such as addition, subtraction, multiplication, division and modulus . How to run that file?

View Replies View Related

Extending Calculator - Added Function To Square Numbers

Feb 14, 2014

I am new to java and during my classes we built a basic calculator. I want to extend my calculator and added function to square numbers.Here is my program:

Java Code:

package calculator02;

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

[code]...

View Replies View Related

Java Calculator - Making Numbers To Stay At Textfield

Jul 12, 2014

How can I make the numbers to stay at the textfield of my calculator instead of disappear like when I type 1 then hit the + sign the number 1 disappears from the textfield I would like to have this (1+1) showing in the text field and also if try to press only + , * , - , / to show error.

this is my code

public class Calculator extends javax.swing.JFrame {
private double TEMP;
private double SolveTEMP;
Boolean addBool = false;
Boolean subBool = false;
Boolean divBool = false;
Boolean mulBool = false;
String display = "";

[Code] ....

View Replies View Related

Infix And Postfix Conversion

Feb 25, 2014

Case study : infix to postfix conversion, i don't really know how i could make codes, I can understand what is the meaning of infix and postfix but when it comes of making codes i really have a hard time with it.

View Replies View Related

Postfix Evaluation Using Stack?

Apr 24, 2014

after i am done calculating everything from numbers stack, i pop the last number and return it... my question is how can i catch an exception if the size of my numbers stack is greater than 1;

public static String evaluate(String input) {
char[] a = input.toCharArray();
if (input.isEmpty())
return "No input";
else if (input.equals(" "))
return "No input";
else if (input.equals(" "))

[code]....

View Replies View Related

How Does Prefix And Postfix Change The Answer

Mar 26, 2015

Will the postfix x++ and the prefix --y change the answer for this question?

If x has the value 10 and so does y, then what is the value of (x ++) * (-- y)?

Answer: 11*9

View Replies View Related

Getting Errors When Converting Infix To Postfix

Nov 24, 2014

I need to convert infix To Postfix but have a few errors.

Error msg:

PostFix:
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Unknown Source)
at java.util.Stack.pop(Unknown Source)
at assignment4.infixToPostfix.evaluatePostfix(infixTo Postfix.java:129)
at assignment4.infixToPostfix.main(infixToPostfix.jav a:19)

NB:
129 >>return (double) s1.pop(); AND 19>>>double ans = evaluatePostfix(postfixStr);
Deadline = less than 1 hour

View Replies View Related

Evaluate Postfix Notation Entered From Keyboard?

Oct 24, 2014

My question is to evaluate a Postfix notation entered from keyboard. I have no errors in my code but it prints only :

Exception in thread "main"

java.util.NoSuchElementException
at ArrayStack.pop(PostFixEvaluation.java:72)
at PostFixEvaluation.evaluatePostfix(PostFixEvaluatio n.java:107)
at PostFixEvaluation.main(PostFixEvaluation.java:140)

I tried many values but it prints the same exception all the time.

Here is my code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
interface Stack<E> {
 
[Code] ....

View Replies View Related

Infix To Postfix And Prefix - Not Ordering Correctly

Jul 28, 2014

So my code works perfectly when I input (a+(c-d) and i get ab+cd- for postfix and *+ab-cd for prefix. However when I input a+b+c for infix i receive abc++ postfix and +a+bc prefix when its supposed to be ab+c+ postfix and ++a b c prefix. So my issue is that any infix input with parenthesis, it converts them correctly, however without parenthesis it does not convert correctly.

import java.util.*;
public class stack {
public static char[] convertToPostfix(char[] infixEx) {
Stack<Character> operatorStack = new Stack<Character>();
char[] postfix = new char[infixEx.length];
int index = 0;

[Code] .....

View Replies View Related







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