Write a java calculator program with the following requirements: Please explain
ID: 3709548 • Letter: W
Question
Write a java calculator program with the following requirements:
Please explain how to add an icon to the title bar if the photo is already in the same folder.
As well as, how to change the layout of the calculator to look like a real calculator.
Add the following properties to your app:
? All digits, 4 operators (+,-,*,/), and the equal sign (=) should be added and they must be working correctly.
Text size in the text-field is very small!
? Use a larger font. Buttons are ugly!
? Change the color of the button. User friendly calculator.
? Keep the buttons in the order that it looks like a real calculator.
Doesn't look like your own app?
? Add a small icon to the title-bar of your frame. Make it yours!
Easy to get an error?
? User should not be able to start with an operator.
(Erase if entered before the input.)
? User should not be able to end with an operator. (Erase if entered at the end of the input.)
? User should not be able to use more than one operator for each calculation. (Keep the first operation only.)
? Preceding zeros should be erased.
? Still user can get some error?
? Create a small text area at the bottom of your app, and whenever the input is bad, show a message like "Bad Input!" and erase the user's input.
My code so far:
Calc
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class calc extends JFrame {
JButton btn_zero = new JButton("0");
JButton btn_one = new JButton("1");
JButton btn_two = new JButton("2");
JButton btn_three = new JButton("3");
JButton btn_four = new JButton("4");
JButton btn_five = new JButton("5");
JButton btn_six = new JButton("6");
JButton btn_seven = new JButton("7");
JButton btn_eight = new JButton("8");
JButton btn_nine = new JButton("9");
JButton btn_plus = new JButton(" + ");
JButton btn_minus = new JButton(" - ");
JButton btn_equals = new JButton(" = ");
JTextArea txt_Area = new JTextArea(3, 5);
char operator;
public calc() {
this.createGUI();
this.showFrame();
}
private void createGUI() {
add(txt_Area, BorderLayout.NORTH);
JPanel buttonpanel = new JPanel();
buttonpanel.setLayout(new FlowLayout());
buttonpanel.add(btn_seven);
buttonpanel.add(btn_eight);
buttonpanel.add(btn_nine);
buttonpanel.add(btn_four);
buttonpanel.add(btn_five);
buttonpanel.add(btn_six);
buttonpanel.add(btn_one);
buttonpanel.add(btn_two);
buttonpanel.add(btn_three);
buttonpanel.add(btn_zero);
buttonpanel.add(btn_plus);
buttonpanel.add(btn_equals);
buttonpanel.add(btn_minus);
add(buttonpanel, BorderLayout.CENTER);
txt_Area.setForeground(Color.BLACK);
txt_Area.setBackground(Color.WHITE);
txt_Area.setEditable(true);
txt_Area.setFont(txt_Area.getFont().deriveFont(20f));
txt_Area.setLineWrap(true);
txt_Area.setWrapStyleWord(true);
btn_zero.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("0".toString());
System.out.println(txt_Area.getText());
}
});
btn_one.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("1".toString());
System.out.println(txt_Area.getText());
}
});
btn_two.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("2".toString());
System.out.println(txt_Area.getText());
}
});
btn_three.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("3".toString());
System.out.println(txt_Area.getText());
}
});
btn_four.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("4".toString());
System.out.println(txt_Area.getText());
}
});
btn_five.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("5".toString());
System.out.println(txt_Area.getText());
}
});
btn_six.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("6".toString());
System.out.println(txt_Area.getText());
}
});
btn_seven.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("7".toString());
System.out.println(txt_Area.getText());
}
});
btn_eight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("8".toString());
System.out.println(txt_Area.getText());
}
});
btn_nine.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("9".toString());
System.out.println(txt_Area.getText());
}
});
btn_plus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("+".toString());
operator = '+';
}
});
btn_minus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("-".toString());
operator = '-';
}
});
btn_equals.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String LeftOperand;
String RightOperand;
String inputText;
String outputText;
inputText = txt_Area.getText();
System.out.println(inputText);
int operationIndex = inputText.indexOf(operator);
LeftOperand = txt_Area.getText().substring(0, operationIndex);
RightOperand = txt_Area.getText().substring(operationIndex + 1, txt_Area.getText().length());
switch (operator) {
case '+':
outputText = " = " + (Double.parseDouble(LeftOperand) + Double.parseDouble(RightOperand));
System.out.println(outputText);
txt_Area.append(outputText);
case '-':
outputText = " =";
}
}
});
}
private void showFrame() {
this.setSize(230, 340);
setTitle("Calculator");
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Main
import calculator.calc;
public class Main {
public static void main(String [] args) {
new calc();
}
}
Explanation / Answer
Calculator program in java:
import java.applet.Applet; // importing neccessary packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calci extends Applet implements ActionListener
{
// Declaring components
JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16,btn17,btn18,btn19,btn20,btn21,btn22,btn23,btn24,btn25,btn26,btn27,btn28,btn29,btn30,btn31,btn32,btn33;
JTextField tf1;
int flag=0,l=0,r,resu=0;
double result=0.0,result1=1.0,pi_value=0.0,res=0.0;
String temp1="",op="";
public void init()
{
setBackground(new Color(40,100,70));
tf1=new JTextField();
setLayout(null); // set Layout to Frame
JButton btn29 = new JButton("sin"); // Initializing components
JButton btn30 = new JButton("cos");
JButton btn31 = new JButton("tan");
JButton btn32 = new JButton("log");
JButton btn33 = new JButton("ln");
JButton btn24 = new JButton("n!");
JButton btn25 = new JButton("x^y");
JButton btn26 = new JButton("10^x");
JButton btn27 = new JButton("Int");
JButton btn28 = new JButton("e^x");
JButton btn19 = new JButton("CE");
JButton btn20 = new JButton("C");
JButton btn21 = new JButton("sqrt");
JButton btn22=new JButton("x^3");
JButton btn23 = new JButton("<=");
JButton btn1 = new JButton("7");
JButton btn2 = new JButton("8");
JButton btn3 = new JButton("9");
JButton btn4 = new JButton("/");
JButton btn17 = new JButton("pi");
JButton btn5 = new JButton("4");
JButton btn6 = new JButton("5");
JButton btn7 = new JButton("6");
JButton btn8 = new JButton("*");
JButton btn15 = new JButton("1/x");
JButton btn9 = new JButton("1");
JButton btn10 = new JButton("2");
JButton btn11 = new JButton("3");
JButton btn12 = new JButton("-");
JButton btn18 = new JButton("=");
JButton btn13 = new JButton("0");
JButton btn14 = new JButton(".");
JButton btn16 = new JButton("+");
add(tf1); // adding componets to Frame
add(btn1);
add(btn2);
add(btn3);
add(btn4);
add(btn17);
add(btn5);
add(btn6);
add(btn7);
add(btn8);
add(btn15);
add(btn9);
add(btn10);
add(btn11);
add(btn12);
add(btn18);
add(btn13);
add(btn14);
add(btn16);
add(btn19);
add(btn20);
add(btn21);
add(btn22);
add(btn23);
add(btn24);
add(btn25);
add(btn26);
add(btn27);
add(btn28);
add(btn29);
add(btn30);
add(btn31);
add(btn32);
add(btn33);
btn1.addActionListener(this); // Providing actonListener to buttons
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn10.addActionListener(this);
btn11.addActionListener(this);
btn12.addActionListener(this);
btn13.addActionListener(this);
btn14.addActionListener(this);
btn15.addActionListener(this);
btn16.addActionListener(this);
btn17.addActionListener(this);
btn18.addActionListener(this);
btn19.addActionListener(this);
btn20.addActionListener(this);
btn21.addActionListener(this);
btn22.addActionListener(this);
btn23.addActionListener(this);
btn24.addActionListener(this);
btn25.addActionListener(this);
btn26.addActionListener(this);
btn27.addActionListener(this);
btn28.addActionListener(this);
btn29.addActionListener(this);
btn30.addActionListener(this);
btn31.addActionListener(this);
btn32.addActionListener(this);
btn33.addActionListener(this);
tf1.setBounds(10,05,405,40);
btn1.setBackground(new Color(40,130,90)); //changing buttons colors background as well as foreground
btn2.setBackground(new Color(40,130,90));
btn3.setBackground(new Color(40,130,90));
btn4.setBackground(new Color(40,130,90));
btn5.setBackground(new Color(40,130,90));
btn6.setBackground(new Color(40,130,90));
btn7.setBackground(new Color(40,130,90));
btn8.setBackground(new Color(40,130,90));
btn9.setBackground(new Color(40,130,90));
btn10.setBackground(new Color(40,130,90));
btn11.setBackground(new Color(40,130,90));
btn12.setBackground(new Color(40,130,90));
btn13.setBackground(new Color(40,130,90));
btn14.setBackground(new Color(40,130,90));
btn15.setBackground(new Color(40,130,90));
btn16.setBackground(new Color(40,130,90));
btn17.setBackground(new Color(40,130,90));
btn18.setBackground(new Color(40,130,90));
btn19.setBackground(new Color(40,130,90));
btn20.setBackground(new Color(40,130,90));
btn21.setBackground(new Color(40,130,90));
btn22.setBackground(new Color(40,130,90));
btn23.setBackground(new Color(40,130,90));
btn24.setBackground(new Color(40,130,90));
btn25.setBackground(new Color(40,130,90));
btn26.setBackground(new Color(40,130,90));
btn27.setBackground(new Color(40,130,90));
btn28.setBackground(new Color(40,130,90));
btn29.setBackground(new Color(40,130,90));
btn30.setBackground(new Color(40,130,90));
btn31.setBackground(new Color(40,130,90));
btn32.setBackground(new Color(40,130,90));
btn33.setBackground(new Color(40,130,90));
btn1.setForeground(Color.white);
btn2.setForeground(Color.white);
btn3.setForeground(Color.white);
btn4.setForeground(Color.white);
btn5.setForeground(Color.white);
btn6.setForeground(Color.white);
btn7.setForeground(Color.white);
btn8.setForeground(Color.white);
btn9.setForeground(Color.white);
btn10.setForeground(Color.white);
btn11.setForeground(Color.white);
btn12.setForeground(Color.white);
btn13.setForeground(Color.white);
btn14.setForeground(Color.white);
btn15.setForeground(Color.white);
btn16.setForeground(Color.white);
btn17.setForeground(Color.white);
btn18.setForeground(Color.white);
btn19.setForeground(Color.white);
btn20.setForeground(Color.white);
btn21.setForeground(Color.white);
btn22.setForeground(Color.white);
btn23.setForeground(Color.white);
btn24.setForeground(Color.white);
btn25.setForeground(Color.white);
btn26.setForeground(Color.white);
btn27.setForeground(Color.white);
btn28.setForeground(Color.white);
btn29.setForeground(Color.white);
btn30.setForeground(Color.white);
btn31.setForeground(Color.white);
btn32.setForeground(Color.white);
btn33.setForeground(Color.white);
btn24.setBounds(10,52,70,30); //Setting bounds to the componets in the Null layout
btn25.setBounds(10,90,70,30);
btn26.setBounds(10,128,70,30);
btn27.setBounds(10,166,70,30);
btn28.setBounds(10,204,70,30);
btn23.setBounds(80,52,50,30);
btn1.setBounds(80,90,50,30);
btn5.setBounds(80,128,50,30);
btn9.setBounds(80,166,50,30);
btn13.setBounds(80,204,100,30);
btn19.setBounds(130,52,50,30);
btn2.setBounds(130,90,50,30);
btn6.setBounds(130,128,50,30);
btn10.setBounds(130,166,50,30);
btn20.setBounds(180,52,55,30);
btn3.setBounds(180,90,55,30);
btn7.setBounds(180,128,55,30);
btn11.setBounds(180,166,55,30);
btn14.setBounds(180,204,55,30);
btn21.setBounds(235,52,60,30);
btn4.setBounds(235,90,60,30);
btn8.setBounds(235,128,60,30);
btn12.setBounds(235,166,60,30);
btn16.setBounds(235,204,60,30);
btn22.setBounds(295,52,60,30);
btn17.setBounds(295,90,60,30);
btn15.setBounds(295,128,60,30);
btn18.setBounds(295,166,60,68);
btn29.setBounds(355,52,60,30);
btn30.setBounds(355,90,60,30);
btn31.setBounds(355,128,60,30);
btn32.setBounds(355,166,60,30);
btn33.setBounds(355,204,60,30);
ImageIcon img=new ImageIcon("1.jpg");
setIconImage(img.getImage());
}
public void actionPerformed(ActionEvent ae)
{
String st=ae.getActionCommand();
if (st.equals("+"))
{
result=result+Double.parseDouble(tf1.getText());
temp1="";
tf1.setText(temp1);
op="+";
}
else if (st.equals("-"))
{
if (flag==0)
{
result=Double.parseDouble(tf1.getText())-result;
temp1="";
flag=1;
}
else if (flag==1)
{
result=result-Double.parseDouble(tf1.getText());
temp1="";
}
tf1.setText(temp1);
op="-";
}
else if (st.equals("*"))
{
result1=result1*Double.parseDouble(tf1.getText());
temp1="";
tf1.setText(temp1);
op="*";
}
else if (st.equals("/"))
{
if (flag==0)
{
result1=Double.parseDouble(tf1.getText())/result1;
temp1="";
flag=1;
}
else if (flag==1)
{
result1=result1/Double.parseDouble(tf1.getText());
temp1="";
}
result=result1;
tf1.setText(temp1);
op="/";
}
else if(st.equals("sin"))
{
tf1.setText(""+Math.sin(Double.parseDouble(tf1.getText())));
}
else if(st.equals("cos"))
{
tf1.setText(""+Math.cos(Double.parseDouble(tf1.getText())));
}
else if(st.equals("tan"))
{
tf1.setText(""+Math.tan(Double.parseDouble(tf1.getText())));
}
else if (st.equals("x^y"))
{
result=Math.pow(Double.parseDouble(tf1.getText()),1);
temp1="";
tf1.setText(temp1);
op="^";
}
else if (st.equals("Int"))
{
tf1.setText(""+(int)(Double.parseDouble(tf1.getText())));
}
else if (st.equals("ln"))
{
tf1.setText(""+Math.log(Double.parseDouble(tf1.getText())));
}
else if (st.equals("log"))
{
tf1.setText(""+Math.log10(Double.parseDouble(tf1.getText())));
}
else if (st.equals("e^x"))
{
tf1.setText(""+Math.exp(Double.parseDouble(tf1.getText())));
}
else if (st.equals("10^x"))
{
tf1.setText(""+Math.pow(10,Double.parseDouble(tf1.getText())));
}
else if (st.equals("="))
{
if (op.equals("+"))
{
result=result+Double.parseDouble(tf1.getText());
op="";
tf1.setText(""+result);
}
else if (op.equals("-"))
{
result=result-Double.parseDouble(tf1.getText());
op="";
tf1.setText(""+result);
}
else if (op.equals("*"))
{
result1=result1*Double.parseDouble(tf1.getText());
op="";
tf1.setText(""+result1);
}
else if (op.equals("/"))
{
result1=result1/Double.parseDouble(tf1.getText());
op="";
tf1.setText(""+result1);
}
else if (op.equals("^"))
{
result=Math.pow(result,Double.parseDouble(tf1.getText()));
op="";
tf1.setText(""+result);
}
flag=0;
result=0;
result1=1;
temp1="";
}
else if (st.equals("<="))
{
if (tf1.getText().length()>0)
{
l=tf1.getText().length()-1;
tf1.setText(tf1.getText().substring(0,l));
}
if (tf1.getText().length()<=0)
{
temp1="";
tf1.setText(temp1);
}
}
else if (st.equals("C") || st.equals("CE"))
{
result=0.0;
result1=1;
op="";
flag=0;
temp1="";
tf1.setText("");
}
else if (st.equals("n!"))
{
r=1;
for (int i=1;i<=Integer.parseInt(tf1.getText());i++)
{
r=r*i;
}
tf1.setText(""+r);
}
else if (st.equals("sqrt"))
{
tf1.setText(""+Math.sqrt(Double.parseDouble(tf1.getText())));
}
else if (st.equals("x^3"))
{
tf1.setText(""+Math.pow(3,Double.parseDouble(tf1.getText())));
}
else if (st.equals("pi"))
{
tf1.setText(""+Math.PI);
}
else if (st.equals("1/x"))
{
tf1.setText(""+(1/Double.parseDouble(tf1.getText())));
}
else
{
temp1=temp1+st;
tf1.setText(temp1);
}
}
}
/*<applet code="Calci.class" width=425 height=240></applet>*/
Adding icon to title bar if both are in same folder:
- Creae a new ImageIcon objects like
ImageIcon img=new ImageIcon (Name of the image);
- Then set it to your AWT
MyFrame.setIconImage(img.getImage());
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.