Hack Forums
[Java] Calculator || + - * / || No gui - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Calculator || + - * / || No gui (/showthread.php?tid=4730078)



Calculator || + - * / || No gui - Captain Steve Rogers - Mar 14, 2015

Made my first Calculator in Java without copying 🤗 🤗 🤗

Next step : With GUI
Please give me some homework( little easy please still beginner )

Source Code :

Code
import java.util.Scanner;

public class Basics
{    
    public static void main(String[] args)
    {  
      Scanner obj = new Scanner(System.in);
      int a;

      System.out.println("1. Addition\n2. Subtraction \n3. Multiplication\n4. Division");
      System.out.print("Choose any one and enter the number : ");
      a=obj.nextInt();

      if(a < 1 || a > 4)      
      {
      System.out.println("Invalid choice");
      System .exit( 0 );
      }
      else
      System.out.print("Enter 1st number : ");
      int b=obj.nextInt();

      System.out.print("Enter 2nd number : ");
      int c=obj.nextInt();

      switch(a)
      {      
      case 1:
      {
      System.out.println("Addition is "+(b+c));
      break;
      }
      case 2:
      {
      System.out.println("Subtraction is "+(b-c));
      break;
      }
      case 3:
      {
      System.out.println("Multiplication is "+(b*c));
      break;
      }
      case 4:
      {
      System.out.println("Division is "+(b/c));
      break;
      }
      }
    }
}

Output :

[Image: fab84b7deda1b5746992f7b51f44c9e0.png]

Special thanks to Viviparous.


* - La Manifesto - Mar 14, 2015

It looks nice so far. Have you ever done GUI before? That would be a interesting next step to try and do.


* - Captain Steve Rogers - Mar 14, 2015

(Mar 14, 2015 10:24 AM)Viviparous Wrote:  It looks nice so far. Have you ever done GUI before? That would be a interesting next step to try and do.

Thanks, yes i have done GUI before like 5 months ago.

I have done Core & Adv Java in college and in institution but still i was not able to code anything before 1 week ago.

Started taking Java seriously.

Yup :)


* - La Manifesto - Mar 14, 2015

Try and make it into a GUI and see if you can do it. Then try doing some more advance concepts in Java.


* - Captain Steve Rogers - Mar 14, 2015

(Mar 14, 2015 10:32 AM)Viviparous Wrote:  Try and make it into a GUI and see if you can do it. Then try doing some more advance concepts in Java.

Thanks a lot mate 🤝

Finally code done of non gui calculator ✌


* - csrk - Mar 16, 2015

Nicely done for a beginner. Let me throw you some tips? :-)
Generally switch-case code is smelly, if you want to add more methods (methods like add/mul/sin/cosine etc etc etc) you have to go back and edit code you know already words. Better to create methods and just implement a new method.
Speaking of methods, in this case your calculator works perfectly fine using only a main method, but it'd be alot prettier if you created methods for each calculation, like public int add(int a, int b); and so on.

Also, it'd probably be easier if you had a classed called RatNum or something that holds a numerator and a denominator int, and you use those for the calculations.


* - Iponu - Mar 16, 2015

Cool I remember that was the first program I wrote to.. People keep saying try write it in gui. Me personally I would tell you to stay away from it a bit, until you learn more about java classes, interfaces, arrays, arraylists, etc... Otherwise you will be tempted use a program that makes the gui for you rather than actually learning how to code it without a visual.


* - Captain Steve Rogers - Mar 16, 2015

(Mar 16, 2015 05:12 AM)Rewbert Wrote:  Nicely done for a beginner. Let me throw you some tips? :-)
Generally switch-case code is smelly, if you want to add more methods (methods like add/mul/sin/cosine etc etc etc) you have to go back and edit code you know already words. Better to create methods and just implement a new method.
Speaking of methods, in this case your calculator works perfectly fine using only a main method, but it'd be alot prettier if you created methods for each calculation, like public int add(int a, int b); and so on.

Also, it'd probably be easier if you had a classed called RatNum or something that holds a numerator and a denominator int, and you use those for the calculations.

Thanks for the tip

you mean class for every method(+-*/) ? why?

is it possible for you to make one calculator and post it so i can understand it better?

(Mar 16, 2015 07:31 AM)iponu Wrote:  Cool I remember that was the first program I wrote to.. People keep saying try write it in gui. Me personally I would tell you to stay away from it a bit, until you learn more about java classes, interfaces, arrays, arraylists, etc... Otherwise you will be tempted use a program that makes the gui for you rather than actually learning how to code it without a visual.

Thanks, i have actually done Core & Adv Java in college and in institution but still i was not able to code anything before 1 week ago.

I just need to learn once again.


* - Leostraza - Mar 17, 2015

Quote:you mean class for every method(+-*/) ? why?


This is what he means:
Code
public int add(int a, int b) { return a + b; }
That's a good idea..

If you have a gui, there isn't a need for Switch/case just create buttonhandlers for each button, inside those handlers do the method. (add(1, 2), etc.)

Code
public class Calculator extends JFrame {
  public Calculator() {
    setTitle("Calculator");
    //... blah blah blah
    addButton.addEventListener(new AddButtonHandler());
  }

  public static void main(String[] args) {
    new Calculator();
  }

  private class AddButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      textField.setText(Integer.toString(add(number1, number2)));
    }
  }
}

Was bored, you could do something like that..
Maybe add the number to a stack (on the textField) which has a list of operations?