Jump to content

Very Simple Calculator Program(New Programmer)

Hey all this community has always been friendly in the past so I decided to share my first full working program made from Java. If you wanna test it out (its a simple calculator) the download link is below.

 

Download link: https://drive.google.com/open?id=0B5ZLrP4AjBgkY2M3c0ZVaWE3cGM&authuser=0

If you think it's a virus I can tell you that it is not! Feedback is appreciated since this is my first program! (I'm only 16 :P) I also had a background image which didn't seem to transfer over to the actual compressing of the program. If anyone can help me with that that will be nice!

 

Thanks,

Jonathan

 

P.S what can I add?

Link to comment
https://linustechtips.com/topic/383803-very-simple-calculator-programnew-programmer/
Share on other sites

Link to post
Share on other sites

Post the source code? EDIT: This is malware, java programs are NOT .EXE FILES

package game;import java.awt.event.*;import javax.swing.*; /* *  creates a calculator that can add,subtract,multiply and divide 2 given numbers. */public class BasicCalculator extends JFrame implements ActionListener{JLabel author = new JLabel("By; Jonathan Hyun"); JButton addition = new JButton("Addition");JButton subtract = new JButton("Subtraction");JButton multiply = new JButton("Multiply");JButton divide = new JButton("Divide");JButton clear = new JButton("Clear");JButton quit = new JButton("Quit"); JTextField num1 = new JTextField(10);JTextField num2 = new JTextField(10);JTextField ans = new JTextField(10); String getTextField1;String getTextField2;String getAns; double textFieldNum1;double textFieldNum2;double textFieldAns; BasicCalculator(String title){super(title);setLayout(null);setContentPane(new JLabel(new ImageIcon("C:\\Users\\Jonathan\\Desktop\\New folder\\background.png"))); add(addition);add(subtract);add(divide);add(multiply);add(num1);add(num2);add(ans);add(clear);add(quit); addition.setBounds(100,10,100,30);subtract.setBounds(300,10,100,30);multiply.setBounds(100,125,100,30);divide.setBounds(300,125,100,30);num1.setBounds(100,70,100,30);num2.setBounds(300,70,100,30);ans.setBounds(200,200,100,50);clear.setBounds(350,300,75,30);quit.setBounds(50,300,75,30); quit.addActionListener(this);addition.addActionListener(this);subtract.addActionListener(this);multiply.addActionListener(this);divide.addActionListener(this);clear.addActionListener(this); quit.setActionCommand("quit");addition.setActionCommand("add");subtract.setActionCommand("subtract");multiply.setActionCommand("multiply");divide.setActionCommand("divide");clear.setActionCommand("clear"); ans.setEditable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}  public void actionPerformed(ActionEvent event) {if(event.getActionCommand().equals("add")){getTextField1 = num1.getText();getTextField2 = num2.getText();textFieldNum1 = Integer.parseInt(getTextField1);textFieldNum2 = Integer.parseInt(getTextField2);textFieldAns = textFieldNum1 + textFieldNum2;getAns = Double.toString(textFieldAns);ans.setText(getAns); }if(event.getActionCommand().equals("subtract")){getTextField1 = num1.getText();getTextField2 = num2.getText();textFieldNum1 = Integer.parseInt(getTextField1);textFieldNum2 = Integer.parseInt(getTextField2);textFieldAns = textFieldNum1 - textFieldNum2;getAns = Double.toString(textFieldAns);ans.setText(getAns);}if(event.getActionCommand().equals("multiply")){getTextField1 = num1.getText();getTextField2 = num2.getText();textFieldNum1 = Integer.parseInt(getTextField1);textFieldNum2 = Integer.parseInt(getTextField2);textFieldAns = textFieldNum1 * textFieldNum2;getAns = Double.toString(textFieldAns);ans.setText(getAns);}if(event.getActionCommand().equals("divide")){getTextField1 = num1.getText();getTextField2 = num2.getText();textFieldNum1 = Integer.parseInt(getTextField1);textFieldNum2 = Integer.parseInt(getTextField2);textFieldAns = textFieldNum1 / textFieldNum2;getAns = Double.toString(textFieldAns);ans.setText(getAns);}if(event.getActionCommand().equals("clear")){num1.setText(null);num2.setText(null);ans.setText(null);}if(event.getActionCommand().equals("quit")){System.exit(0);} } public static void main(String [] args){BasicCalculator calculator = new BasicCalculator("Calculator v0.1");calculator.setVisible(true);calculator.setSize(500,400);calculator.setResizable(false); } }
Edited by colonel_mortis
Code tags and spoiler
Link to post
Share on other sites

My bad, made a mistake. I'll blame the lack of sleep since I haven't slept for over 24 hours.

hehe, yeah I had a feeling people might mark it as malware. I'm still trying to figure out how to put the background image with the compressing. I see the problem since the image comes from a folder location in my computer

Link to post
Share on other sites

Quick one i see: don't parse it as int nce users may usw float /double

 

 

Dividing by 0 should give an error or Nan, not infinity.

Yeah I friend of mine just pointed out that he couldn't use decimals. It is easy as parsing it as a double?

 

Thanks for pointing it out ill try to fix that.

Link to post
Share on other sites

Yeah I friend of mine just pointed out that he couldn't use decimals. It is easy as parsing it as a double?

Thanks for pointing it out ill try to fix that.

Depends on how long of a digit you want for input. Check or divide by zero and users also want faster process so use other events when needed.

Experiment and that's a good time drain, r practice mean.

Link to post
Share on other sites

Depends on how long of a digit you want for input. Check or divide by zero and users also want faster process so use other events when needed.

Experiment and that's a good time drain, r practice mean.

alright thanks i'll take that into consideration

Link to post
Share on other sites

getTextField1 = num1.getText();getTextField2 = num2.getText();textFieldNum1 = Integer.parseInt(getTextField1);textFieldNum2 = Integer.parseInt(getTextField2);

It is unnecessary to type this into all if statements, you could just make one that regardless of which of the buttons was pressed.

Curing shitposts by shitposts

Link to post
Share on other sites

Hey all this community has always been friendly in the past so I decided to share my first full working program made from Java. If you wanna test it out (its a simple calculator) the download link is below.

 

Download link: https://drive.google.com/open?id=0B5ZLrP4AjBgkY2M3c0ZVaWE3cGM&authuser=0

If you think it's a virus I can tell you that it is not! Feedback is appreciated since this is my first program! (I'm only 16 :P) I also had a background image which didn't seem to transfer over to the actual compressing of the program. If anyone can help me with that that will be nice!

 

Thanks,

Jonathan

 

P.S what can I add?

Nice GUI. I also made a calculator recently, but in C#. 

 

https://github.com/GenesisOfTheVoid/C-.NET-Projects/tree/master/WindowsFormsApplication2

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to post
Share on other sites

Hey, I did a really basic calculator in VSE 2012! :D

 

https://drive.google.com/file/d/0By5LOV3t5pwWamhFV3FfYVcwZ0U/view?usp=sharing

Main rig on profile

VAULT - File Server

Spoiler

Intel Core i5 11400 w/ Shadow Rock LP, 2x16GB SP GAMING 3200MHz CL16, ASUS PRIME Z590-A, 2x LSI 9211-8i, Fractal Define 7, 256GB Team MP33, 3x 6TB WD Red Pro (general storage), 5x 8TB WD White Label/Red (Plex) (both arrays in their respective Windows Parity storage spaces), 1TB Teamgroup MP33 (dumping ground) Corsair RM750x, TrueNAS Scale

Sleeper HP Pavilion A6137C

Spoiler

Intel Core i7 6700K @ 4.4GHz, 4x8GB G.SKILL Ares 1800MHz CL10, ASUS Z170M-E D3, 128GB Team MP33, 1TB Seagate Barracuda, MSI GTX 970 100ME, EVGA 650G1, Windows 11 Pro

OptiPlex 7040M

Spoiler

Intel Core i7 6700, 2x16GB Mushkin Redline (stuck at 2133MHz CL13), 240GB Corsair MP510, 2TB Seagate Barracuda 2.5", 130w Dell power brick, Windows 11 Pro

Mac Mini (Late 2020)

Spoiler

Apple M1, 8GB RAM, 256GB, macOS Sonoma

Consoles: Steam Deck LCD (512GB), Softmodded 1.4 Xbox w/ 500GB HDD, Xbox 360 Elite 120GB Falcon, XB1X w/2TB MX500, Xbox Series X, PS1 1001, PS2 Slim 70000 w/ FreeMcBoot, PS4 Pro 7015B 1TB, PS5 Digital, Nintendo Switch OLED, Nintendo Wii RVL-001 (black)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×