How to create a simple addition function in java and use command line arguments to accept the 2 numbers to add

  How to create a simple addition function in java and use command line arguments to accept the 2 numbers to add





To create even a simple function in java, there is foundational code (often called boilerplate) to get you started that you need first and then you can create your simple function.  The boilerplate code you need in this case is the setup of a class.  After that, you can add your function to the class, and you can use your function as we do.

Once you master this step, you can advance to more complex steps, for example adding another function, or even adding another function that lives inside another class.

This is another baby step that is worthy of practicing, by following, and then by aiming to complete more and more without referencing, and then completely by memory, and to practice often.

So let's do it, and discuss it, just a bit ...


Here's the code:

  1 public class SimpleFunction {

  2 

  3     public static int mySimpleAdditionFunction (int a, int b) {

  4         return a + b;

  5     }

  6 

  7     public static void main (String argv[]) {

  8         int firstArgument = Integer.valueOf(argv[0]);

  9         int secondArgument = Integer.valueOf(argv[1]);

 10 

 11         System.out.println("first argument: " + firstArgument);

 12         System.out.println("second argument: " + secondArgument);

 13         System.out.println("result of simple addition using our function: " + mySimpleAdditionFunction(firstArgument, secondArgument));

 14     }

 15 }

Here's what we used when building this code:

  • java 8
  • vim editor (note: we have much love for intellij and other editors too!)
  • code was placed in a file named SimpleFunction.java


Here's what we did to compile and run the code:

  • javac SimpleFunction.java
  • java SimpleFunction 2 3

Notice that we provide two values after SimpleFunction, which are called command line variables.


Here's what you'll see when you run it:

first argument: 2

second argument: 3

result of simple addition using our function: 5

Here's a bit of description about the code and concepts used:

  • the public class name of SimpleFunction is the same as the filename, and that's important in order for the file class to compile
  • The syntax System.out.println is used for printing lines of content, and the content that you want to be printed belongs between the quotes for the text portion of values, but if you want to print other values (i.e. numerical values or values that are the result of a function), you can do that too, and we do in this example.
  • The typical public static void main function is added and is the entry point to the application
  • The two values that are provided on the command line when we run the application are extracted in the code using the argv variable (which is an input argument of the public static void main function).
  • The two values that are read from the command line in the code are casted / converted from string/text values to integer values.  The Integer.valueOf function (which expects a string and returns an integer) does this work for us.  The Integer.valueOf function is one of the many functions that come out of the box in java (we call these built-in functions) and they solve so many problems for us that we don't have to write code for.
  • We didn't need any special imports for this code.

Another baby step completed, nice job!

Want to know how to do something in java or have any questions?  Let me know by commenting, and we'll consider getting there as we continue to take baby steps!

Copyright © 2024 correlateencourage - All Rights Reserved.

..............................

Practicing baby steps is one of the things we believe correlates with success, and keeping track of your practice experience is another.  It's one of the many things you can track with our correlateencourage app, available here at the apple store: https://apps.apple.com/us/app/correlateencourage/id6504427387  


Comments

Popular posts from this blog

How to create a java app that converts fahrenheit to celsius and allows you to list the cities you want converted in an input text file

How to create a java app that creates a very simple plot from data in a csv file

Learn Java: The ABC's of Java