Question

(java)write a simple graphical calculator with four operations: addition,substractuon, multipication division, that must do the following:...

(java)write a simple graphical calculator with four operations: addition,substractuon, multipication division, that must do the following:

a)it must support real and whole numbers (3.5 and 3 for instance)..

b)it must support negative numbers using a button "+/-" that allows changing between positivity and negativity of the number

c)the graphical interface of the calculator will include a matrix of buttons that include the digits 0,...,9 and "+" "-""*" "/" "." "+/-" and "=".

d)the calculator will include a text field that shows the input number or the result

e(each number represents one digit so it must have the ability to create a number that contains multiple digits(for instance 999 is 3 clicks on the number 9)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package hee;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class CalC extends JFrame implements ActionListener {
static JFrame f;
  
static JTextField l;
  
String a0, a1, a2;
  
CalC()
{
a0 = a1 = a2 = "";
}
  
public static void main(String args[])
{
f = new JFrame("CALCULATOR");
  
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println(e.getMessage());
}
  
CalC q= new CalC();

l = new JTextField(16);

l.setEditable(false);
  
JButton c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cs, cd, cm, ce, ceq, ceq1;
  
c0 = new JButton("0");
c1 = new JButton("1");
c2 = new JButton("2");
c3 = new JButton("3");
c4 = new JButton("4");
c5 = new JButton("5");
c6 = new JButton("6");
c7 = new JButton("7");
c8 = new JButton("8");
c9 = new JButton("9");   
ceq1 = new JButton("=");
  
ca = new JButton("+");
cs = new JButton("-");
cd = new JButton("/");
cm = new JButton("*");
ceq = new JButton("C");
  
ce = new JButton(".");
JPanel w = new JPanel();
  
cm.addActionListener(q);
cd.addActionListener(q);
cs.addActionListener(q);
ca.addActionListener(q);
c9.addActionListener(q);
c8.addActionListener(q);
c7.addActionListener(q);
c6.addActionListener(q);
c5.addActionListener(q);
c4.addActionListener(q);
c3.addActionListener(q);
c2.addActionListener(q);
c1.addActionListener(q);
c0.addActionListener(q);
ce.addActionListener(q);
ceq.addActionListener(q);
ceq1.addActionListener(q);
  
w.add(l);
w.add(ca);
w.add(c1);
w.add(c2);
w.add(c3);
w.add(cs);
w.add(c4);
w.add(c5);
w.add(c6);
w.add(cm);
w.add(c7);
w.add(c8);
w.add(c9);
w.add(cd);
w.add(ce);
w.add(c0);
w.add(ceq);
w.add(ceq1);
  

w.setBackground(Color.red);
  

f.add(w);
  
f.setSize(200, 200);
f.show();
}
public void actionPerformed(ActionEvent e)
{
String st = e.getActionCommand();
  
  
if ((st.charAt(0) >= '0' && st.charAt(0) <= '9') || st.charAt(0) == '.') {
  
if (!a1.equals(""))
a2 = a2 + st;
else
a0 = a0 + st;
  

l.setText(a0 + a1 + a2);
}
else if (st.charAt(0) == 'C') {
  
a0 = a1 = a2 = "";

l.setText(a0 + a1 + a2);
}
else if (st.charAt(0) == '=') {
  
double tee;
  
if (a1.equals("+"))
tee = (Double.parseDouble(a0) + Double.parseDouble(a2));
else if (a1.equals("-"))
tee = (Double.parseDouble(a0) - Double.parseDouble(a2));
else if (a1.equals("/"))
tee = (Double.parseDouble(a0) / Double.parseDouble(a2));
else
tee = (Double.parseDouble(a0) * Double.parseDouble(a2));
  
l.setText(a0 + a1 + a2 + "=" + tee);
  
a0 = Double.toString(tee);
a1 = a2 = "";
}
else {
if (a1.equals("") || a2.equals(""))
a1 = st;
else {
double tee;
  
if (a1.equals("+"))
tee = (Double.parseDouble(a0) + Double.parseDouble(a2));
else if (a1.equals("-"))
tee = (Double.parseDouble(a0) - Double.parseDouble(a2));
else if (a1.equals("/"))
tee = (Double.parseDouble(a0) / Double.parseDouble(a2));
else
tee = (Double.parseDouble(a0) * Double.parseDouble(a2));   
a0 = Double.toString(tee);
a1 = st;
a2 = "";
}
  
l.setText(a0 + a1 + a2);
}
}
}

Add a comment
Know the answer?
Add Answer to:
(java)write a simple graphical calculator with four operations: addition,substractuon, multipication division, that must do the following:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce...

    QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce the GUI below. Use 3 panels to arrange the components. You must use an array when defining the numbered buttons (o-9. Do not declare 10 buttons individually. The textfield must be initialized with the text as shown below, and be able to accommodate up to 25 characters. The spacing between the numbered buttons is 10. (28) for caloula Add Equals 3.2 Rewrite any existing...

  • Write a java calculator program that perform the following operations: 1. function to Addition of two...

    Write a java calculator program that perform the following operations: 1. function to Addition of two numbers 2. function to Subtraction of two numbers 3. function to Multiplication of two numbers 4. function to Division of two numbers 5. function to Modulus (a % b) 6. function to Power (a b ) 7. function to Square root of () 8. function to Factorial of a number (n!) 9. function to Log(n) 10. function to Sin(x) 11. function to Absolute value...

  • PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

    PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). : Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support...

  • C# ONLY C# ONLY C# ONLY In this assignment you will implement the undo feature for a simple calculator. The undo feature...

    C# ONLY C# ONLY C# ONLY In this assignment you will implement the undo feature for a simple calculator. The undo feature must be implemented using a generic or template-based Stack data structure. You may utilize language supplied libraries (e.g. java.util.stack) or a stack implementation of your own design. No third-party downloads or software installs are allowed. The undo feature must allow for an unlimited number of undo operations. If the user attempts to perform an undo operation and the...

  • Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “...

    Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “ :-( “ whenever the number displayed by the calculator is negative and a happy face “ :-) ” whenever the number displayed is positive. The calculator responds to the following commands: num + , num - , num * , num / and C ( Clear ). After initial run, if the user clicks 8 and then presses the “+” button for example, the...

  • Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses:...

    Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses: Exception handling File Processing(text) Regular Expressions Prep Readings: Absolute Java, chapters 1 - 9 and Regular Expression in Java Project Overview: Create a Java program that allows a user to pick a cell phone and cell phone package and shows the cost. Inthis program the design is left up to the programmer however good object oriented design is required.    Project Requirements Develop a text...

  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT