Java Programs Document
Java Programs Document
Create a test
project, add a test class, and run it. See how you can use auto suggestions, auto fill.
Try code formatter and code refactoring like renaming variables, methods, and
classes. Try debug step by step with a small program of about 10 to 15 lines which
contains at least one if else condition and a for loop.
Program :
import java.util.*;
public class TestProject
{
public static void main(String[] args)
{
System.out.println("Welcome to Bharat Institute Of Engineering & Technology");
Scanner sc = new Scanner(System.in);
System.out.println("Enter valid Number");
int n = sc.nextInt();
int c = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
System.out.println(n + "is Prime Number");
}
else
{
System.out.println(n + "is not Prime Number");
}
}
}
i)if
Syntax:
if(condition){
//code to be executed
}
Example:
Output:
ii) Syntax:
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
Example:
iii) Syntax:
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }
Example:
Output:
20
iv) For loop
Syntax:
1. for(initialization;condition;incr/decr){
2. //code to be executed
3. }
4. public class ForExample {
5. public static void main(String[] args) {
6. for(int i=1;i<=10;i++){
7. System.out.println(i);
8. }
9. }
10. }
Output:
1
2
3
4
5
v)foreach loop
syntax :
Example program :
Int arr[]={10,12,15,12};
for(int i:arr)
System.out.println(i);
Example:
Output:
1
2
3
4
5
6
7
8
9
10
Syntax:
1. do{
2. //code to be executed
3. }while(condition);
Example:
Output:
1
2
3
4
5
6
7
8
9
10
Example:
1. public class BreakExample {
2. public static void main(String[] args) {
3. for(int i=1;i<=10;i++){
4. if(i==5){
5. break;
6. }
7. System.out.println(i);
8. }
9. }
10. }
Output:
1
2
3
4
Example:
Output:
1
2
3
4
6
7
8
9
10
3)Constructor program
1. class Bike1{
2. Bike1(){System.out.println("Bike is created");}
3. public static void main(String args[]){
4. Bike1 b=new Bike1();
5. }
6. }
Output:
Bike is created
Output:
0 null
0 null
1. class Student4{
2. int id;
3. String name;
4.
5. Student4(int i,String n){
6. id = i;
7. name = n;
8. }
9. void display(){System.out.println(id+" "+name);}
10.
11. public static void main(String args[]){
12. Student4 s1 = new Student4(111,"Karan");
13. Student4 s2 = new Student4(222,"Aryan");
14. s1.display();
15. s2.display();
16. }
17. }
Test it Now
Output:
111 Karan
222 Aryan
1. class Adder{
2. static int add(int a,int b){return a+b;}
3. static int add(int a,int b,int c){return a+b+c;}
4. }
5. class TestOverloading1{
6. public static void main(String[] args){
7. System.out.println(Adder.add(11,11));
8. System.out.println(Adder.add(11,11,11));
9. }}
Output:
22
33
1. class Adder{
2. static int add(int a, int b){return a+b;}
3. static double add(double a, double b){return a+b;}
4. }
5. class TestOverloading2{
6. public static void main(String[] args){
7. System.out.println(Adder.add(11,11));
8. System.out.println(Adder.add(12.3,12.6));
9. }}
Test it Now
Output:
22
24.9
Example of method overriding
In this example, we have defined the run method in the subclass as defined in the parent
class but it has some specific implementation. The name and parameter of the method is
same and there is IS-A relationship between the classes, so there is method overriding.
1. class Vehicle{
2. void run(){System.out.println("Vehicle is running");}
3. }
4. class Bike2 extends Vehicle{
5. void run(){System.out.println("Bike is running safely");}
6.
7. public static void main(String args[]){
8. Bike2 obj = new Bike2();
9. obj.run();
10. }
Test it Now
Type Casting
Assigning a value of one type to a variable of another type is known as Type Casting.
Example :
int x = 10;
byte y = (byte)x;
• Widening Casting(Implicit)
• Narrowing Casting(Explicitly done)
Example :
public class Test
int i = 100;
}
}
Output :
Int value 100
double d = 100.04;
Output :
Double value 100.04
----------------------------------------------------------------------------------------------------------------------------- -------------
6) FACTORIAL PROGRAM
Factorial Program using loop in java
Let's see the factorial Program using loop in java.
1. class FactorialExample{
2. public static void main(String args[]){
3. int i,fact=1;
4. int number=5;//It is the number to calculate factorial
5. for(i=1;i<=number;i++){
6. fact=fact*i;
7. }
8. System.out.println("Factorial of "+number+" is: "+fact);
9. }
10. }
Output:
1. class FactorialExample2{
2. static int factorial(int n){
3. if (n == 0)
4. return 1;
5. else
6. return(n * factorial(n-1));
7. }
8. public static void main(String args[]){
9. int i,fact=1;
10. int number=4;//It is the number to calculate factorial
11. fact = factorial(number);
12. System.out.println("Factorial of "+number+" is: "+fact);
13. }
14. }
Output:
Factorial of 4 is: 24
7)INHERITANCE PROGRAMS
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class TestInheritance{
8. public static void main(String args[]){
9. Dog d=new Dog();
10. d.bark();
11. d.eat();
12. }}
Output:
barking...
eating...
Output:
weeping...
barking...
eating...
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class Cat extends Animal{
8. void meow(){System.out.println("meowing...");}
9. }
10. class TestInheritance3{
11. public static void main(String args[]){
12. Cat c=new Cat();
13. c.meow();
14. c.eat();
15. //c.bark();//C.T.Error
16. }}
Output:
meowing...
eating...
8)ABSTRACT CLASS,INTERFACE,PACKAGES
abstract method
A method that is declared as abstract and does not have implementation is known as
abstract method.
EX1
running safely..
EX 2
1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }
Test it Now
Output:
Hello
EX2
File: TestInterface1.java
Output:
drawing circle
1. //save as Simple.java
2. package mypack;
3. public class Simple{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }
1. import package.*;
2. import package.classname;
1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible
but not subpackages.
The import keyword is used to make the classes and interface of another package accessible
to the current package.
2) Using packagename.classname
If you import package.classname then only declared class of this package will be accessible.
It is generally used when two packages have same class name e.g. java.util and java.sql
packages contain Date class.
1. import java.io.Serializable;
2. public class Student implements Serializable{
3. int id;
4. String name;
5. public Student(int id, String name) {
6. this.id = id;
7. this.name = name;
8. }
9. }
1. import java.io.*;
2. class Persist{
3. public static void main(String args[])throws Exception{
4. Student s1 =new Student(211,"ravi");
5.
6. FileOutputStream fout=new FileOutputStream("f.txt");
7. ObjectOutputStream out=new ObjectOutputStream(fout);
8.
9. out.writeObject(s1);
10. out.flush();
11. System.out.println("success");
12. }
13. }
success
10)ENUMERATION PROGRAM
Java Enum
Enum in java is a data type that contains fixed set of constants.
It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc.
The java enum constants are static and final implicitly. It is available from JDK 1.5.
Java Enums can be thought of as classes that have fixed set of constants.
o enum may implement many interfaces but cannot extend any class because it
internally extends Enum class
Output:WINTER
SPRING
SUMMER
FALL