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


ADVERTISEMENT

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

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

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

Swing/AWT/SWT :: Simple Java GUI

Feb 4, 2015

I'm just starting to learn Java GUI. I'm trying to make a simple GUI that will update dynamically but having trouble ending the loop correctly.The Currently I've got the GUI updating and stopping, but the Test class method loop does not terminate correctly, it continues to count.

import java.awt.*; // Using AWT container and component classes
import java.awt.event.*; // Using AWT event classes and listener interfaces
import java.util.Timer;
import java.util.TimerTask;

[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

Swing/AWT/SWT :: Pay Calculator And If Statement

Oct 18, 2014

Ihave assignment to work out rate of pay based on hours worked.

rate is 4.60 per hour for 1st hours and then extra payment 4.60 * 1.5 for hours over 40 only.

It's this part I'm having problems with.

My if statement is below but I'm struggling with how I should write the if hours > 40 one.

[
public void actionPerformed(ActionEvent ev)
{
hours1 = Integer.parseInt(hours.getText());
if (hours1 <=40) {
pay1 = (rate * hours1);
}
if (hours1 >40){
pay1 = (rate * hours1 -40) * (rate * 1.5);
}
hours.setText("");
pay.setText(""+pay1);
}

View Replies View Related

Swing/AWT/SWT :: Set Look And Feel For Calculator To Nimbus?

Feb 11, 2014

I am trying to set the look and feel for my calculator to Nimbus but I am having some problems. It looks horrible though nothing at all like the screenshots I have seen for it.

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

[Code].....

View Replies View Related

Swing/AWT/SWT :: Simple Applet Won't Run

Mar 11, 2002

I coded up a simple applet and some html with an applet tag and it tested out OK using the applet viewer. But it doesn't do anything (except draw a grey empty box 100x200) when executed from the application server. For the applicaton server test, I put the applet tag in the HTML Body of the JSP page i.e.

<applet code=CatApplet.class height=100 width=200> </applet>

The applet class file is in the same directory as the JSP page. The source for it is:

import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.net.*;
import java.util.Date;public class CatApplet extends JApplet{public void init (){setBackground(Color.green);
resize (250,100);}public void paint (Graphics g){g.drawString ("lets get some cat gui going", 35, 15);}}

Why won't my applet run when the applet tags are in the JSP page?

View Replies View Related

Swing/AWT/SWT :: Use JComboBox With Simple ListCellRenderer

Aug 7, 2014

I am trying to use a JComboBox with a simple ListCellRenderer

public class EntscheidendeBehoerdeListRenderer extends JLabel implements ListCellRenderer<EntscheidendeBehoerde> {
private static final Color HIGHLIGHT_COLOR = new Color(0, 0, 128);
private static final long serialVersionUID = -4719965772580952993L;
public Component getListCellRendererComponent(
JList<? extends EntscheidendeBehoerde> list,
EntscheidendeBehoerde value, int index, boolean isSelected,
boolean cellHasFocus) {

[code].....

The JCombox shows and odd behavior, when I point to an item it moves up or down and an empty line appears under the cursor.

View Replies View Related

Swing/AWT/SWT :: Simple Image Display

Aug 8, 2014

I am fairly new to Java. I am trying to learn some image manipulation basics. I have searched the web endlessly and made numerous attempts but I cannot seem to get a darn image to display. This code is my latest attempt in Eclipse. The file I am using is chairangle.jpg which is is the src folder of the package I am working in in Eclipse. I am on Windows 8.

import java.awt.*;
import javax.swing.*;
import javax.swing.ImageIcon;
public class otherimg extends JFrame
{
public static void main(String args[]){
JFrame f = new JFrame();
ImageIcon image = new ImageIcon("image/chairangle.jpg");
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
f.pack();
f.setVisible(true);
}
}

View Replies View Related

Swing/AWT/SWT :: Restaurant Bill Calculator - JFrame Is Not Working

Sep 5, 2014

Why the JFrame is not working

// Project: RestaurantBillCalculator.java Calculates a table's bill.

// Import statements for different classes
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
public class RestaurantBillCalculator extends JFrame

[Code] ....

View Replies View Related

Swing/AWT/SWT :: Create A Simple JFrame Application

Jan 22, 2015

I installed Windowsbuilder in Eclipse as described in this video:URL....

To get starated I now want to create a simple JFrame application that works in the following way. On the opening screen I want to present 4 buttons. Based on the selection I want the JFrame to change layout and components so that the same JFrame behind the scenes contains 5 layouts but only one visible at any time. What is the best way to accomplish this? Or should I do this in another way using multiple JFrames or similar?

View Replies View Related

Swing/AWT/SWT :: Simple GUI Task - Draw Pie Chart With Different Colors

Jan 31, 2014

Simple gui task. it meant to draw pie chart with different colors. i can't seem to find a mistake in it. It works if i put in g2g.draw(arc) i know arcs overlaps a bit due int conversion, but that's ok.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.geom.Arc2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;

[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

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

Java Calculator - Multiplication And Division

Apr 24, 2015

I am having problems with my code I have added the multiplication and division but they will not display also how can I correct any error when dividing by zero?

import java.awt.*;
import java.awt.event.*;
public class calculator2 extends java.applet.Applet implements ActionListener {
TextField txtTotal = new TextField("");
Button button[] = new Button[10];

[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

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

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







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