Java Programming Examples
Java Programming Examples
Output of program:
Output:
Output:
Output of program:
}
}
Output of program:
Above program uses nested for loops (for loop inside a for loop) to print stars.
You can also use spaces to create another pattern, It is left for you as an exercise.
Output of program:
Output of program:
You can easily modify the above java program to print alphabets in upper case.
Download Alphabets program class file.
Output of program:
Output of program:
System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float "+b);
Output of program:
Java program to add two numbers :- Given below is the code of java program that
adds two numbers which are entered by the user.
Output of program:
This java program finds if a number is odd or even. If the number is divisible by 2
then it will be even, otherwise it is odd. We use modulus operator to find
remainder in our program.
if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
Output of program:
Java static method program: static methods in Java can be called without
creating an object of class. Have you noticed why we write static keyword when
defining main it's because program execution begins from main and no object has
been created yet. Consider the example below to improve your understanding of
static methods.
Output of program:
Output of code:
Java program can contain more than one i.e. multiple classes. Following example
Java program contain two classes: Computer and Laptop. Both classes have their
own constructors and a method. In main method we create object of two classes
and call their methods.
class Laptop {
Laptop() {
System.out.println("Constructor of Laptop class.");
}
void laptop_method() {
System.out.println("99% Battery available.");
}
Output of program:
Constructor java tutorial: Java constructors are the methods which are used to
initialize objects. Constructor method has the same name as that of class, they are
called or invoked when an object of class is created and can't be called explicitly.
Attributes of an object may be available when creating objects if no attribute is
available then default constructor is called, also some of the attributes may be
known initially. It is optional to write constructor method in a class but due to
their utility they are used.
Output of program:
Java exception handling tutorial: In this tutorial we will learn how to handle
exception with the help of suitable examples. Exceptions are errors which occur
when the program is executing. Consider the Java program below which divides
two integers.
import java.util.Scanner;
class Division {
public static void main(String[] args) {
int a, b, result;
Scanner input = new Scanner(System.in);
System.out.println("Input two integers");
a = input.nextInt();
b = input.nextInt();
result = a / b;
Now we compile and execute the above code two times, see the output of program
in two cases:
This java program swaps two numbers using a temporary variable. To swap
numbers without using extra variable see another code below.
This java program finds largest of three numbers and then prints it. If the entered
numbers are unequal then "numbers are not distinct" is printed.
import java.util.Scanner;
class LargestOfThreeNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if ( x > y && x > z )
System.out.println("First number is largest.");
else if ( y > x && y > z )
System.out.println("Second number is largest.");
else if ( z > x && z > y )
System.out.println("Third number is largest.");
else
System.out.println("Entered numbers are not distinct.");
}
Enhanced for loop java: Enhanced for loop is useful when scanning the array
instead of using for loop. Syntax of enhanced for loop is:
for (data_type variable: array_name)
This java program finds factorial of a number. Entered number is checked first if
its negative then an error message is printed.
Output of program:
This java program prints prime numbers, number of prime numbers required is
asked from the user. Remember that smallest prime number is 2.
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
static int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
}
return p;
System.out.println();
This java program reverses a string entered by the user. We use charAt method to
extract characters from the string and append them in reverse order to reverse
the entered string.
Output of program:
Output of program:
This program compare strings i.e test whether two strings are equal or not,
compareTo method of String class is used to test equality of two String class
objects. compareTo method is case sensitive i.e "java" and "Java" are two
different strings if you use compareTo method. If you wish to compare strings but
ignoring the case then use compareToIgnoreCase method.
Java program for linear search: Linear search is very simple, To check if an
element is present in the given list we compare search element with every
element in the list. If the number is found then success occurs otherwise the list
doesn't contain the element we are searching.
Output of program:
Java program for binary search: This code implements binary search algorithm.
Please note input numbers must be in ascending order.
".");
Java program to find substrings of a string :- This program find all substrings of a
string and the prints them. For example substrings of "fun" are :- "f", "fu", "fun",
"u", "un" and "n". substring method of String class is used to find substring. Java
code to print substrings of a string is given below.
Java date and time program :- Java code to print or display current system date
and time. This program prints current date and time. We are using
GregorianCalendar class in our program. Java code to print date and time is given
below :-
"+day+"/"+(month+1)+"/"+year);
"+hour+" : "+minute+" :
Output of program:
This program prints reverse of a number i.e. if the input is 951 then output will be
159.
Output of program: