Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Calculator || + - * / || No gui

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.
[Image: ebisfS8.png]
It looks nice so far. Have you ever done GUI before? That would be a interesting next step to try and do.
(Mar 14, 2015 10:24 AM)La Manifesto 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 :)
[Image: ebisfS8.png]
Try and make it into a GUI and see if you can do it. Then try doing some more advance concepts in Java.
(Mar 14, 2015 10:32 AM)La Manifesto 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 ✌
[Image: ebisfS8.png]
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.
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.

If Java had true garbage collection, most programs would delete themselves upon execution.
  — Robert Sewell

(Mar 16, 2015 05:12 AM)csrk 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.
[Image: ebisfS8.png]
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?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by billybluiceezDec 8, 2016 08:13 PM 3 152 Dec 8, 2016 08:13 PM
Last Post: billybluiceez
[.NET] by ChessoMar 11, 2015 08:50 AM 7 289 Mar 11, 2015 08:50 AM
Last Post: ElementalTree
[Java] by JekwaAug 4, 2014 09:01 PM 9 124 Aug 4, 2014 09:01 PM
Last Post: Psifour