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
ADVERTISEMENT
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
Nov 25, 2014
I'm new to Java and I want to write a program that compute all the possible arithmetic operations of 4 integers.
For example, if the user enter 1, 2, 3, and 4 respectively, then I want the computer to print out the following results:
1 + 2 + 3 + 4
1 + 2 + 3 - 4
1 + 2 + 3 * 4
1 + 2 + 3 / 4
..
1 / 2 / 3 / 4
(parenthesis can be ignored for this program)
This is what I have so far, but I don't know how to continue:
import java.util.Scanner;
public class ArithmeticComputation{
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int num1 = stdin.nextInt();
[Code] ....
I'm looking for a method that allows me to assign char into actual operators, so that I can loop through all the computation.
View Replies
View Related
Apr 18, 2014
I am having trouble using the equals method to return the appropriate value using standard arithmetic.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
[Code] ....
I know that "inText.setText(Integer.toString(secondOperand));" in the code is wrong, I just don't know the right code...
View Replies
View Related
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
Apr 26, 2015
Write a program to simulate the operation of a simple robot. The robot moves in four directions: Forward, backwawrd, left, and right. The job of the robot is to move items and place it in the right slots in each station. There are 8 stations plus the pickup station. Pick up station is the initial start where the robot picks the items. 8 items can be placed on each station. The nearest station must be filled before placing items on the next station. The items are marked with identification or serial numbers. The items with odd serial number go to the right and items with even number go to the eft. The last slot, number 7 is reserved for light items which are less than 80kg. The serial number is a five digit number, the MSB digit 5 indicates that the product must be placed in the fifth station which is keeping the product at 20 degree F.
View Replies
View Related
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
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
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
Jan 24, 2015
im considered quite the "javanoob" as my account name states..The assignment is to basically take a number that is entered by a user, and based off what they type in, display a corresponding grade letter. For example, if they enter a number between 90-100, they should receive a response that they received an "A". Now, this is fairly simple using an if/else statement, but my professor wanted us to implement this, using a switch case to teach us the difference.
import java.util.*;
public class SwitchTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a number to find out what letter grade you have recieved");
[code]...
View Replies
View Related
Apr 28, 2014
So, I am supposed to write a program that calculates the total annual compensation for a salesman using user input for the salary, sales, and commission rate (list of 3 predefined commission rates. have to display in dollar format the amount of commission and total annual salary.
CommCalc class
public class CommCalc
{
//attributes of class, stored data
private double sales;
private double salary;
private double rate;
//and a total field? beware stale data.
//store sales argument from sales field
public void setSales(double sal)
[Code] ....
I know that i have to fix some of the semantics . I have this code that I made earlier, is there an easy way to make it into a GUI interface? Or will I have to go throughthe code lines to add the JOptionPane? And I am a little confused on how to make a list (array) for the commission rate.
View Replies
View Related
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
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
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
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
View Related
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
Feb 25, 2015
Write a program to simulate the rolling of two dice. The program should use Math.random (google how to use Math.random if needed) to roll the first die and should use Math.random again to roll the second die. The sum of the two values should then be calculated your program should roll 50,000 times. Use a single dimensional array to tally the numbers of times each possible sum appear(the possible values are 2 through 12, think about why?). Print the results in a tabular format.
Here's the code I'm working on so far:
public class RollingDice{
public static void main(String[] args){
int die1;
int die2;
int roll;
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
roll = die1 + die2;
System.out.println("The roll of the first dice is: " + die1);
System.out.println("The roll of the second dice is: " + die2);
System.out.println("Your total roll is: " + roll);
}
}
What am I missing at this point, and what parts did I do wrong to change it?
View Replies
View Related
May 28, 2014
I am trying to run the queue operations in java such as enqueue, dequeue using pointers ... How to use references as pointer in java.
View Replies
View Related
Oct 13, 2014
I have written this program but I am seeing error when I compile this program ( javac TestArithmetic.java).
1)TestArithmetic.java:26 cannot find symbol
2)symbol: class Arithmetic
3)location: class TestArithmetic
4) Arithmetic ar = new Arithmetic(x1,x2,x3);
1)TestArithmetic.java:26 cannot find symbol
2)symbol: class Arithmetic
3)location: class TestArithmetic
4) Arithmetic ar = new Arithmetic(x1,x2,x3);
2 errors.
Code :
import java.util.*;
class TestArithmetic {
public static void main (String[] args){
float number;
Scanner console= new Scanner (System.in);
System.out.println("Enter first number");
[Code] .....
View Replies
View Related
Nov 30, 2014
I am currently working on a application for a car park system. It uses GUI created by myself and holds 15 parking spaces. The user has the option to add, delete or search for a specific car. When the user adds a new car, details of the car are entered, the cars registration number (saved as a string), and the user had to check one raido button if the car is expensive, large or normal.
Once this information is entered a new instance of a vehicle will be created and the bay the car is allocated to turns green and stored in one of three arrays(expensive, normal or large). I also have a fourth array for creating the parking spaces.Each array can only hold 5 vehicles apart from the fourth which can hold all 15. When searching for a vehicle the user enters the registration of the vehicle they want to find, but I have to search through all three arrays to find it, and if it does not exist showing a message saying so.These are my three arrays
public static ParkingBay[] regularBays = new ParkingBay[5];
public static ParkingBay[] largeBays = new ParkingBay[5];
public static ParkingBay[] expensiveBays = new ParkingBay[5];
String Registration; - saves registration entered from add form
String RegistrationNumber; - saves registration number from search form
I need to search to see if RegistrationNumber is in either of the three arrays if not show a message saying otherwise
View Replies
View Related
Aug 29, 2014
How many files does it take to make a very basic java page? Can it be just one file which is run off a hard drive?I want to connect to a SQL server and update records only
View Replies
View Related
Feb 27, 2014
Basic console java program. I need to generate an employee id. I have an employee class that I will paste here so you can see my fields and constructors.
public class Employee {
private String firstName;
private String lastName;
private int id;
public int nextUniqueId = 0;
public Scanner sc = new Scanner(System.in);
[Code] ....
View Replies
View Related
Jan 27, 2012
I have to be able to read files that have been serialized in VB. 2 things, 1 they are serialized in Binary, and I tried to look up on how to serialize/deserialize in Binary, but didn't have much support...
Also in the VB code they talk about "TYPES" which seems to be to just be a class full of stuff...
View Replies
View Related
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
Oct 18, 2014
Write a method called statistic that simulates the rolling of two dice 1000 times. (1000 times is a parameter of the method statistic).The program should have no input, and should use pseudo random numbers to simulate the rolls (one random number per die). Store the sum resulting from each roll of two dice, determine the number of times each 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12 was rolled. The out produced by the program is:
Sum 2 3 4 5 6 7 8 9 10 11 12
Total 33 49 93 103 127 159 150 124 85 49 28
This is my code:
public static void main(String[] args) {
statistic(1000);
}
public static void statistic(int number) {
Random randomNumbers = new Random();
[code]...
i have been trying for hours to make it look like the output above but no luck. I was wondering if there's any other way writing the code without using arrays.
View Replies
View Related
Nov 2, 2014
Suppose that a certain airport has one runway, that each airplane takes LandingTime minutes to land and TakeOffTime minutes to take off, and that on the average, TakeOffTime planes take off and LandingTime planes land each hour.
Assume that the planes arrive at random instants of time. (Delays make the assumption to randomness quite reasonable.) There are 2 types of queues: a queue of airplanes waiting to land and a queue of airplanes waiting go take off. Because it is more expensive to keep a plane airborne than to have one waiting on the ground, we assume that the airplanes in the landing queue have priority over those in the take off queue.
Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one-minute intervals. For each minute, generate two random numbers: If the first in less than LandingTime /60, a "landing arrival" has occurred and is added to the landing queue, and if the second is less than TakeOffTime /60, a "takeoff arrival" has occurred and is added to the takeoff queue. Next, check whether the runway is free. If it is, first check whether the landing queue is nonempty, and if so, allow the first plane to land; otherwise, consider the takeoff queue. Have the program calculate the average queue length and the average time that an airplane spends in a queue. You might also investigate the effect of varying arrival and departure rates to simulate the prime and slack times of day, or what happens if the amount of time to land or takeoff is increased or decreased.
My Queue Interface: [URL] ....
My Queue Implementation: [URL] ....
My Demo (Main Method) Program: [URL] ....
Right now, I'm struggling with the demo program. I wrote some pseudocode in which I tried to match what the instructions were asking:
for(each minute) {
rand1 = generator.nextInt();
rand2 = generator.nextInt();
if(rand1 < LANDING_RATE / 60)
Add to the landing queue
if(rand2 < TAKE_OFF_RATE/60)
[Code] .....
Firstly, exactly how many minutes am I supposed to be doing this for? The assignment gave me a variable called final int ITERATIONS = 1440 , could that have something to do with how long I loop? Secondly, what exactly do I add to the queue if the conditions are true. For example, if rand1 < LANDING_RATE/60, what would I enqueue to the landingQueue? Thirdly, how am I supposed to check if the runway is free? Does that mean check to see whether the takeOffQueue is empty or not? Fourth, would allowing the first plane to land mean removing an item from the landingQueue? Also, what does it mean by "otherwise, consider the takeoff queue". Does that mean if the landingQueue is empty, I should start removing items from the takeOff queue?
The biggest problem is the fact that I have to calculate the average queue length and average time that an airplane spends in a queue.
View Replies
View Related