Simple Java Programs : Basic Fundamentals for Beginners

simple java programsJava is a high-level object oriented programming language and some users may find it tough. However, these sets of programs will make you comfortable with the Java programming language and its basic concepts. You will see a set of programs mentioned below that ranges from a simple “hello world” application to searching for a number. It covers all the small details that every beginner must know to be a Java programmer.

Program to Print “Hello World”

class HelloWorld

{

public static void main(String args[])

{

System.out.println("Hello World"); //Prints on screen “Hello World”

}

}

Output:

Hello World

Learn more about Java programming at Udemy.com

Program to Swap Two Numbers: The user gives two numbers as input and the numbers are swapped.

import java.util.Scanner;

class SwapNumbers

{

public static void main(String args[])

{

int z, y, temp;

System.out.println("Enter z and y");

Scanner sct = new Scanner(System.in);   //User inputs two numbers

z = sct.nextInt();   //User inputs two numbers

y = sct.nextInt();

System.out.println("Before Swapping\nz = "+z+"\ny = "+y);

temp = z;   //Swapping is done

z = y;

y = temp;

System.out.println("After Swapping\nz = "+z+"\ny = "+y);

}

}

Output:

Enter z and y 5 10

Before Swapping

z = 5

y = 10

After Swapping

z = 10

y = 5

Learn Java from scratch at Udemy.com

Program to Display Date and Time

Current Date and time is printed on the screen

import java.util.*;

class GetDateAndTime

{

public static void main(String args[])

{

int day, month, year;

int second, minute, hour;

GregorianCalendar date = new GregorianCalendar(); //Creating Date Object

day = date.get(Calendar.DAY_OF_MONTH);// Storing day of the Month

month = date.get(Calendar.MONTH);// Storing the month

year = date.get(Calendar.YEAR); // Storing the current Year

second = date.get(Calendar.SECOND); //Time in Seconds, Minutes and Hours

minute = date.get(Calendar.MINUTE);

hour = date.get(Calendar.HOUR);

System.out.println("Current date is  "+day+"/"+(month+1)+"/"+year);

System.out.println("Current time is  "+hour+" : "+minute+" : "+second);

}

}

Output

Current date is  15/5/2014

Current time is  2 : 15 : 49

Want to know more about Java? Take a tutorial at Udemy.com

Calculate the largest of three numbers: The largest of the three numbers entered by the user is calculated.

import java.util.Scanner;

class Largest

{

public static void main(String args[])

{

int a,b,c;

System.out.println("Enter three integers ");

Scanner sct = new Scanner(System.in);

a = sct.nextInt();

b = sct.nextInt(); //User Input

c = sct.nextInt();

if ( a > b && a > c )  // Condition check for Largest number

System.out.println("1st number is largest.");

else if ( b > a && b > c )

System.out.println("2nd number is largest.");

else if ( c > a && c > b )

System.out.println("3rd number is largest.");

else

System.out.println("Number are not distinct.");

}

}

Output:

Enter three integers 6 10 7

2nd number is largest.

Factorial Calculation and a basic loop structure: The factorial of a number entered by the user is calculated.

import java.util.Scanner;

class Factorial

{

public static void main(String args[])

{

int n,c, fact = 1;

System.out.println("Enter a number");

Scanner sct = new Scanner(System.in);

n = sct.nextInt();

if ( n < 0 )

System.out.println("Number should be non-negative.");

else

{

for ( c = 1 ; c <= n ; c++ )  // Calculating Factorial

fact = fact*c;

System.out.println("Factorial of "+n+" is = "+fact);

}

}

}

Output

Enter a number 5

Factorial of 5 is = 120

Program to calculate Reverse of a number

import java.util.Scanner;

class ReverseNumber

{

public static void main(String args[])

{

int n, rev = 0;

System.out.println("Enter the number to reverse ");

Scanner sct = new Scanner(System.in);

n = sct.nextInt(); //User Input

while( n != 0 )// Reversing a Number Entered

{

rev = rev * 10;

rev = rev + n%10;

n = n/10;

}

System.out.println("Reverse of entered number is "+rev);

}

}

Output

Enter the number to reverse 5234

Reverse of entered number is 4325

Program to calculate Reverse of a String

import java.util.*;

class ReverseString

{

public static void main(String args[])

{

String original, reverse = "";

Scanner sct = new Scanner(System.in);

System.out.println("Enter  string to reverse");

original = sct.nextLine(); //String input from user

int length = original.length();

for ( int i = length - 1 ; i>= 0 ; i-- ) // Reversing the String

reverse = reverse + original.charAt(i);

System.out.println("Reverse of entered string is: "+reverse);

}

}

Output

Enter a string to reverse I am a reverse

Reverse of entered string is: esrever a ma I

Retrieve a Substring in a String

import java.util.Scanner;

class SubstringsOfAString

{

public static void main(String args[])

{

String str, sub;

int i, c, length;

Scanner sct = new Scanner(System.in);

System.out.println("Enter  string to print all it's substrings:");

str = sct.nextLine(); // User input

length = str.length();

System.out.println("Substrings of \""+str+"\" are :-");

for( c = 0 ; c < length ; c++ )// Calculating all the substring possible

{

for( i = 1 ; i<= length - c ; i++ )

{

sub = str.substring(c, c+i);

System.out.println(sub);

}

}

}

}

Output

Enter a string to print all substrings: funny

Substrings of “funny” are :-

 

f

fu

fun

funn

funny

 

u

un

unn

unny

 

n

nn

nny

 

n

ny

 

y

Searching using Linear Search Technique

import java.util.Scanner;

class LinearSearch

{

public static void main(String args[])

{

int c, n, search, array[];

Scanner sct = new Scanner(System.in);

System.out.println("Enter number of elements");

n = sct.nextInt();

array = new int[n];

System.out.println("Enter " + n + " integers");

for (c = 0; c < n; c++) //User input of array values

{

array[c] = sct.nextInt();

}

System.out.println("Enter value to find"); //User input of number to Search

search = sct.nextInt();

for (c = 0; c < n; c++)

{

if (array[c] == search)     // element is present

{

System.out.println(search + "  present at location " + (c + 1) + ".");

break;

}

}

if (c == n)  /* element is absent */

System.out.println(search + "  not present inside array.");

}

}

Output

Enter number of elements 5

Enter 5 integers 8 9 7 5 0

Enter value to find 5

5  present at location 4.

Search using Binary Search Technique

The binary search is quick and is used in sorted array. The binary search starts the element search from middle of the array and proceeds further up or down the array depending on the mid value of the array.

import java.util.Scanner;

class BinarySearch

{

public static void main(String args[])

{

int c, first, last, middle, n, search, array[];

Scanner sct = new Scanner(System.in);

System.out.println("Enter number of elements");

n = sct.nextInt();

array = new int[n];

System.out.println("Enter " + n + " integers");

for (c = 0; c < n; c++)

{

array[c] = sct.nextInt();

}

System.out.println("Enter value to find");

search = sct.nextInt();

first  = 0; // Basic binary search

last   = n - 1;

middle = (first + last)/2;

while( first <= last )

{

if ( array[middle] < search )

first = middle + 1;

else if ( array[middle] == search )

{

System.out.println(search + " found at location " + (middle + 1) + ".");

break;

}

else

{

last = middle - 1;

}

middle = (first + last)/2;

if ( first > last ){

System.out.println(search + " is not present in the list.\n");

}

}

}

Output

Enter number of elements 5

Enter 5 integers 3 5 7 8 9

Enter value to find 8

8  present at location 4.