♠ Posted by
Java Tutorials
at
11:38 PM
Page 1|2|3|4
1.
Java programming source code
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello
World");
}
}
Output of program:
Hello World is passed as an
argument to println method, you can print whatever you want. There is also a
print method which doesn't takes the cursor to beginning of next line as
println does. System is a class, out is object of PrintStream class and println
is the method.
This program print
alphabets on screen i.e a, b, c, ..., z.
2.
Java source code
class Alphabets
{
public static void main(String args[])
{
char ch;
for( ch = 'a' ; ch <= 'z' ; ch++ )
System.out.println(ch);
}
}
Output of program:
Printing alphabets using while loop (only body of main method is shown:
char c = 'a';
while (c <= 'z') {
System.out.println(c);
c++;
}
Using do while loop:
char c = 'A';
do {
System.out.println(c);
c++;
} while (c <= 'Z');
This java program prints
multiplication table of a number entered by the user using a for loop.
3.
Java programming source code
import java.util.Scanner;
class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an
integer to print it's multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication
table of "+n+" is :-");
for ( c = 1 ; c <= 10 ; c++ )
System.out.println(n+"*"+c+" = "+(n*c));
}
}
Using nested loops we can
print tables of number between a given range say a to b, For example if the
input numbers are 3 and 6 then tables of 3, 4, 5 and 6 will be printed. Code:
import java.util.Scanner;
class Tables
{
public static void main(String args[])
{
int a, b, c, d;
System.out.println("Enter
range of numbers to print their multiplication table");
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
for (c = a; c <= b; c++) {
System.out.println("Multiplication
table of "+c);
for (d = 1; d <= 10; d++) {
System.out.println(c+"*"+d+" = "+(c*d));
}
}
}
}
·
This program tells you how
to get input from user in a java program. We are using Scanner class to get
input from user. This program firstly asks the user to enter a string and then
the string is printed, then an integer and entered integer is also printed and
finally a float and it is also printed on the screen. Scanner class is present
in java.util package so we import this package in our program. We first create
an object of Scanner class and then we use the methods of Scanner class.
Consider the statement
Scanner a = new Scanner(System.in);
Here Scanner is the class
name, a is the name of object, new keyword is used to allocate the memory and
System.in is the input stream. Following methods of Scanner class are used in
the program below :-
1) nextInt to input an integer
2) nextFloat to input a float
3) nextLine to input a string
1) nextInt to input an integer
2) nextFloat to input a float
3) nextLine to input a string
4.
Java programming source code
import java.util.Scanner;
class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a
string");
s = in.nextLine();
System.out.println("You
entered string "+s);
System.out.println("Enter an
integer");
a = in.nextInt();
System.out.println("You
entered integer "+a);
System.out.println("Enter a
float");
b = in.nextFloat();
System.out.println("You entered
float "+b);
}
}
Output of program:
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.
5.
Java programming source code
import java.util.Scanner;
class AddNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter two
integers to calculate their sum ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of
entered integers = "+z);
}
}
Output of program:
Above code can add only numbers in range of integers(4 bytes), if you wish to add very large numbers then you can use BigInteger class. Code to add very large numbers:
Above code can add only numbers in range of integers(4 bytes), if you wish to add very large numbers then you can use BigInteger class. Code to add very large numbers:
import java.util.Scanner;
import java.math.BigInteger;
class AddingLargeNumbers {
public static void main(String[] args) {
String number1, number2;
Scanner in = new Scanner(System.in);
System.out.println("Enter
first large number");
number1 = in.nextLine();
System.out.println("Enter
second large number");
number2 = in.nextLine();
BigInteger first = new BigInteger(number1);
BigInteger second = new BigInteger(number2);
System.out.println("Addition =
" + first.add(second));
}
}
In our code we create two
objects of BigInteger class in java.math package. Input should be digit strings
otherwise an exception will be raised, also you cannot simply use '+' operator
to add objects of BigInteger class, you have to use add method for addition of
two objects.
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.
6.
Java programming source code
import java.util.Scanner;
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an
integer to check if it is odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if ( x % 2 == 0 )
System.out.println("You
entered an even number.");
else
System.out.println("You
entered an odd number.");
}
}
import java.util.Scanner;
class EvenOdd
{
public static void main(String args[])
{
int c;
System.out.println("Input an
integer");
Scanner in = new Scanner(System.in);
c = in.nextInt();
if ( (c/2)*2 == c )
System.out.println("Even");
else
System.out.println("Odd");
}
}
There are other methods for
checking odd/even one such method is using bitwise operator.
7.
Java program to convert Fahrenheit to Celsius
Java program to convert
Fahrenheit to Celsius: This code does temperature conversion from Fahrenheit
scale to Celsius scale.
import java.util.*;
class FahrenheitToCelsius {
public static void main(String[] args) {
float temperatue;
Scanner in = new Scanner(System.in);
System.out.println("Enter
temperatue in Fahrenheit");
temperatue = in.nextInt();
temperatue = (temperatue - 32)*5/9;
System.out.println("Temperatue
in Celsius = " + temperatue);
}
}
For Celsius to Fahrenheit
conversion use
T = 9*T/5 + 32
where T is temperature on Celsius scale.
where T is temperature on Celsius scale.
8.
Java program to bubble sort
Java program to bubble
sort: This code sorts numbers inputted by user using Bubble sort algorithm.
import java.util.Scanner;
class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input
number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter
" + n + "
integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For
descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted
list of numbers");
for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}
Complexity of bubble sort
is O(n2) which makes it a less frequent option for arranging in
sorted order when quantity of numbers is high.
You can also use sort
method of Arrays class to sort integers in ascending order but remember that
sort method uses a variation of Quick sort algorithm.
import java.util.Arrays;
class Sort
{
public static void main(String args[])
{
int data[] = { 4, -5, 2, 6, 1 };
Arrays.sort(data);
for (int c: data)
{
System.out.println(c);
}
}
}
Java programming language
offers a block known as static which is executed before main method executes.
Below is the simplest example to understand functioning of static block later
we see a practical use of static block.
9.
Java static block program
class StaticBlock {
public static void main(String[] args) {
System.out.println("Main
method is executed.");
}
static {
System.out.println("Static
block is executed before main method.");
}
}
Static block can be used to
check conditions before execution of main begin, Suppose we have developed an
application which runs only on Windows operating system then we need to check
what operating system is installed on user machine. In our java code we check
what operating system user is using if user is using operating system other
than "Windows" then the program terminates.
class StaticBlock {
public static void main(String[] args) {
System.out.println("You are
using Windows operating system.");
}
static {
String os = System.getenv("OS");
if (os.equals("Windows_NT") != true) {
System.exit(1);
}
}
}
We are using getenv method
of System class which returns value of environment variable name of which is
passed an as argument to it. Windows_NT is a family of operating systems which
includes Windows XP, Vista, 7, 8 and others.
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.
0 comments :
Post a Comment