GOYO Large

Calculating things such as addition, subtraction, division, and multiplication is easy to do but you must print the result like System.out.println(3+5);. However this calculator I made using java is made for people who do not know any clue how to code. Just by a normal calculator, you have all the numbers and the signs, and all you have to do is press the buttons. I feel really accomplished by doing this project. The previous projects I made was games, but this one is a program that can actually help people. There are many more better calculators on the internet that are better than me, however I think the calculator I made was a step to make mine better like the ones you can see on a mac or windows. My calculator is only for two numbers, so if you type 1+1+2, you will get 3. I still have not been able to figure out how to do 3 or more numbers, and that will be posted on my next GOYO, with an improved and better one. I did not learn anything new doing this project, but if I progress and figure out how to multiply, divide, add or subtract 3 or more numbers, it will be something new I will learn. The program I made is very good for people who need a calculator. When I use it, I would feel accomplished, since I made a useful program! Below is the calculator and its code. Since this is a really useful code, I feel like extra credit will help me out.

Image

In this input I am multiplying 7 with 9 giving me 63, which is 7 * 9.

Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame{
private static final long serialVersionUID = 1L;
static double x=0,y=0,result=0;
static int operator=0;
JLabel l;
JButton b[]=new JButton[18];
Container c;
Main(){
setTitle(“Calculator”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(600,200);
c=getContentPane();
c.setLayout(null);
c.setBackground(Color.WHITE);
l=new JLabel();
c.add(l);
b[0]=new JButton(“1”);
b[1]=new JButton(“2”);
b[2]=new JButton(“3”);
b[3]=new JButton(“4”);
b[4]=new JButton(“5”);
b[5]=new JButton(“6”);
b[6]=new JButton(“7”);
b[7]=new JButton(“8”);
b[8]=new JButton(“9”);
b[9]=new JButton(“0”);
b[10]=new JButton(“x”);
b[11]=new JButton(“รท”);
b[12]=new JButton(“+”);
b[13]=new JButton(“-“);
b[14]=new JButton(“.”);
b[15]=new JButton(“=”);
b[16]=new JButton(“Delete”);
b[17]=new JButton(“Clear”);

l.setBounds(0,0,360,150);
l.setFont(new Font(“Arial”,Font.BOLD,40));

b[9].setBounds(0,410,90,60);
b[14].setBounds(90,410,90,60);
b[15].setBounds(180,410,90,60);
b[12].setBounds(270,410,90,60);

b[0].setBounds(0,350,90,60);
b[1].setBounds(90,350,90,60);
b[2].setBounds(180,350,90,60);
b[13].setBounds(270,350,90,60);

b[3].setBounds(0,290,90,60);
b[4].setBounds(90,290,90,60);
b[5].setBounds(180,290,90,60);
b[10].setBounds(270,290,90,60);

b[6].setBounds(0,230,90,60);
b[7].setBounds(90,230,90,60);
b[8].setBounds(180,230,90,60);
b[11].setBounds(270,230,90,60);

b[16].setBounds(0,170,180,60);
b[17].setBounds(180,170,180,60);

for(int i=0;i<18;i++){
b[i].setFocusable(false);
b[i].setBackground(Color.WHITE);
b[i].setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
b[i].setFont(new Font(“Arial”,Font.BOLD,20));
b[i].addActionListener(new MyButtonListener());
c.add(b[i]);
}
setVisible(true);
setSize(360,500);
setResizable(false);
}
class MyButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==b[0]){
l.setText(l.getText().concat(“1”));
}
if(source==b[1]){
l.setText(l.getText().concat(“2”));
}
if(source==b[2]){
l.setText(l.getText().concat(“3”));
}
if(source==b[3]){
l.setText(l.getText().concat(“4”));
}
if(source==b[4]){
l.setText(l.getText().concat(“5”));
}
if(source==b[5]){
l.setText(l.getText().concat(“6”));
}
if(source==b[6]){
l.setText(l.getText().concat(“7”));
}
if(source==b[7]){
l.setText(l.getText().concat(“8”));
}
if(source==b[8]){
l.setText(l.getText().concat(“9”));
}
if(source==b[9]){
l.setText(l.getText().concat(“0”));
}
if(source==b[14]){
l.setText(l.getText().concat(“.”));
}
if(source==b[11]){// /
x=Double.parseDouble(l.getText());
operator=4;
l.setText(“”);
}
if(source==b[10]){// x
x=Double.parseDouble(l.getText());
operator=3;
l.setText(“”);
}
if(source==b[13]){// –
x=Double.parseDouble(l.getText());
operator=2;
l.setText(“”);
}
if(source==b[12]){// +
x=Double.parseDouble(l.getText());
operator=1;
l.setText(“”);
}
if(source==b[15]){
y=Double.parseDouble(l.getText());

switch(operator)
{
case 1: result=x+y;
break;

case 2: result=x-y;
break;

case 3: result=x*y;
break;

case 4: result=x/y;
break;

default: result=0;
}

l.setText(“”+result);
}
if(source==b[17]){
l.setText(“”);
}
if(source==b[16]){
String s=l.getText();
l.setText(“”);
for(int i=0;i<s.length()-1;i++){
l.setText(l.getText()+s.charAt(i));
}
}
}
}
public static void main(String[] args) {
new Main();
}
}

2 thoughts on “GOYO Large”

  1. Not only is that a pretty nifty start on a calculator utility, it’s a pretty meaty Codex post, too. Normally, I prefer that you DO learn something with each GOYO, but since this is one in which you’ve built a foundation and are planning to go further, I’ll award some extra XP and Gold for it. Keep moving forward!

    (For the adding multiple numbers problem, you might be interested in looking up the computing terms “stack” (LIFO) and “queue” (FIFO); one of these might be useful for that issue.)

Leave a Reply

Your email address will not be published. Required fields are marked *