-2

With the given code I was given the directions: Java lets us use Methods/Functions so we can store procedures that we may use more than once, so I would like you to update your code where there is common tasks it can be done inside a method/function. Any idea how to do this?

package fifthAssignment;

public class Arithmetic {

    public static void main(String[] args) {

        // setting up the variable firstNumber and secondNumber
        int length = args.length;

        if (length != 3) {
            System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
            return;
        }
        int firstNumber = Integer.parseInt(args[0]);
        int secondNumber = Integer.parseInt(args[1]);
        int addition = firstNumber + secondNumber;
        int minus = firstNumber - secondNumber;
        int division = firstNumber / secondNumber;
        int multiply = firstNumber * secondNumber;
        String arithmetic = args[2];

        if (arithmetic.equals("+")) {

            System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + addition);


        } else if (arithmetic.equals("-")) {

            System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + minus);


        } else if (arithmetic.equals("/")) {

            System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + division);


            // I could not use "*" operator as it was looking to pull down all
            // the files associated with this program instead of
            // using it the way I intended to use it. So in this case I changed
            // the "*" to "x" so that I can get the solution you
            // were looking for.
        } else if (arithmetic.equals("x")) {

            System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + multiply);

        }
        // following prints out to the console what the length of each argument
        // is.
        System.out.println(args[0] + " has the length of " + args[0].length());
        System.out.println(args[1] + " has the length of " + args[1].length());

        if (arithmetic.equals("+")) {
            int total = String.valueOf(addition).length();
            System.out.println(addition + " has the length of " + total);
        }else if (arithmetic.equals("-")) {
            int total = String.valueOf(minus).length();
            System.out.println(minus + " has the length of " + total);
        }else if (arithmetic.equals("/")) {
            int total = String.valueOf(division).length();
            System.out.println(division + " has the length of " + total);
        } else if (arithmetic.equals("x")) {
            int total = String.valueOf(multiply).length();
            System.out.println(multiply + " has the length of " + total);
        }

    }
}
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
sluger233
  • 21
  • 7
  • 6
    This is elementary Java. You may have to go through a basic tutorial to see how you can do this for yourself. – AntonH Apr 22 '14 at 13:33
  • 3
    "I would like you to update your code where there is common tasks it can be done inside a method/function" How about *you* do this? –  Apr 22 '14 at 13:35
  • 2
    Also, quick note, with Java it's not "functions", it's "methods". – AntonH Apr 22 '14 at 13:35
  • 2
    Sorry, but yours is a very lazy question. In the future, you'll get better help if you show your attempt to solve it first. If you don't do this, how will we know where you're stuck or what confuses you? Also your showing your effort gains much in terms of respect for you and your question, showing that you're willing to put in the effort and initiative to try to solve it yourself first. – Hovercraft Full Of Eels Apr 22 '14 at 13:41

2 Answers2

2

I'll provide a singular example, but you should do this on your own.

You have this in your code:

System.out.println(addition + " has the length of " + total);

Instead, you could potentially create a method that would work with two ints:

public void printStatus(int check, int length) {
    System.out.println(check + " has the length of " + length);
}

Which would allow you to call

printStatus(addition, total);

This is just a rough example, but you can wrap a "process" of code in a method, and pass the necessary parameters needed to execute the method to it.

Rogue
  • 11,105
  • 5
  • 45
  • 71
-3
package fifthAssignment;

public class Arithmetic {

    public static void main(String[] args) {

        // setting up the variable firstNumber and secondNumber
        int length = args.length;

        if (length != 3) {
            System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
            return;
        }
        int firstNumber = Integer.parseInt(args[0]);
        int secondNumber = Integer.parseInt(args[1]);
        int addition = firstNumber + secondNumber;
        int minus = firstNumber - secondNumber;
        int division = firstNumber / secondNumber;
        int multiply = firstNumber * secondNumber;
        String arithmetic = args[2];

        // following prints out to the console what the length of each argument
        // is.
        System.out.println(args[0] + " has the length of " + args[0].length());
        System.out.println(args[1] + " has the length of " + args[1].length());
        performOperation(arithmetic);
    }

    public void performOperation(String arithmetic) {
        if (arithmetic.equals("+")) {
            int total = String.valueOf(addition).length();
            System.out.println(addition + " has the length of " + total);
        } else if (arithmetic.equals("-")) {
            int total = String.valueOf(minus).length();
            System.out.println(minus + " has the length of " + total);
        } else if (arithmetic.equals("/")) {
            int total = String.valueOf(division).length();
            System.out.println(division + " has the length of " + total);
        } else if (arithmetic.equals("x")) {
            int total = String.valueOf(multiply).length();
             System.out.println(multiply + " has the length of " + total);
        }
    }

}
Rogue
  • 11,105
  • 5
  • 45
  • 71
Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
  • I have reposted the solution just by adding your operation logic inside a method. thats how we achive the modularity. – Karibasappa G C Apr 22 '14 at 13:47
  • I fixed the code, but this isn't much different than just taking a whole chunk of code and putting it elsewhere. It's not really modularized, it's just split in two – Rogue Apr 22 '14 at 13:52
  • Okay so I have the code and I am maneuvering things around to try and get it to work. The problem I have now it I cant access the variables addition, minus, division etc. in the performOperation method. I am sorry if this is juvenile or easy java but I am having a problem with this. – sluger233 Apr 22 '14 at 15:52