Simple Printing Calculator With Accumulator

Feb 13, 2014

Here's the lab assignment: This program is a simple "printing" calculator. It will accept input from the user in the following form:

number operator

The number entered will be expected to be a float type.

The operator will be expected to be one of the following:

'E' or 'e' - Enter the number into the accumulator (this will destroy the current value). This operator is used to set the accumulator to a known value.
'A' or 'a' - Add the number to the current value in the accumulator
'S' or 's' - Subtract the number from the current value in the accumulator
'M' or 'm' - Multiply the number times the current value in the accumulator
'D' or 'd' - Divide the current value in the accumulator by the number (do not divide the accumulator by zero)
'P' or 'p' - Raise the current value in the accumulator to the power of the number, for this problem truncate the number to its integer value. (negative numbers will not be allowed) SDo not use the "Math.pow()" method to solve this problem.
'T' or 't' - Terminate processing and print the final result. The number accompanying this operator should be ignored.

The 't' or 'T' stop signal is entered by the user, the program will print out the final value contained in the accumulator and terminate.

A sample session would be the following:

User inputs: Calculator outputs:

10.0 e = 10.0000
2 D = 5.0000
2 P = 25.0000
55.0 s = -30.0000
100.25 E = 100.2500
4 m = 401.0000
0 t = 401.0000

When the program starts the initial value for the accumulator is set to zero.

If the user gives an input that would result in an error if the operation were to be attempted, an error message is printed and the current accumulator value. This calculator handles three types of input errors:

1). if the user enters an invalid operator, the message is printed : an invalid operator was entered - please reenter

2). if the user attempts to cause a divide by zero, the message is printed : attempt to divide by zero - please reenter

3). if the user attempts to raise the accumulator to a negative power, the message is printed: negative powers are not allowed - please reenter

By looking at the requirements, here's how I would like to setup the program if I knew the proper syntax and all:

Step 1: Create Class called 'calculatorProgram'

Step 2: Use code that will allow UserInput after program executes (Our teacher forbids us to use Scanner, wants us to use InputStreamReader and BufferedReader)

Step 3: Create Variables:
*Fnum (It will be the Accumulating number)
*Ans (displayed after Fnum + operator)
*Operators: E, A, S, M, D, P, T (define their functions)

Step 4: -Tell user: "How to use the calculator and what letters to use for their function"
-Ask user to: "enter a number and an Operator function"

Step 5: Begin with a While loop( != 'T' || != 't'){} -I think that's how the loop should be formatted

Step 6: Design proper If/else and nested If/else statements for each operator and its function, be sure to use nested if/else for invalid operators, divide by 0, and using negative powers.

Assuming I knew how to write Java this is how I would want to code my program. I understand the concept or I/O and the purpose of Conditional statements just I have a hard time knowing how to place the variables in the right places and writing proper syntax.

View Replies


ADVERTISEMENT

Else Without If Error - Simple Calculator Using JOP

Oct 8, 2014

import javax.swing.JOptionPane;
public class Calculator {
public static void main ( String args[] ) {
double n1 = Double.parseDouble(JOptionPane.showInputDialog(" Enter first number: "));
double n2 = Double.parseDouble(JOptionPane.showInputDialog(" Enter second number: "));
String x = JOptionPane.showInputDialog("Enter operator: ");
double result;

[code].....

Write a program that mimics a calculator. The program will need to accept as input two numeric double values, and accept the operation to be performed as the third input. The operation to be performed will be input in the form of the character representing the java operator for the operation to be performed (i.e + for addition, - for subtraction, * for multiplication, / for division, or % for modulus). Once the program has read these 3 inputs it should output the numbers, the operator, and the result. (For division, if the denominator is zero, the program will output an appropriate message (i.e division by zero is an invalid operation). Please use the JOptionPane class for both accepting the required inputs and for the required output demonstrated below.Some sample output may look as follows:

3 + 4 = 7
13 * 5 = 65
10 / 3 = 3.33333333

Error Messages:
3 errors found:
File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 23]
Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:23: 'else' without 'if'
File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 31]
Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:31: 'else' without 'if'
File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 37]
Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:37: 'else' without 'if

View Replies View Related

Calculator - Simple Else If Statement

Jan 12, 2014

Started java a couple days ago, and now im working on a calculator. But i'm having a problem with else-if statement. I've made a simple version of the problem here so its easier to identify. If i type in 17, it will give me the answer that i want it to. But if i type in 38, nothing happens.

Java Code:

import static java.lang.System.out;
import java.util.Scanner;
public class Firstclass {
public static void main(String[] args) {
Scanner answer = new Scanner(System.in);
out.println("Whats your name?");
if (answer.nextLine().equals("bob")) {
out.println("How old are you?:");
if (answer.nextLine().equals("17")) {
out.println("Nice");
} else if (answer.nextLine().equals("38")) {
out.println("good");
}
}
}
} mh_sh_highlight_all('java');

View Replies View Related

Make A Simple Calculator Program

Mar 11, 2015

i am working on my assignment in Compro 2, we are ask to make a simple calculator program

here's my code;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.BorderLayout;

[code]...

View Replies View Related

Simple Calculator Swing Java

Dec 13, 2014

I am a beginner with Java. I have never used SWING before but I have to use it now and I am clueless. I don't know if what I am doing is right.I need to create a simple calculator. My problem is getting the buttons the user clicks to appear in a text field. I know I haven't added the =/*- buttons yet.

I have left actionPerformed practically blank, what to put in ?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Created by IntelliJ IDEA.
* Date: 11/12/2014
* Changing Face Program.
*/
public class Calc extends JFrame implements ActionListener

[code]...

View Replies View Related

Calculator That Ranges From Simple Math To Trig

Nov 15, 2014

I am trying to make a calculator that ranges from simple math to trig. I am trying to start the j-frame for it and I already have most of the formulas programmed with the if-else statements and such. How do I go about starting the jFrame and marrying the 2 codes together to start it. (I have taken a bit of programming but relatively new to writing j-frames and using other classes and putting them together).

View Replies View Related

Java Simple While Loop Wage Calculator

Nov 13, 2014

I need to do a simple while loop pay calculator. I am very new and not sure what I need to do to put all the requirements in the new code from another code. Here is my code that doesn't work yet.

1.Place the wage calculation routines in a while loop.
2.Allow the user to input an individual's hours worked.
3.Accumulate the total regular hours worked, the overtime hours worked, the amount of the regular pay, the amount of overtime pay, and the total payroll, for all of the employees entered.
4.Allow the user to enter a zero (0) as the Boolean expression to end the loop.
5.When the loop ends print out all of the accumulated totals with appropriate labels, each on a separate line.

import java.util.Scanner;
public class WhileLoopPayCalc
{
public static void main(String [] args) {
final double REGULAR_HOURS = 40f;
final double HOURLY_WAGE = 12.5f;
final double OVERTIME = 1.5f;
double wage = 0f;

[Code] ....

View Replies View Related

Tip Calculator - OptionPane Not Printing On 2 Different Lines

Jan 30, 2015

So I'm having an issue. I need the Option Pane to print on 2 different lines but it is only printing on 1. Here is my code:
 
public class TipCalculator
{
public static void main(String[] args) {
String finalBillString = JOptionPane.showInputDialog(
"Enter the bill amount before tip, for Example, 35.49");
double finalBill = Double.parseDouble(finalBillString);
 
[Code] ......

View Replies View Related

Simulate Simple Calculator That Performs Basic Arithmetic Operations Of JAVA

Sep 9, 2014

How can i write a java program that simulate a simple calculator that performs the basic arithmetic operations of JAVA. (JOption Pane and Repeat control structure)

Example : Addition, Subtraction, Multiplication, Division.

The program will prompt to input two floating point numbers and a character that represents the operations presented above. In the event that the operator input is valid, prompt the user to input the operator again.

Sample Output :

First number 7
Second number 4
Menu

<+> Addition
<-> Subtraction
<*> Multiplication
</> Division

Enter Choice: * <enter>

The answer is 28.

View Replies View Related

Tip Calculator For Fun

Oct 18, 2014

So, I'm working on a Tip Calculator for fun. I have everything worked out for the most part. The feature I am trying to add is to give the user the amount of change they'll get back from their transaction. I do not ask the user any information other than what is their bill.

Please enter your bill total : 5.50
Please enter your name : some name
Total : 5.50
Tax : 0.48 -- Based off a tax rate of 8.75%
Total with Tax : 5.98 --
20 percent Tip : 1.10
Total with Tax and 20 percent Tip : 7.08
Total change : <-- I believe this will be about $2.92

I want the total bill to round to the nearest $10 value (e.g. for $5.50, calculate bill to $10. For $13.00, calculate bill to the $20.00, and so on... This way I get an accurate amount of change to give back to the user.

View Replies View Related

MVC Calculator - How To Get It To Actually Calculate

Sep 28, 2014

I made a MVC calculator. I was wondering if you could take a look at my design and if I was on track. I am still working on getting it to actually calculate something. All the buttons respond and print text on the JTextField but it is not calculating.

package calculator.MVC;
import javax.swing.JButton;
import javax.swing.JTextField;
public class CalculatorModel {
private int sum;
private int number;
private char opt;

[code]....

View Replies View Related

Calculator Only Return 0.0?

Sep 2, 2014

I am making a calculator in Java. However, when I press the "=" button on my calculator, it always returns 0.0, no matter what. I don't understand why? The code is below:

import javax.swing.*;//import the packages needed for gui
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.List;
import static java.lang.Math.*;
public class CalculatorCopy {

[Code] ....

View Replies View Related

How To Add Loop In Calculator

Mar 11, 2015

import javax.swing.*;
public class BasicCalculator{
public static void main (String[] args) {
String output="";
double n1,n2;
String operation;
operation= JOptionPane.showInputDialog("Please enter your operation");

[Code] ....

How to add a loop in this problem:

View Replies View Related

While Loop Pay Calculator

Dec 2, 2014

I have the basic workings of this program just doing all the printing at the end is throwing me off. Anyway here are some of the requirements and my code:

1. Allow the user to input an individual's hours worked.
2. Accumulate the total regular hours worked, the overtime hours worked, the amount of the regular pay, the amount of overtime pay, and the total payroll, for all of the employees entered.
3. Allow the user to enter a zero (0) as the Boolean expression to end the loop.
4. When the loop ends print out all of the accumulated totals with appropriate labels, each on a separate line.

import java.util.Scanner;
public class WhileLoopPayCalc
{
public static void main(String [] args)
{
final double REGULAR_HOURS = 40f;
final double HOURLY_WAGE = 12.5f;
final double OVERTIME = 1.5f;

[code]....

View Replies View Related

Calculation In Awt Calculator

Jul 10, 2014

I am facing problem during calculation in my awt calculator. It is showing NumberFormatException. Below is part of code.

if(e.getSource()==b14)//On button 14, label is "/"
{
String v1=tf.getText().toString();
num1=num1+Integer.parseInt(v1);//num1 is as integer variable
tf.setText("");//tf means textField
}else

[code]....

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

Trying To Create A Calculator

Sep 16, 2014

I'm new to java and have been coding for a few hours. I tried to create a calculator (Which has no GUI, just text inputs)which will let you select how to process the two integer (Add, minus, times etc.) I cant seem to get it to work. I will put in the process way such as times and have entered two integers and every time it doesn't work. Here is my code:

import java.util.*;
public class Calculator
{
public static void main (String args[])
{
int input1 = 0;
int input2 = 0;
int answer = 0;

[code]....

View Replies View Related

Create A Calculator With JCreator

May 24, 2010

I have got a problem when coding a java program to create a calculator with JCreator. How to write codes for decimal point.

Decimal point should not be set to the text more than 1 time until an operator is pressed. How can I use a Boolean value to solve this problem.

View Replies View Related

Calculator That Change Input Value Into Another Value

Nov 14, 2014

I am currently working on a small java programm. I use netbeans for it. And obviously I am new to java.

Basically its a calculator that change the input value into another value: like 10 € into whatever $ and same with Inch and Centimeters.

Therefore I need a second jDialog Form where i can change the factors for the calculator and start the calculator again.

Here is my problem: I can open the second Dialog but if I want to change the factor it doesn't change the value in the variable and the second calculator frame is with the old factors. It seems the double variable isn't working globally

I will add the files later when i find out how?

View Replies View Related

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

Random Number Calculator

Feb 1, 2015

the program basically has a random number generating, and I want to ask the user to try to guess the number, and keep guessing until the number is right. In addition to this, I need to put in extra conditions for too high or too low by 10. So for example, if they user guesses a number and its off by more than 10, then it prints that they guessed too high, and if its below 10 they guessed too low.

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

Scanner scan = new Scanner(System.in);

[code].....

Modify the program to keep the user trying to guess the number till he/she gets it right, and stop once you guess the right number.Too high, high, too low, too low( If it's off by more than 10 = way too high, if its less than 10, way too low)

View Replies View Related

Basic Calculator Functions

Jul 2, 2014

my task is to add a few functions to an example calculator. I managed to add a divide button but I simply can't figure out how to add a working Pi and reciprocal function.

package newjavaproject;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CalculateurNC extends Applet {

[code]....

View Replies View Related

How To Make Calculator Using Stacks

Feb 5, 2014

I got to make a java code for my class. I must make a calculator using stacks. I have a code but i got some mistakes. Here is the code

Java Code:

package stackscalc;
import java.util.Scanner;
import java.util.Stack;
import java.util.EmptyStackException;
class Arithmetic {
int length;
Stack stk;
String exp,

[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

Basic Cost Calculator

Oct 20, 2014

Why I am getting errors for this program I (tried) to write. I am new to java.

import java.util.Scanner;
public class TheBarkingLot {
public static void main(String args[]) {
int n;
Scanner input = new Scanner(System.in);
System.out.println("How many large dogs are boarding today?");

[Code] ....

View Replies View Related

Adding Functions To A Calculator

Dec 6, 2014

I've made a calculator that can add, subtract, multiply, and divide, but I still need a clear function and a square root one for it. I've tried going on the various calculator tutorials, but they don't work.

And here's my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JPanel implements ActionListener
{
private JTextField display = new JTextField("0");
private String button = "789/456*123-0.+=";
private double result = 0;

[Code] .....

View Replies View Related







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