Java Program Documents
Java Program Documents
.
JAVA
• Java programming language was released by James Gosling in the year
of 1995, so he is called as the father of JAVA.
• He invented JAVA at Sun Micro systems INC, USA.
• Later the copyrights are given to Oracle Corporation, now this Free and
Open Source (OpenJDK) software is running in around 3 Billion devices.
• Java is used to build various software applications, such as
• Mobile Applications,
• Web Applications,
• Games,
• stand alone desktop applications and more.
What is Software?
• Collection of Programs meant for performing some operations or
obtaining some objectives.
• In Real time scenario, based on operations we have, software's are
classified into 3 types.
1. System Software
2. Application Software
3. Internet Software
System Software:
• The Stand Alone applications run in the context of single machine and
whose results can be accessed in the same machine but not in the other
machines. All the System Software's comes under Stand Alone
application. In Real time, three projects comes under System Software
development.
• They are:
a) Development of Device Drivers
b) Development of Utility Programs: i.e. Antivirus software.
c) Development of Real time Operating System:
Examples of Operating System are Windows, UNIX, LINUX, Mac, etc.
2. Application Software:
• Application Software comes under the Stand Alone applications but it is
used to develop projects for small scale organizations.
• They can run only in particular organization.
• Examples of Application Software are: Supermarkets, Shopping Malls,
Health Care services etc.
3. Internet Software:
• Internet software is used to develop distributed applications. These applications
runs in the context of Browser/www and whose results can be accessed across the
universe.
Examples of Internet Software are:
1) Development of Websites.
2) Development of ATM applications.
3) Development of credit/debit applications.
4) Development of net banking applications.
5) Development of all e-commerce applications.
• Now, Let's see what is JAVA?
"JAVA is an internet software used for developing distributed applications and used
by other technology programmers for developing their applications.“
Java is case-sensitive
Why Use Java?
• Java works on different platforms (Windows, Mac, Linux, etc.)
• It is one of the most popular programming language in the
world
• It has a large demand in the current job market
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has a huge community support (tens of millions of
developers)
• Java is an object oriented language which gives a clear
structure to programs and allows code to be reused, lowering
development costs
Java Install
To install Java on Windows:
1.Go to "System Properties" (Can be found on Control Panel > System and Security > System
> Advanced System Settings)
2.Click on the "Environment variables" button under the "Advanced" tab
3.Then, select the "Path" variable in System variables and click on the "Edit" button
4.Click on the "New" button and add the path where Java is installed, followed by \bin. By
default, Java is installed in C:\Program Files\Java\jdk-11.0.1 (If nothing else was specified
when you installed it). In that case, You will have to add a new path with: C:\Program
Files\Java\jdk-11.0.1\bin
Then, click "OK", and save the settings
OR
1. Install jdkc driveprogramsjavabincopy its path*(1)
2. Searchedit the system environment variablesAdvanceenvironment variablesSelect on PATH under
system variableEditNewpaste Java path*(1)ok
3. environment variablesSelect on NewJava_HomePaste the java path without binok
4.At last, open Command Prompt (cmd.exe) and type java -version to see if Java is running
on your machine
Now write a program in notepad and save it with same class
name in Java folder
class Hello{
public static void main(String args[]){
System.out.println(“Hello Welcome to ITI Chaibasa");
}
}
Now, click the java folder address tab & type cmdnow type the below details
• hello Output
Example explained
// this is hello program
class Hello{
public static void main(String args[]){
System.out.println(“Hello Welcome to ITI Chaibasa");
}
}
• We use // for single comments, and /*……….*/ for multi line comment.
• Every line of code that runs in Java must be inside a class followed by a class name.
• public: lt declares that the file access has no restriction.
• static: It is a keyword which instruct compiler, so that main should run before any thing else in the program.
• void: It indicates that main() will not return anything.
• String args[]: It declares a string array.
• The name of the java file must match the class name & with ".java" as a extension name.
• main() is a method/function.
Syntax:
Type variablename=value;
Ex.
int myNum=20; //single variable declaration
int x=3,y=4,z=4; //multiple variable declaration
Ex.
public class Main {
public static void main(String[] args) {
String name = "John";
System.out.println(name);
}
}
To combine both text and a variable, use the + character:
Ex.
public class Main {
public static void main(String[] args) {
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
}
}
If….else statement
If(expression)
{
Statement if expression is true
}
else
{
Statement if expression is false
}
Ex.
public class Main {
public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
If…else with checkAge() call method
public class Main {
static void checkAge(int age) {
} else {
System.out.println("Access granted - You are old enough!");
}
System.out.println("Enter username");
userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
Output:
Enter username:
ex. i.e. let xyz
Username is: xyz
Explanation:
Cont…..
Cont..
class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
The animal makes a sound
The dog says: bow wow
Java Abstraction
System.out.println("Zzz");
int x, y, sum;
System.out.println("Type a number:");
sum = x + y;
}
OUTPUT:
Type a number:
10
Sum is: 20
Reverse a String
public class Main {
public static void main(String[] args) {
String originalStr = "Hello";
String reversedStr = "";
System.out.println("Original string: " + originalStr);
• if (temp.equalsIgnoreCase (str)) //compare original string available in temp with this reversed String
• System.out.println (temp + " is palindrome");
• else
• System.out.println (temp + " is not palindrome");
Java code to find ASCII value of a character:
• import java.util.Scanner;
• class Programming9
• {
• public static void main(String[] args)
• {
• //scanner is a class for creating an object
• Scanner sc=new Scanner(System.in);
1
121
12321
1234321
123454321
12345654321
Java Program to Calculate BMI (Body Mass Index) Value
• import java.io.*;
• import java.lang.*;
• class BmiCalc {
• public static void main(String args[]) throws IOException {
• float h, w, bmi;
• try {
• BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
• System.out.println("enter the weight(in kgs):");
• w = Float.parseFloat(br.readLine());
• System.out.println("enter the height(in meters):");
• h = Float.parseFloat(br.readLine());
• bmi = w / (h * h);
• System.out.println("your BMI(body mass index) value is" + bmi);
• System.out.println("if your BMI is '<18' you are under weight");
• System.out.println("if your BMI is in between '18 and 24 ' you are normal weight");
• System.out.println("if your BMI is '>25' you are under weight");
• System.out.println("if your BMI is '>30' you are suffering from obesity");
• } catch (NumberFormatException a)
• {
• System.out.println(a);
• }
• }
• }
Java Program to Find Sum of Individual Digits of a Given Number
• import java.io.BufferedReader;
• import java.io.IOException;
• import java.io.InputStreamReader;
• class SumOfDigits {
• public static void main(String args[]) throws IOException {
• BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
• System.out.println("Enter the value to do the sum :");
• int n = Integer.parseInt(br.readLine());
• int sum = 0, k;
• while (n != 0) {
• k = n % 10;
• sum = sum + k;
• n = n / 10;
• }
• System.out.print("\nSum of individual digits : " + sum);
• }
• }
Java Program for Printing Natural Numbers up to Given Number
• import java.io.*;
• import java.lang.*;
• class Num
• {
• public static void main(String args[])throws IOException
• {
• BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
• System.out.println("Enter the number:");
• int n=Integer.parseInt(br.readLine());
• System.out.println("Numbers are");
• for(int i=1; i<=n; i++)
• System.out.println(i);
• }
• }
Java Program For Concatenation of Two Strings
• import java.io.*;
• import java.lang.*;
• class Concatenation
• {
• public static void main(String args[])throws IOException
• {
• BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
• String s1,s2;
• System.out.println("Enter First String: ");
• s1=br.readLine();
• System.out.println("Enter Second String: ");
• s2=br.readLine();
• System.out.println("Concatination of s1 and s2 :"+s1.concat(s2));
• }
• }
Java Program to Find Factorial of a Given Number using For Loop
• import java.io.*;
• import java.lang.*;
• class Factorial
• {
• public static void main(String args[]) throws IOException
• {
• BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
• System.out.println("Enter Number: ");
• int num=Integer.parseInt(br.readLine());
• int fact=1;
• for(int i=1; i<=num; i++)
• {
• fact=fact*i;
• }
• System.out.println("Factorial of a Given Number is : "+fact);
• }
• }
Java Program to Reverse a Number
• import java.io.*;
• import java.lang.*;
• class Reverse
• {
• public static void main(String args[]) throws IOException
• {
• BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
• System.out.println("Enter Number: ");
• int num=Integer.parseInt(br.readLine());