Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Calculator Method issue

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Calculator Method issue

    Hi, I have a question regarding my code here. It's a simple calculator. It's not necessarily wrong, but I think there is a shorter, better way to do this.

    Here is the code:

    import javax.swing.*;
    public class Java_calculator_3_4 {
     
        static void addition () {
            int a = Integer.parseInt(JOptionPane.showInputDialog("Please enter the first value:"));
            int b = Integer.parseInt(JOptionPane.showInputDialog("Please enter the second value:"));
            int answer = (a+b);
            System.out.println("\nThe answer is " + answer);
            }
     
        static void subtraction () {
            int a = Integer.parseInt(JOptionPane.showInputDialog("Please enter the first value:"));
            int b = Integer.parseInt(JOptionPane.showInputDialog("Please enter the second value:"));
            int answer = (a-b);
            System.out.println("\nThe answer is " + answer);
            }
        static void multiplication () {
            int a = Integer.parseInt(JOptionPane.showInputDialog("Please enter the first value:"));
            int b = Integer.parseInt(JOptionPane.showInputDialog("Please enter the second value:"));
            int answer = (a*b);
            System.out.println("\nThe answer is " + answer);
            }
        static void division () {
            int a = Integer.parseInt(JOptionPane.showInputDialog("Please enter the first value:"));
            int b = Integer.parseInt(JOptionPane.showInputDialog("Please enter the second value:"));
            int answer = (a/b);
            System.out.println("\nThe answer is " + answer);
            }
     
    public static void main(String[] args) {
        String arithmeticOperation;
        char selection;
     
        System.out.println("Possible arithmetic operations:\n a\t Addition\n b\t Subtraction\n c\t Multiplication\n d\t Division");
     
        arithmeticOperation = JOptionPane.showInputDialog("Which arithmetic operation would you like to initiate?");
        selection = arithmeticOperation.charAt(0);
     
        switch (selection) {
            case 'a':
                addition();
                break;
            case 'b':
                subtraction();
                break;
            case 'c':
                multiplication();
                break;
            case 'd':
                division();
                break;
                default:
     
            System.out.println("You choice is invalid");
            }
     
            System.exit(0);
        }
    }

    I'm trying to figure out how I can convert the code so the 'InputDialog' only has to be written once, preferably within the Main Method like the 'arithmeticOperation', and then from there transfer the two entered numbers as arguments back to the methods to be calculated. This means the output also only has to be programmed once. Same issue.

    I'm not sure where to begin here, and how to adjust the code accordingly. So any advice and tips would be greatly appreciated.

    Thanks

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calculator Method issue

    You could write a method to prompt the user for the two int values and return those two values in a custom class you have written or return the values in a 2 dim int array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculator Method issue

    Hi Norm. Thanks for the reply. I don't understand though. Please check this code. It's a simpler version with my idea. I've commented everything so you should be able to see what the issue is. Thanks again.

    import javax.swing.JOptionPane;
     
    public class Java_methods_p_a {
     
     
        public static void main(String[] args) {
     
     
            //This is where the user chooses the operation. Ill keep it simple and only have 2 options here.
            int operationA =Integer.parseInt( JOptionPane.showInputDialog("Add operation: 1.Addition /t 2.Subtraction"));
     
            //I use a switch case function here as i usually have more than 2 options.
            switch(operationA) {
            case '1':
                System.out.println("+");
                break;
            case '2':
                System.out.println("-");
                break;
     
     
            }
     
                //Here I initialize the variables where the user can enter the 2 numbers.
             int variable1 = Integer.parseInt(JOptionPane.showInputDialog("Add first number:"));
             int variable2 = Integer.parseInt(JOptionPane.showInputDialog("Add second number:"));
     
             System.out.println("The answer is " + ???????  (variable1, variable2));
     
            /*THIS PART IS THE ISSUE!!!!System.out.println("blablabla " + ???????  (variable1, variable2)); What do I add where the ??? are         
           to be able to get the correct output according to the operation the user chooses? So basically if user chooses option 1 which is'+' the 
           output should be from the method 'addition' with 'return(a+b)';.For option 2 the output should be method subtraction 'return(a-b);' Thanks*/
     
     
        }
        //method addition
        static int addition(int a, int b) {
            return(a+b);
     
        }
        //method subtraction
        static int subtraction(int a, int b) {
            return(a-b);
     
        }
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calculator Method issue

    I don't understand
    Please explain what you do not understand. Some of the issues could be:
    how to call a method to prompt for and receive 2 int values
    returning an int array containing the 2 values
    receiving an int array from the method
    extracting 2 values from the returned array
    or what?

    The new code looks like it is trying a whole new approach. I think it would be better to implement the previously discussed technique. When you have that technique working, then we could talk about another technique using an interface and a group of classes that implement that interface.
    I think that technique would be more complicated than a method that returns values in an array.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Calculator double zero button issue.
    By mtahirkn in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 27th, 2021, 06:29 AM
  2. Calling a Method with my Hexagon Area Calculator
    By EarlyBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2013, 07:25 PM
  3. GUI calculator issue
    By pricklygoo33 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 27th, 2013, 12:07 AM
  4. [SOLVED] Simple calculator issue
    By ikocijan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2012, 02:43 AM
  5. Mortgage Calculator Issue
    By coyboss in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 5th, 2011, 09:12 AM