Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Java Programming

Uploaded by

imamoddin15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Programming

Uploaded by

imamoddin15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

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

System.out.println(" Hello worldl");

Output:

run:

Hello worldl

BUILD SUCCESSFUL (total time: 0 seconds)

2023 1
JAVA PROGRAMMING- I

2.Calculate 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;

System.out.println(" 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;

System.out.println(" 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;

System.out.println(" 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

5.Calculate 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;

System.out.println(" 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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/** @author YASH

*/

public class add {

public static void main(String[] args)throws IOException {

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

double n1,n2,n3;

System.out.print("Enter n1 : ");

n1=Integer.parseInt(br.readLine());

System.out.print("Enter n1 : ");

n2=Integer.parseInt(br.readLine());

n3 = n1 + n2;

System.out.println("n1 = "+n1);

System.out.println("n2 = "+n2);

System.out.println("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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author YASH

*/

public class circle {

public static void main(String args[])throws IOException

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

float PI=3.14f,area, r;

System.out.print("Enter radius: ");

r=Integer.parseInt(br.readLine());

area=PI*r*r;

System.out.println(" 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 java.io.BufferedReader;

import java.io.InputStreamReader;

import static java.lang.StrictMath.PI;

/**

* @author YASH

*/

public class quare {

public static void main(String args[]) throws IOException

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

System.out.print("Enter length : ");

l=Integer.parseInt(br.readLine());

float area,l;

area=l*l;

System.out.println(" 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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author YASH

*/

public class rectangle {

public static void main(String args[])throws IOException{

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

float l,b, area;

System.out.print("Enter length : ");

l=Integer.parseInt(br.readLine());

System.out.print("Enter Height : ");

b=Integer.parseInt(br.readLine());

area=l*b;

System.out.println(" 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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author YASH

*/

public class triangle {

public static void main(String args[])throws IOException{

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

float h, b, area;

System.out.print("Enter height : ");

h=Integer.parseInt(br.readLine());

System.out.print("Enter base : ");

b=Integer.parseInt(br.readLine());

area=0.5f *h*b;

System.out.println(" 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

11.area of circle using Class mechanism:

Input:

package student;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @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 ( System.in);

BufferedReader br = new BufferedReader ( ir);

System.out.print(" Enter radius :");

r = Integer.parseInt(br.readLine());

public void display_data()

System.out.println(" radius = "+ r);

System.out.println(" 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 ( System.in);

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 java.io.*;

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 = pobj.acno;

name = pobj.name;

balance = pobj.balance;

public void input_data() throws IOException

InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ir);

System.out.print("Enter account no : ");

acno=Integer.parseInt(br.readLine());

System.out.print("Enter name : ");

name = br.readLine();

System.out.print("Enter balance : ");

balance =Integer.parseInt(br.readLine());

return;

public void display_data()

2023 15
JAVA PROGRAMMING- I

System.out.println( "acno : "+ acno );

System.out.println( "name : "+ name );

System.out.println( "balance : "+ balance );

public void deposite() throws IOException

InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ir);

int deposite_amt;

System.out.print("Enter deposite amount : ");

deposite_amt =Integer.parseInt(br.readLine());

balance = balance + deposite_amt;

return;

public void withdraw() throws IOException

InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ir);

int withdraw_amt;

System.out.print("Enter withdraw amount : ");

withdraw_amt =Integer.parseInt(br.readLine());

if (( balance - withdraw_amt) > 500 )

balance = balance - withdraw_amt;

else

System.out.println(" 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(System.in);

BufferedReader br=new BufferedReader(ir);

int choice;

do

System.out.println(" Main Menu ");

System.out.println("");

System.out.println("1. Insert Customer Information ");

System.out.println("2. Display Information ");

System.out.println("3. Deposite Amount ");

System.out.println("4. Withdraw Amount ");

System.out.println("5. Exit");

System.out.print("Enter your choice : ");

choice =Integer.parseInt(br.readLine());

switch (choice)

2023 17
JAVA PROGRAMMING- I

case 1:

bob.input_data();

break;

case 2:

bob.display_data();

break;

case 3:

bob.deposite();

break;

case 4:

bob.withdraw();

break;

case 5:

break;

default :

System.out.println("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 java.io.*;

// 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 = pobj.rno;

name = pobj.name;

s1 = pobj.s1;

s2 = pobj.s2;

s3 = pobj.s3;

public void inputdata()throws IOException

InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ir);

System.out.print("Enter Roll no= ");

rno=Integer.parseInt(br.readLine());

System.out.print("Enter Name= ");

name=br.readLine();

System.out.print("Enter s1= ");

s1=Integer.parseInt(br.readLine());

System.out.print("Enter s2= ");

s2=Integer.parseInt(br.readLine());

System.out.print("Enter s3= ");

s3=Integer.parseInt(br.readLine());

return;

2023 22
JAVA PROGRAMMING- I

public void displaydata()

System.out.println("rno= "+rno);

System.out.println(" Name : "+ name );

System.out.println("s1= "+s1);

System.out.println("s2= "+s2);

System.out.println("s3= "+s3);

System.out.println("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);

st1.inputdata();

st1.cal_result();

st1.displaydata();

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

14.Write program to insert and display array:

Input:

package myarray;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author YASH

*/

public class array1 {

public static void main(String[] args)throws IOException {

InputStreamReader ir = new InputStreamReader ( System.in);

BufferedReader br = new BufferedReader ( ir);

//declare array

int a[]= new int[10];

int i;

int n;

System.out.print( " Enter n : ");

n= Integer.parseInt(br.readLine());

System.out.println("Enter Array elements ");

for(i=0;i<n;i++){

System.out.print("ENter a["+i+"]=");

a[i]=Integer.parseInt(br.readLine());

2023 25
JAVA PROGRAMMING- I

for( i=0; i<n ; i++)

System.out.println("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

15.Write program of linear search array:

Input:

package myarray;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author YASH

*/

public class linear_search {

public static void main(String[] args) throws IOException

// TODO code application logic here

InputStreamReader ir = new InputStreamReader ( System.in);

BufferedReader br = new BufferedReader ( ir);

// decalre array

int a[] = new int [10];

int n; // total number of elements

int i,item, pos;

System.out.print( " Enter n : ");

n= Integer.parseInt(br.readLine());

System.out.println("Enter Array elements ");

for( i=0; i<n ; i++)

System.out.print("Enter a[ "+i+" ] =");

a[i] = Integer.parseInt(br.readLine());

2023 27
JAVA PROGRAMMING- I

System.out.println("Array elements are");

for( i=0; i<n ; i++)

System.out.println("a[ "+i+" ] ="+a[i]);

System.out.print( " Enter item to search : ");

item= Integer.parseInt(br.readLine());

pos = -1;

for( i=0; i<n ; i++)

if (a[i] == item )

pos = i;

System.out.println( item +" Found at position "+ pos);

return;

if ( pos == -1 )

System.out.println( 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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author YASH

*/

public class bubble_sort {

public static void main(String[] args) throws IOException

InputStreamReader ir = new InputStreamReader ( System.in);

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;

System.out.print( " Enter n : ");

n= Integer.parseInt(br.readLine());

System.out.println("Enter Array elements ");

for( i=0; i<n ; i++){

System.out.print("Enter a[ "+i+" ] =");

a[i] = Integer.parseInt(br.readLine());

System.out.println("Array elements are");

for( i=0; i<n ; i++)

System.out.println("a[ "+i+" ] ="+a[i]);

2023 30
JAVA PROGRAMMING- I

// sort array using bubble sort method

int k,ptr,temp;

System.out.println("\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;

System.out.println("Array elements are");

for( i=0; i<n ; i++)

System.out.println("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

17.write 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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @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(System.in);

BufferedReader br=new BufferedReader(ir);

System.out.print(" Enter Employee no : ");

empno = Integer.parseInt(br.readLine());

System.out.print(" Enter Employee name : ");

name = br.readLine();

System.out.print(" Enter Employee Designation : ");

designation = br.readLine();

return;

// display_data()

public void display_data()

System.out.println("Employee no : "+empno);

System.out.println("Employee name : "+name);

System.out.println("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(System.in);

BufferedReader br=new BufferedReader(ir);

super.input_data();

System.out.print(" Enter Employee Basic Pay : ");

basic_pay = Integer.parseInt(br.readLine());

return;

// display_data()

public void display_data()

super.display_data();

System.out.println(" Employee Basic Pay : "+basic_pay);

return;

2023 35
JAVA PROGRAMMING- I

public static void main(String args[]) throws IOException

InputStreamReader ir=new InputStreamReader(System.in);

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

18.write 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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @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(System.in);

BufferedReader br=new BufferedReader(ir);

System.out.print("Enter Roll No :");

rno = Integer.parseInt(br.readLine());

System.out.print("Enter Name :");

name = br.readLine();

return;

public void display_data()

System.out.println("Roll No : "+rno);

System.out.println(" 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(System.in);

BufferedReader br=new BufferedReader(ir);

super.input_data();

System.out.print("Enter sub1 :");

sub1 = Integer.parseInt(br.readLine());

System.out.print("Enter sub2 :");

2023 39
JAVA PROGRAMMING- I

sub2 = Integer.parseInt(br.readLine());

System.out.print("Enter sub3 :");

sub3 = Integer.parseInt(br.readLine());

return;

public void display_data()

super.display_data();

System.out.println(" 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(System.in);

BufferedReader br=new BufferedReader(ir);

super.input_data();

return;

public void display_data()

super.display_data();

System.out.println(" Percentage = "+ percent);

System.out.println("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(System.in);

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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @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(System.in);

BufferedReader br=new BufferedReader(ir);

System.out.print(" Enter Employee no : ");

empno = Integer.parseInt(br.readLine());

System.out.print(" Enter Employee name : ");

name = br.readLine();

return;

// display_data()

public void display_data()

System.out.println("Employee no : "+empno);

System.out.println("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(System.in);

BufferedReader br=new BufferedReader(ir);

super.input_data(); // base class input_data()

System.out.print(" Enter Employee extra holiday : ");

2023 45
JAVA PROGRAMMING- I

extra_holiday = Integer.parseInt(br.readLine());

System.out.print(" Enter Employee designation : ");

design = br.readLine();

return;

// display_data()

public void display_data()

super.display_data(); // base class

System.out.println(" Employee extra holiday : "+ extra_holiday);

System.out.println(" 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(System.in);

BufferedReader br=new BufferedReader(ir);

super.input_data(); // base class input_data()

System.out.print(" Enter Employee salary : ");

salary = Integer.parseInt(br.readLine());

System.out.print(" Enter Employee department : ");

department = br.readLine();

return;

// display_data()

public void display_data()

super.display_data(); // base class

System.out.println(" Employee salary : "+ salary);

System.out.println(" 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(System.in);

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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author YASH

*/

abstract class one_d

int dim1;

public one_d()

dim1=10;

public void input_data()throws IOException

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

System.out.print("Enter dim1 : ");

dim1=Integer.parseInt(br.readLine());

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() {

System.out.println("Radius = "+dim1);

System.out.println("area = "+area);

public void cal_area() {

area = 3.14f * dim1 * dim1;

class mysquare extends one_d

float area;

public void display_data() {

System.out.println("Length = "+dim1);

System.out.println("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();

System.out.println("Area Of circle:");

c.input_data();

c.cal_area();

c.display_data();

System.out.println("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() {

System.out.println("The Dog says: wee wee");

public void sleep() {

System.out.println("woof!Yaffle!yawp!");

public static void main(String[] args) {

Dog myD = new Dog(); // Create a dog object

myD.animalSound();

myD.sleep();

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 student.Mycircle.*;

/**

* @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()

System.out.println(" radious = "+ r);

System.out.println(" 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

23.Make use of data binding :

Input:

package data_binding; /**

* @author YASH

*/

class Animal

void eat()

System.out.println("Animal is eating");

return;

// derived class

public class Dog extends Animal

// overriding eat() method of base class

void eat()

System.out.println(" Dog is eating ");

return;

public static void main(String args[])

Animal d1 = new Dog();

d1.eat();

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 java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

class File_notFound_Demo {

public static void main(String args[]) {

try {

// Following file does not exist

File file = new File("E://file.txt");

FileReader fr = new FileReader(file);

} catch (FileNotFoundException e) {

System.out.println("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 java.io.*;

public class TryCatchExample{

public static void main(String args[]){

try{

System.out.println("Inside try block");

int a[] = new int[2];

System.out.println("A[3]:"+a[3]);

catch(ArrayIndexOutOfBoundsException e){

System.out.println("Exception thrown :"+e);

finally{

System.out.println("finally block");

System.out.println("out of the block");

Output:

run:

Inside try block

Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3

finally block

out of the block

BUILD SUCCESSFUL (total time: 0 seconds)

2023 59
JAVA PROGRAMMING- I

26.Program 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

System.out.println("Double value "+d);

System.out.println("Long value "+l);

System.out.println("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(){

System.out.println("Example of Final.");

final class tulip extends dog{

final String animal;

tulip(String str){

animal = str;

void getex(){

System.out.println("This is "+animal+".");

class FinalExample{

public static void main(String args[]){

tulip t = new tulip("A Dog");

t.display();

t.getex();

}}

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;

System.gc();

System.out.println("Hello World");

protected void finalize(){

System.out.println("finalize method called");

Output:

run:

Hello World

finalize method called

BUILD SUCCESSFUL (total time: 0 seconds)

2023 62

You might also like