Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Java for beginners Part 3

, 21 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Calculator (Functions Or Methods)Welcome to your third Java for beginners tutorial, in this tutorial you will learn the simple hello Me program after you've learned Hello World! and Hello Me!..Things you will need:Java you can download it if it isn't already installed from Java official site free Ja

Calculator (Functions Or Methods)

Welcome to your third Java for beginners tutorial, in this tutorial you will learn the simple hello Me program after you've learned Hello World! and Hello Me!..

Things you will Need

  1. Java you can download it if it isn't already installed from Java official site free Java.
  2. NetBeans you can download it for free from NetBeans official site free NetBeans.
  3. Read Hello Me! if you didn't read it yet From here.
  4. 10 minutes of your time.

NOTE: Its advised to download and install Java before installing NetBeans.

Let's Start

Now that you ready to make your third program after installing Java and NetBeans lets open NetBeans, now you will have to create a new project there is lots of project's types you can choose from but we will start with the simplest and very basic one of them the Java application.

File>>New Project

You will get a window similar to this (it may look different depending on the version of your NetBeans and other installed programs you have or have not)

New Project

Project Type

Choose Java for the Categories then Java Application for the Projects then Next>.

Project Name

Project Name

For the name type Calculator or whatever you like your program name to be, But in here I will be using Calculator as the project name so its a good practice to do the same so that names wont make you confused.

Now that you created the project its time to start writing some codes, This is how your code will look like when you open Calculator.java file

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculator;
/**
 *
 * @author YourName
 */
public class Calculator {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
}

Our program is very simple all we want to do is to make our program ask you to insert a number then second one and Then adds them and give you the result and to make the program read anything from the Output Window we create an object from Scanner Class we will also use variables in this program.

NOTE: Variables are containers that hold our data temporary it has many types as we may need to store text or numbers or anything else

text = String       ex:"This is sentence"
number = int      ex: 1
point number = float       ex: 1.5

NOTE: Scanner Class is A simple text scanner which can parse primitive types and strings using regular expressions.

Now lets make our program ask for the first number, In the Main type System.out.Println("Please enter the first number"); Now we need to create an object of Scanner Class and name it input or whatever you want.

Scanner input= new Scanner(System.in);

Then we need to get the first number from the Output window so we will declare a variable called num1 of type integer and put your first number there first here is how to declare a variable of type int

int num1;

It's as easy as that but we need to tell our program to put your number in this variable because now we reserved a integer space in our memory and never used it and this is how to tell your program to read your number from the Output window and put it in the variable num1.

int num1 = input.nextInt();

We will do the same with the other number and create another variable called num2 now all we need to do is to add the two numbers and display the result which is simple as we learned before how to display any text on the Output window but there is a little thing we will add there because we want the program to output num1 + num2 = result so you will notice the difference when we reach this step but now we will make a function or method which are the same.

NOTE: Functions is block of codes to be used in different places in the same code. This is how we declare the function:

private static String add(int x, int y)
{

}

When we declare a function we first set if its going to be public or private which simply means if it private then we cant use it out of this code file if it public then we can, then we declare that its static which will be explained in later tutorials, then we set the return type which means that this function will return a text for me in our case, then we set the function name which can be whatever you want here I called it add, then between the brackets we put our arguments which are values we will be using in the function here I put 2 variables of type int because we will send our 2 numbers to our function.

NOTE: If your function wont return any values you can use void as the return type.

NOTE: Functions must be declared outside the Main function.

private static String add(int x, int y)
{
        int result=x+y;
        return String.format("%d + %d = %d", x,y,result);
}

In our function we simple declared another variable which is the result and added our 2 numbers and put the sum in the result variable then we returned the string and we used format to make our output in a specific format which is num1 + num2 = result

Then all we need to do is to call our function and display its return value and this is how

System.out.println(add(num1,num2));

Now that is how your final code will look like./*

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hellome;
import java.util.Scanner;
/**
 *
 * @author YourName
 */
public class HelloMe {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.print("Please enter the first number");
        Scanner name= new Scanner(System.in);
        int num1 = input.nextInt();
        System.out.print("Please enter the second number");
        int num2 = input.nextInt();
        System.out.println(add(num1,num2));
    }

    private static String add(int x, int y)
   {
        int result=x+y;
        return String.format("%d + %d = %d", x,y,result);
   }
}

Now lets Run our program and see what we did press F6 to run your program.

Output Result

The Result

Congratulations now you have done your third Java Program you are ready to learn more

Hope you enjoyed and benefited from this tutorial please feel free to comment or ask about anything and more tutorials will be added.

Thank you for reading and enjoy your day.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

a1mimo
Software Developer
Egypt Egypt
No Biography provided
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 21 Oct 2012
Article Copyright 2012 by a1mimo
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid