2023
Java Programming- i
JAVA PROGRAMMING- I
1. Print “Hello World”.
Input:
package student;
/**
* @author YASH
*/
public class hello {
public static void main(String[] args) {
// TODO code application logic here
[Link](" Hello worldl");
Output:
run:
Hello worldl
BUILD SUCCESSFUL (total time: 0 seconds)
2023 1
JAVA PROGRAMMING- I
[Link] are of circle:
Input:
package student;
/**
* @author YASH
*/
public class circle {
public static void main(String args[])
float PI=3.14f,r=10.0f,area;
area=PI*r*r;
[Link](" area of circle with radius " + r + " is:"+area);
Output:
run:
area of circle with radius 10.0 is:314.0
BUILD SUCCESSFUL (total time: 0 seconds)
2023 2
JAVA PROGRAMMING- I
3. Calculate are of square :
Input:
package student;
/**
* @author YASH
*/
public class quare {
public static void main(String args[])
int l=10;
float area;
area=l*l;
[Link](" area of square with length " + l + " is:"+area);
Output:
run:
area of square with length 10 is:100.0
BUILD SUCCESSFUL (total time: 0 seconds)
2023 3
JAVA PROGRAMMING- I
4. Calculate are of rectangle:
Input:
package student;
/**
* @author YASH
*/
public class rectangle {
public static void main(String args[]){
int l=10;
int b=25;
float area;
area=l*b;
[Link](" area of rectangle with length " + l +"And height"+b+ "
is:"+area);
Output:
run:
area of square with length 10And height25 is:250.0
BUILD SUCCESSFUL (total time: 0 seconds)
2023 4
JAVA PROGRAMMING- I
[Link] are of triangle:
Input:
package student;
/**
* @author YASH
*/
public class triangle {
public static void main(String args[]){
int h=10;
int b=25;
float area;
area=0.5f *h*b;
[Link](" area of triangle with height " + h +"And baset"+b+ " is:"+area);
Output:
run:
area of triangle with height 10And baset25 is:125.0
BUILD SUCCESSFUL (total time: 0 seconds)
2023 5
JAVA PROGRAMMING- I
6. add two integer numbers. (Take input form user):
Input:
package student;
import [Link];
import [Link];
import [Link];
/** @author YASH
*/
public class add {
public static void main(String[] args)throws IOException {
InputStreamReader ir = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(ir);
double n1,n2,n3;
[Link]("Enter n1 : ");
n1=[Link]([Link]());
[Link]("Enter n1 : ");
n2=[Link]([Link]());
n3 = n1 + n2;
[Link]("n1 = "+n1);
[Link]("n2 = "+n2);
[Link]("Sum = "+n3);
Output:
run:
Enter n1 : 12
Enter n1 : 20
n1 = 12.0
n2 = 20.0
Sum = 32.0
BUILD SUCCESSFUL (total time: 5 seconds)
2023 6
JAVA PROGRAMMING- I
7. Calculate are of circle: (Take input form user)
input:
package student;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
public class circle {
public static void main(String args[])throws IOException
InputStreamReader ir = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(ir);
float PI=3.14f,area, r;
[Link]("Enter radius: ");
r=[Link]([Link]());
area=PI*r*r;
[Link](" area of circle with radius " + r + " is:"+area);
Output:
run:
Enter radius: 12
area of circle with radius 12.0 is:452.16
BUILD SUCCESSFUL (total time: 3 seconds)
2023 7
JAVA PROGRAMMING- I
8. Calculate are of square: (Take input form user)
Input:
package student;
import [Link];
import [Link];
import static [Link];
/**
* @author YASH
*/
public class quare {
public static void main(String args[]) throws IOException
InputStreamReader ir = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(ir);
[Link]("Enter length : ");
l=[Link]([Link]());
float area,l;
area=l*l;
[Link](" area of square with length " + l + " is:"+area);
Output:
run:
Enter length : 14
area of square with length 14.0 is:196.0
BUILD SUCCESSFUL (total time: 10 seconds)
2023 8
JAVA PROGRAMMING- I
9. Calculate are of rectangle: (Take input form user)
Input:
package student;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
public class rectangle {
public static void main(String args[])throws IOException{
InputStreamReader ir = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(ir);
float l,b, area;
[Link]("Enter length : ");
l=[Link]([Link]());
[Link]("Enter Height : ");
b=[Link]([Link]());
area=l*b;
[Link](" area of square with length " + l +"And height"+b+ " is:"+area);
Output:
run:
Enter length : 12
Enter Height : 24
area of square with length 12.0And height24.0 is:288.0
BUILD SUCCESSFUL (total time: 7 seconds)
2023 9
JAVA PROGRAMMING- I
10. Calculate are of triangle: (Take input form user)
Input:
package student;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
public class triangle {
public static void main(String args[])throws IOException{
InputStreamReader ir = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(ir);
float h, b, area;
[Link]("Enter height : ");
h=[Link]([Link]());
[Link]("Enter base : ");
b=[Link]([Link]());
area=0.5f *h*b;
[Link](" area of triangle with height " + h +"And base"+b+ " is:"+area);
Output:
run:
Enter height : 14
Enter base : 17
area of triangle with height 14.0And base17.0 is:119.0
BUILD SUCCESSFUL (total time: 6 seconds)
2023 10
JAVA PROGRAMMING- I
[Link] of circle using Class mechanism:
Input:
package student;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
public class Mycircle
int r;
float area;
// default constructor
public Mycircle()
r = 10;
// parameterised constructor
public Mycircle(int pdim1)
r = pdim1;
// copy constructor
public Mycircle(Mycircle mc)
2023 11
JAVA PROGRAMMING- I
r = mc.r;
public void input_data() throws IOException
InputStreamReader ir = new InputStreamReader ( [Link]);
BufferedReader br = new BufferedReader ( ir);
[Link](" Enter radius :");
r = [Link]([Link]());
public void display_data()
[Link](" radius = "+ r);
[Link](" area = "+ area);
return;
public void cal_area()
area = 3.14f * r * r;
return;
public static void main(String[] args) throws IOException
// TODO code application logic here
InputStreamReader ir = new InputStreamReader ( [Link]);
BufferedReader br = new BufferedReader ( ir);
Mycircle mc = new Mycircle();
2023 12
JAVA PROGRAMMING- I
mc.input_data();
mc.cal_area();
mc.display_data();
Output :
run:
Enter radius :17
radius = 17
area = 907.46
BUILD SUCCESSFUL (total time: 1 minute 40 seconds)
2023 13
JAVA PROGRAMMING- I
12. write a java program which will perform operation on bank
1. Insert Customer Information
2. Display Information
3. Deposite Amount
4. Withdraw Amount
Input:
package student;
/**
* @author YASH
*/
import [Link].*;
class bank
int acno;
String name;
int balance;
public bank()
acno = 100;
name = "Patel Hatsh";
balance = 1000;
// parametrised
public bank( int pacno , String pname, int pbalance)
acno = pacno;
2023 14
JAVA PROGRAMMING- I
name = pname;
balance = pbalance;
// copy
public bank( bank pobj)
acno = [Link];
name = [Link];
balance = [Link];
public void input_data() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
[Link]("Enter account no : ");
acno=[Link]([Link]());
[Link]("Enter name : ");
name = [Link]();
[Link]("Enter balance : ");
balance =[Link]([Link]());
return;
public void display_data()
2023 15
JAVA PROGRAMMING- I
[Link]( "acno : "+ acno );
[Link]( "name : "+ name );
[Link]( "balance : "+ balance );
public void deposite() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
int deposite_amt;
[Link]("Enter deposite amount : ");
deposite_amt =[Link]([Link]());
balance = balance + deposite_amt;
return;
public void withdraw() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
int withdraw_amt;
[Link]("Enter withdraw amount : ");
withdraw_amt =[Link]([Link]());
if (( balance - withdraw_amt) > 500 )
balance = balance - withdraw_amt;
else
[Link](" Your Balance is not sufficient ...");
2023 16
JAVA PROGRAMMING- I
return;
public class bank_info {
public static void main(String a[])throws IOException
// create object
bank bob = new bank();
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
int choice;
do
[Link](" Main Menu ");
[Link]("");
[Link]("1. Insert Customer Information ");
[Link]("2. Display Information ");
[Link]("3. Deposite Amount ");
[Link]("4. Withdraw Amount ");
[Link]("5. Exit");
[Link]("Enter your choice : ");
choice =[Link]([Link]());
switch (choice)
2023 17
JAVA PROGRAMMING- I
case 1:
bob.input_data();
break;
case 2:
bob.display_data();
break;
case 3:
[Link]();
break;
case 4:
[Link]();
break;
case 5:
break;
default :
[Link]("Invalid Choice...");
}while( choice != 5) ;
return;
Output:
run:
Main Menu
1. Insert Customer Information
2023 18
JAVA PROGRAMMING- I
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
Enter your choice : 1
Enter account no : 839022
Enter name : Sadie Sink
Enter balance : 157000
Main Menu
1. Insert Customer Information
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
Enter your choice : 2
acno : 839022
name : Sadie Sink
balance : 157000
Main Menu
1. Insert Customer Information
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
Enter your choice : 3
Enter deposite amount : 43000
Main Menu
1. Insert Customer Information
2023 19
JAVA PROGRAMMING- I
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
Enter your choice : 4
Enter withdraw amount : 50000
Main Menu
1. Insert Customer Information
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
Enter your choice : 2
acno : 839022
name : Sadie Sink
balance : 150000
Main Menu
1. Insert Customer Information
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
Enter your choice :
2023 20
JAVA PROGRAMMING- I
13. Write a java program to input and display student's information
and also display student_result:
Input:
package student;
/**
* @author YASH
*/
import [Link].*;
// declare class
class student
// instance variables
int rno , s1, s2,s3;
String name, result;
public student()
rno = 101;
name = " Sadie Sink”
s1 = 55;
s2 = 66;
s3 = 77;
public student(int prno, String pname, int ps1, int ps2, int ps3)
rno = prno;
name = pname;
s1 = ps1;
2023 21
JAVA PROGRAMMING- I
s2 = ps2;
s3 = ps3;
public student( student pobj)
rno = [Link];
name = [Link];
s1 = pobj.s1;
s2 = pobj.s2;
s3 = pobj.s3;
public void inputdata()throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
[Link]("Enter Roll no= ");
rno=[Link]([Link]());
[Link]("Enter Name= ");
name=[Link]();
[Link]("Enter s1= ");
s1=[Link]([Link]());
[Link]("Enter s2= ");
s2=[Link]([Link]());
[Link]("Enter s3= ");
s3=[Link]([Link]());
return;
2023 22
JAVA PROGRAMMING- I
public void displaydata()
[Link]("rno= "+rno);
[Link](" Name : "+ name );
[Link]("s1= "+s1);
[Link]("s2= "+s2);
[Link]("s3= "+s3);
[Link]("Result = " + result);
return;
public void cal_result()
// local variables
int total;
float percent;
total = s1+s2+s3;
percent = total / 3.0f;
if ( percent >= 70 )
result = " Distinction ";
else if ( percent >=60 )
result = " First Class";
else if ( percent >= 50 )
result = " Second Class ";
else if ( percent >= 35 )
result = " Pass Class";
else
result = "Fail";
return;
2023 23
JAVA PROGRAMMING- I
public class stud_result {
public static void main(String args[])throws IOException
//create object
student st1 = new student();
// default constructor
student st2 = new student(102, "kepal rohit",44,55,66);
[Link]();
st1.cal_result();
[Link]();
return;
Output:
run:
Enter Roll no= 45
Enter Name= Lucas Sinkler
Enter s1= 56
Enter s2= 64
Enter s3= 50
rno= 45
Name : Lucas Sinkler
s1= 56
s2= 64
s3= 50
Result = Second Class
BUILD SUCCESSFUL (total time: 32 seconds)
2023 24
JAVA PROGRAMMING- I
[Link] program to insert and display array:
Input:
package myarray;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
public class array1 {
public static void main(String[] args)throws IOException {
InputStreamReader ir = new InputStreamReader ( [Link]);
BufferedReader br = new BufferedReader ( ir);
//declare array
int a[]= new int[10];
int i;
int n;
[Link]( " Enter n : ");
n= [Link]([Link]());
[Link]("Enter Array elements ");
for(i=0;i<n;i++){
[Link]("ENter a["+i+"]=");
a[i]=[Link]([Link]());
2023 25
JAVA PROGRAMMING- I
for( i=0; i<n ; i++)
[Link]("a[ "+i+" ] ="+a[i]);
}\
Output :
run:
Enter n : 7
Enter Array elements
ENter a[0]=1
ENter a[1]=2
ENter a[2]=3
ENter a[3]=4
ENter a[4]=5
ENter a[5]=6
ENter a[6]=7
a[ 0 ] =1
a[ 1 ] =2
a[ 2 ] =3
a[ 3 ] =4
a[ 4 ] =5
a[ 5 ] =6
a[ 6 ] =7
BUILD SUCCESSFUL (total time: 16 seconds)
2023 26
JAVA PROGRAMMING- I
[Link] program of linear search array:
Input:
package myarray;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
public class linear_search {
public static void main(String[] args) throws IOException
// TODO code application logic here
InputStreamReader ir = new InputStreamReader ( [Link]);
BufferedReader br = new BufferedReader ( ir);
// decalre array
int a[] = new int [10];
int n; // total number of elements
int i,item, pos;
[Link]( " Enter n : ");
n= [Link]([Link]());
[Link]("Enter Array elements ");
for( i=0; i<n ; i++)
[Link]("Enter a[ "+i+" ] =");
a[i] = [Link]([Link]());
2023 27
JAVA PROGRAMMING- I
[Link]("Array elements are");
for( i=0; i<n ; i++)
[Link]("a[ "+i+" ] ="+a[i]);
[Link]( " Enter item to search : ");
item= [Link]([Link]());
pos = -1;
for( i=0; i<n ; i++)
if (a[i] == item )
pos = i;
[Link]( item +" Found at position "+ pos);
return;
if ( pos == -1 )
[Link]( item+ " Not Found ...");
Output:
run:
Enter n : 7
Enter Array elements
2023 28
JAVA PROGRAMMING- I
Enter a[ 0 ] =1
Enter a[ 1 ] =2
Enter a[ 2 ] =3
Enter a[ 3 ] =5
Enter a[ 4 ] =4
Enter a[ 5 ] =7
Enter a[ 6 ] =6
Array elements are
a[ 0 ] =1
a[ 1 ] =2
a[ 2 ] =3
a[ 3 ] =5
a[ 4 ] =4
a[ 5 ] =7
a[ 6 ] =6
Enter item to search : 5
5 Found at position 3
BUILD SUCCESSFUL (total time: 22 seconds)
2023 29
JAVA PROGRAMMING- I
16. Write program of bubble search array:
Input:
package myarray;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
public class bubble_sort {
public static void main(String[] args) throws IOException
InputStreamReader ir = new InputStreamReader ( [Link]);
BufferedReader br = new BufferedReader ( ir);
// decalre array
// type array_name [] = new type [10];
int a[] = new int [10];
int n; // total number of elements
int i;
[Link]( " Enter n : ");
n= [Link]([Link]());
[Link]("Enter Array elements ");
for( i=0; i<n ; i++){
[Link]("Enter a[ "+i+" ] =");
a[i] = [Link]([Link]());
[Link]("Array elements are");
for( i=0; i<n ; i++)
[Link]("a[ "+i+" ] ="+a[i]);
2023 30
JAVA PROGRAMMING- I
// sort array using bubble sort method
int k,ptr,temp;
[Link]("\n sort array using bubble sort method \n");
for(k=0;k<n-1;k++)
for(ptr=0;ptr<n-k-1;ptr++)
/* > : for ascending order(incresing order)
< : for descending order(decresing order)
*/
if(a[ptr]>a[ptr+1])
temp=a[ptr];
a[ptr]=a[ptr+1];
a[ptr+1]=temp;
[Link]("Array elements are");
for( i=0; i<n ; i++)
[Link]("a[ "+i+" ] ="+a[i]);
Output:
run:
Enter n : 7
2023 31
JAVA PROGRAMMING- I
Enter Array elements
Enter a[ 0 ] =2
Enter a[ 1 ] =4
Enter a[ 2 ] =5
Enter a[ 3 ] =7
Enter a[ 4 ] =2
Enter a[ 5 ] =3
Enter a[ 6 ] =4
Array elements are
a[ 0 ] =2
a[ 1 ] =4
a[ 2 ] =5
a[ 3 ] =7
a[ 4 ] =2
a[ 5 ] =3
a[ 6 ] =4
sort array using bubble sort method
Array elements are
a[ 0 ] =2
a[ 1 ] =2
a[ 2 ] =3
a[ 3 ] =4
a[ 4 ] =4
a[ 5 ] =5
a[ 6 ] =7
BUILD SUCCESSFUL (total time: 13 seconds)
2023 32
JAVA PROGRAMMING- I
[Link] a program to input and display employees detail
Using single inheritance:
mployee_personal_info : eno, name , designation
emp_salary : basic_pay
Input:
package inheritance;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
class employee_personal_info
int empno;
String name;
String designation;
// constructor
public employee_personal_info()
empno = 101;
name = " patel s r";
designation = "manager";
//input_data()
public void input_data() throws IOException
2023 33
JAVA PROGRAMMING- I
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
[Link](" Enter Employee no : ");
empno = [Link]([Link]());
[Link](" Enter Employee name : ");
name = [Link]();
[Link](" Enter Employee Designation : ");
designation = [Link]();
return;
// display_data()
public void display_data()
[Link]("Employee no : "+empno);
[Link]("Employee name : "+name);
[Link]("Employee Designation : "+designation);
return;
} // end of base class
2023 34
JAVA PROGRAMMING- I
//derived class
public class employee_salary extends employee_personal_info
int basic_pay;
// constructor
public employee_salary()
super();
basic_pay = 12000;
//input_data()
public void input_data() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
super.input_data();
[Link](" Enter Employee Basic Pay : ");
basic_pay = [Link]([Link]());
return;
// display_data()
public void display_data()
super.display_data();
[Link](" Employee Basic Pay : "+basic_pay);
return;
2023 35
JAVA PROGRAMMING- I
public static void main(String args[]) throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
// create object here
employee_salary gscv = new employee_salary();
gscv.input_data();
gscv.display_data();
return;
Output:
run:
Enter Employee no : 103
Enter Employee name : Mike WHeeler
Enter Employee Designation : JR>Developer
Enter Employee Basic Pay : 20000
Employee no : 103
Employee name : Mike WHeeler
Employee Designation : JR>Developer
Employee Basic Pay : 20000
BUILD SUCCESSFUL (total time: 38 seconds)
2023 36
JAVA PROGRAMMING- I
[Link] a program to display student info and results using multilevel inheritance;
student_personal : rno , name
student_test : sub1,sub2,sub3
student_result : percent , result
student_personal --> student_test --> student_result
Input:
package inheritance;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
class student_personal
protected int rno;
protected String name;
// constructors
student_personal()
rno = 101;
name = "Patel D V ";
student_personal(int prno , String pname )
2023 37
JAVA PROGRAMMING- I
rno = prno;
name = pname;
public void input_data() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
[Link]("Enter Roll No :");
rno = [Link]([Link]());
[Link]("Enter Name :");
name = [Link]();
return;
public void display_data()
[Link]("Roll No : "+rno);
[Link](" Name :"+ name);
return;
// student_test : sub1,sub2,sub3 derived class 1
2023 38
JAVA PROGRAMMING- I
class student_test extends student_personal
protected int sub1 , sub2, sub3;
// constructor
public student_test()
super();
sub1 = 55;
sub2 = 66;
sub3 = 77;
public student_test(int prno, String pname, int psub1, int psub2, int psub3)
super(prno,pname);
sub1 = psub1;
sub2 = psub2;
sub3 = psub3;
public void input_data() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
super.input_data();
[Link]("Enter sub1 :");
sub1 = [Link]([Link]());
[Link]("Enter sub2 :");
2023 39
JAVA PROGRAMMING- I
sub2 = [Link]([Link]());
[Link]("Enter sub3 :");
sub3 = [Link]([Link]());
return;
public void display_data()
super.display_data();
[Link](" Sub1 = "+sub1 + " sub2 = "+ sub2+ " sub3 = "+sub3);
return;
// student_result : percent , result derived class 2
public class student_result extends student_test
// data members
float percent;
String result;
public student_result()
super();
public student_result(int prno,String pname, int psub1, int psub2, int psub3)
2023 40
JAVA PROGRAMMING- I
super(prno,pname,psub1,psub2,psub3);
public void input_data() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
super.input_data();
return;
public void display_data()
super.display_data();
[Link](" Percentage = "+ percent);
[Link]("Result = "+ result);
return;
public void cal_result()
percent = ( sub1 + sub2 + sub3) / 3.0f;
if ( percent >= 70)
result = "Distinction";
else if ( percent >= 60 )
result = " First Class";
else if ( percent >= 50 )
result = " Second Class";
else if ( percent >= 35)
result = "Pass Class";
else
result = " Fail";
2023 41
JAVA PROGRAMMING- I
return;
public static void main(String args[]) throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
// create object here
student_result ty = new student_result();
ty.input_data();
ty.cal_result();
ty.display_data();
return;
Output:
run:
Enter Roll No :02
Enter Name :Patel H M
Enter sub1 :40
Enter sub2 :41
Enter sub3 :48
Roll No : 2
Name :Patel H M
Sub1 = 40 sub2 = 41 sub3 = 48
Percentage = 43.0
Result = Pass Class
BUILD SUCCESSFUL (total time: 43 seconds)
2023 42
JAVA PROGRAMMING- I
19. Hierarchical Inheritance with Employees personal Detail Example.
general ( empno , name )--> non_tech ( design, extra_holiday )
general ( empno , name )--> tech ( department , salary )
Input:
package inheritance;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
// base class
class employee_general
protected int empno;
protected String name;
// constructor
public employee_general()
empno = 101;
name = " patel s r";
public employee_general(int pempno , String pname)
empno = pempno;
2023 43
JAVA PROGRAMMING- I
name = pname;
//input_data()
public void input_data() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
[Link](" Enter Employee no : ");
empno = [Link]([Link]());
[Link](" Enter Employee name : ");
name = [Link]();
return;
// display_data()
public void display_data()
[Link]("Employee no : "+empno);
[Link]("Employee name : "+name);
return;
} // end of base class
2023 44
JAVA PROGRAMMING- I
// general ( empno , name )--> non_tech ( design, extra_holiday )
//derived class
class non_tech extends employee_general
private int extra_holiday;
private String design;
// constructor
public non_tech()
super();
extra_holiday = 5;
design = "Supervisor";
public non_tech(int pempno , String pname,int pextra_holiday , String pdesign )
super(pempno , pname);
extra_holiday = pextra_holiday;
design = pdesign;
//input_data()
public void input_data() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
super.input_data(); // base class input_data()
[Link](" Enter Employee extra holiday : ");
2023 45
JAVA PROGRAMMING- I
extra_holiday = [Link]([Link]());
[Link](" Enter Employee designation : ");
design = [Link]();
return;
// display_data()
public void display_data()
super.display_data(); // base class
[Link](" Employee extra holiday : "+ extra_holiday);
[Link](" Employee designation : "+ design);
return;
// general ( empno , name )--> tech ( department , salary )
//derived class
class tech extends employee_general
private int salary;
private String department;
// constructor
public tech()
super();
salary = 5000;
department = "civil";
2023 46
JAVA PROGRAMMING- I
public tech(int pempno , String pname,int psalary , String pdepartment )
super(pempno , pname);
salary = psalary;
department = pdepartment;
//input_data()
public void input_data() throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
super.input_data(); // base class input_data()
[Link](" Enter Employee salary : ");
salary = [Link]([Link]());
[Link](" Enter Employee department : ");
department = [Link]();
return;
// display_data()
public void display_data()
super.display_data(); // base class
[Link](" Employee salary : "+ salary);
[Link](" Employee department : "+ department);
2023 47
JAVA PROGRAMMING- I
return;
public class hierarchical_inheritance_sample2
public static void main(String[] args) throws IOException
InputStreamReader ir=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(ir);
// TODO code application logic here
non_tech iti = new non_tech();
tech iit = new tech();
iti.input_data();
iti.display_data();
iit.input_data();
iit.display_data();
2023 48
JAVA PROGRAMMING- I
Output:
run:
Enter Employee no : 22
Enter Employee name : Patel H M
Enter Employee extra holiday : 4
Enter Employee designation : clark
Employee no : 22
Employee name : Patel H M
Employee extra holiday : 4
Employee designation : clark
Enter Employee no : 70
Enter Employee name : Patel S R
Enter Employee salary : 25000
Enter Employee department : civil
Employee no : 70
Employee name : Patel S R
Employee salary : 25000
Employee department : civil
BUILD SUCCESSFUL (total time: 1 minute 31 seconds)
2023 49
JAVA PROGRAMMING- I
20. make use of Abstract Class in java.
Input:
package abstract1;
import [Link];
import [Link];
import [Link];
/**
* @author YASH
*/
abstract class one_d
int dim1;
public one_d()
dim1=10;
public void input_data()throws IOException
InputStreamReader ir = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(ir);
[Link]("Enter dim1 : ");
dim1=[Link]([Link]());
return;
public abstract void display_data();
2023 50
JAVA PROGRAMMING- I
public abstract void cal_area();
class mycircle extends one_d
float area;
public void display_data() {
[Link]("Radius = "+dim1);
[Link]("area = "+area);
public void cal_area() {
area = 3.14f * dim1 * dim1;
class mysquare extends one_d
float area;
public void display_data() {
[Link]("Length = "+dim1);
[Link]("area = "+area);
2023 51
JAVA PROGRAMMING- I
public void cal_area() {
area = dim1 * dim1;
public class onedsahape {
public static void main(String args[])throws IOException
mycircle c = new mycircle ();
mysquare s = new mysquare();
[Link]("Area Of circle:");
c.input_data();
c.cal_area();
c.display_data();
[Link]("Area Of Square:");
s.input_data();
s.cal_area();
s.display_data();
2023 52
JAVA PROGRAMMING- I
Output:
run:
Area Of circle:
Enter dim1 : 14
Radius = 14
area = 615.44006
Area Of Square:
Enter dim1 : 24
Length = 24
area = 576.0
BUILD SUCCESSFUL (total time: 29 seconds)
2023 53
JAVA PROGRAMMING- I
21. make use of Interface in java
Input:
package myinterface;
/**
* @author YASH
*/
// Interface
interface Animal {
public void animalSound(); // interface method
public void sleep(); // interface method
// "implements" the Animal interface
class Dog implements Animal {
public void animalSound() {
[Link]("The Dog says: wee wee");
public void sleep() {
[Link]("woof!Yaffle!yawp!");
public static void main(String[] args) {
Dog myD = new Dog(); // Create a dog object
[Link]();
[Link]();
Output:
run:
The Dog says: wee wee
woof!Yaffle!yawp!
BUILD SUCCESSFUL (total time: 0 seconds)
2023 54
JAVA PROGRAMMING- I
22. make use of Package in java.
Input:
package student;
import [Link].*;
/**
* @author YASH
*/
public class my_circle extends Mycircle
// default constructor
float area;
public my_circle()
super(); // call constructor
public void cal_area()
area = 3.14f * super.r * super.r; // member variable 3.14 * r * r
// method overriding
public void display_data()
[Link](" radious = "+ r);
[Link](" area = "+ area);
return;
public static void main(String argv[])
2023 55
JAVA PROGRAMMING- I
my_circle mc = new my_circle();
mc.cal_area();
mc.display_data();
Output:
run:
radious = 10
area = 314.0
BUILD SUCCESSFUL (total time: 0 seconds)
2023 56
JAVA PROGRAMMING- I
[Link] use of data binding :
Input:
package data_binding; /**
* @author YASH
*/
class Animal
void eat()
[Link]("Animal is eating");
return;
// derived class
public class Dog extends Animal
// overriding eat() method of base class
void eat()
[Link](" Dog is eating ");
return;
public static void main(String args[])
Animal d1 = new Dog();
[Link]();
Output:
run: Dog is eating
BUILD SUCCESSFUL (total time: 0 seconds)
2023 57
JAVA PROGRAMMING- I
24. make use of Exception in java:
Input:
package myException;
/**
* @author YASH
*/
import [Link];
import [Link];
import [Link];
class File_notFound_Demo {
public static void main(String args[]) {
try {
// Following file does not exist
File file = new File("E://[Link]");
FileReader fr = new FileReader(file);
} catch (FileNotFoundException e) {
[Link]("File does not exist");
Output:
run:
File does not exist
BUILD SUCCESSFUL (total time: 0 seconds)
2023 58
JAVA PROGRAMMING- I
25. make use of try….catch….finally in java.
Input:
import [Link].*;
public class TryCatchExample{
public static void main(String args[]){
try{
[Link]("Inside try block");
int a[] = new int[2];
[Link]("A[3]:"+a[3]);
catch(ArrayIndexOutOfBoundsException e){
[Link]("Exception thrown :"+e);
finally{
[Link]("finally block");
[Link]("out of the block");
Output:
run:
Inside try block
Exception thrown :[Link]: 3
finally block
out of the block
BUILD SUCCESSFUL (total time: 0 seconds)
2023 59
JAVA PROGRAMMING- I
[Link] of type casting in java:
Input:
package casting;
/**
* @author YASH
*/
public class casting1 {
public static void main(String[] args)
double d = 100.04;
long l = (long)d; //explicit type casting required
int i = (int)l; //explicit type casting required
[Link]("Double value "+d);
[Link]("Long value "+l);
[Link]("Int value "+i);
Output:
run:
Double value 100.04
Long value 100
Int value 100
BUILD SUCCESSFUL (total time: 0 seconds)
2023 60
JAVA PROGRAMMING- I
27. make use of final keyword with class in java.
Input:
package casting;
/**
* @author YASH
*/
class dog{
final void display(){
[Link]("Example of Final.");
final class tulip extends dog{
final String animal;
tulip(String str){
animal = str;
void getex(){
[Link]("This is "+animal+".");
class FinalExample{
public static void main(String args[]){
tulip t = new tulip("A Dog");
[Link]();
[Link]();
}}
Output:
run:
Example of Final.
This is A Dog.
BUILD SUCCESSFUL (total time: 0 seconds)
2023 61
JAVA PROGRAMMING- I
28. make use of finalize keyword in java.
Input:
package casting;
/**
* @author YASH
*/
public class FinalizeExample {
public static void main(String[] args) throws Throwable{
FinalizeExample obj = new FinalizeExample();
obj = null;
[Link]();
[Link]("Hello World");
protected void finalize(){
[Link]("finalize method called");
Output:
run:
Hello World
finalize method called
BUILD SUCCESSFUL (total time: 0 seconds)
2023 62