Java Programming
Java Programming
Java Programming- i
JAVA PROGRAMMING- I
Input:
package student;
/**
* @author YASH
*/
Output:
run:
Hello worldl
2023 1
JAVA PROGRAMMING- I
Input:
package student;
/**
* @author YASH
*/
float PI=3.14f,r=10.0f,area;
area=PI*r*r;
Output:
run:
2023 2
JAVA PROGRAMMING- I
Input:
package student;
/**
* @author YASH
*/
int l=10;
float area;
area=l*l;
Output:
run:
2023 3
JAVA PROGRAMMING- I
Input:
package student;
/**
* @author YASH
*/
int l=10;
int b=25;
float area;
area=l*b;
Output:
run:
2023 4
JAVA PROGRAMMING- I
Input:
package student;
/**
* @author YASH
*/
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:
2023 5
JAVA PROGRAMMING- I
Input:
package student;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
*/
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
2023 6
JAVA PROGRAMMING- I
input:
package student;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
float PI=3.14f,area, r;
r=Integer.parseInt(br.readLine());
area=PI*r*r;
Output:
run:
Enter radius: 12
2023 7
JAVA PROGRAMMING- I
Input:
package student;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* @author YASH
*/
l=Integer.parseInt(br.readLine());
float area,l;
area=l*l;
Output:
run:
Enter length : 14
2023 8
JAVA PROGRAMMING- I
Input:
package student;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
l=Integer.parseInt(br.readLine());
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
2023 9
JAVA PROGRAMMING- I
Input:
package student;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
float h, b, area;
h=Integer.parseInt(br.readLine());
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
2023 10
JAVA PROGRAMMING- I
Input:
package student;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
int r;
float area;
// default constructor
public Mycircle()
r = 10;
// parameterised constructor
r = pdim1;
// copy constructor
2023 11
JAVA PROGRAMMING- I
r = mc.r;
r = Integer.parseInt(br.readLine());
return;
area = 3.14f * r * r;
return;
2023 12
JAVA PROGRAMMING- I
mc.input_data();
mc.cal_area();
mc.display_data();
Output :
run:
radius = 17
area = 907.46
2023 13
JAVA PROGRAMMING- I
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;
balance = 1000;
// parametrised
acno = pacno;
2023 14
JAVA PROGRAMMING- I
name = pname;
balance = pbalance;
// copy
acno = pobj.acno;
name = pobj.name;
balance = pobj.balance;
acno=Integer.parseInt(br.readLine());
name = br.readLine();
balance =Integer.parseInt(br.readLine());
return;
2023 15
JAVA PROGRAMMING- I
int deposite_amt;
deposite_amt =Integer.parseInt(br.readLine());
return;
int withdraw_amt;
withdraw_amt =Integer.parseInt(br.readLine());
else
2023 16
JAVA PROGRAMMING- I
return;
// create object
int choice;
do
System.out.println("");
System.out.println("5. Exit");
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
2023 18
JAVA PROGRAMMING- I
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
Main Menu
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
acno : 839022
balance : 157000
Main Menu
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
Main Menu
2023 19
JAVA PROGRAMMING- I
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
Main Menu
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
acno : 839022
balance : 150000
Main Menu
2. Display Information
3. Deposite Amount
4. Withdraw Amount
5. Exit
2023 20
JAVA PROGRAMMING- I
Input:
package student;
/**
* @author YASH
*/
import java.io.*;
// declare class
class student
// instance variables
public student()
rno = 101;
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;
rno = pobj.rno;
name = pobj.name;
s1 = pobj.s1;
s2 = pobj.s2;
s3 = pobj.s3;
rno=Integer.parseInt(br.readLine());
name=br.readLine();
s1=Integer.parseInt(br.readLine());
s2=Integer.parseInt(br.readLine());
s3=Integer.parseInt(br.readLine());
return;
2023 22
JAVA PROGRAMMING- I
System.out.println("rno= "+rno);
System.out.println("s1= "+s1);
System.out.println("s2= "+s2);
System.out.println("s3= "+s3);
return;
// local variables
int total;
float percent;
total = s1+s2+s3;
if ( percent >= 70 )
else
result = "Fail";
return;
2023 23
JAVA PROGRAMMING- I
//create object
// default constructor
st1.inputdata();
st1.cal_result();
st1.displaydata();
return;
Output:
run:
Enter s1= 56
Enter s2= 64
Enter s3= 50
rno= 45
s1= 56
s2= 64
s3= 50
2023 24
JAVA PROGRAMMING- I
Input:
package myarray;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
//declare array
int i;
int n;
n= Integer.parseInt(br.readLine());
for(i=0;i<n;i++){
System.out.print("ENter a["+i+"]=");
a[i]=Integer.parseInt(br.readLine());
2023 25
JAVA PROGRAMMING- I
}\
Output :
run:
Enter n : 7
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
2023 26
JAVA PROGRAMMING- I
Input:
package myarray;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
// decalre array
n= Integer.parseInt(br.readLine());
a[i] = Integer.parseInt(br.readLine());
2023 27
JAVA PROGRAMMING- I
item= Integer.parseInt(br.readLine());
pos = -1;
if (a[i] == item )
pos = i;
return;
if ( pos == -1 )
Output:
run:
Enter n : 7
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
a[ 0 ] =1
a[ 1 ] =2
a[ 2 ] =3
a[ 3 ] =5
a[ 4 ] =4
a[ 5 ] =7
a[ 6 ] =6
5 Found at position 3
2023 29
JAVA PROGRAMMING- I
Input:
package myarray;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
// decalre array
int i;
n= Integer.parseInt(br.readLine());
a[i] = Integer.parseInt(br.readLine());
2023 30
JAVA PROGRAMMING- I
int k,ptr,temp;
for(k=0;k<n-1;k++)
for(ptr=0;ptr<n-k-1;ptr++)
*/
if(a[ptr]>a[ptr+1])
temp=a[ptr];
a[ptr]=a[ptr+1];
a[ptr+1]=temp;
Output:
run:
Enter n : 7
2023 31
JAVA PROGRAMMING- I
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
a[ 0 ] =2
a[ 1 ] =4
a[ 2 ] =5
a[ 3 ] =7
a[ 4 ] =2
a[ 5 ] =3
a[ 6 ] =4
a[ 0 ] =2
a[ 1 ] =2
a[ 2 ] =3
a[ 3 ] =4
a[ 4 ] =4
a[ 5 ] =5
a[ 6 ] =7
2023 32
JAVA PROGRAMMING- I
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;
designation = "manager";
//input_data()
2023 33
JAVA PROGRAMMING- I
empno = Integer.parseInt(br.readLine());
name = br.readLine();
designation = br.readLine();
return;
// display_data()
System.out.println("Employee no : "+empno);
return;
2023 34
JAVA PROGRAMMING- I
//derived class
int basic_pay;
// constructor
public employee_salary()
super();
basic_pay = 12000;
//input_data()
super.input_data();
basic_pay = Integer.parseInt(br.readLine());
return;
// display_data()
super.display_data();
return;
2023 35
JAVA PROGRAMMING- I
gscv.input_data();
gscv.display_data();
return;
Output:
run:
Employee no : 103
2023 36
JAVA PROGRAMMING- I
18.write a program to display student info and results using multilevel inheritance;
student_test : sub1,sub2,sub3
Input:
package inheritance;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
class student_personal
// constructors
student_personal()
rno = 101;
2023 37
JAVA PROGRAMMING- I
rno = prno;
name = pname;
rno = Integer.parseInt(br.readLine());
name = br.readLine();
return;
System.out.println("Roll No : "+rno);
return;
2023 38
JAVA PROGRAMMING- I
// 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;
super.input_data();
sub1 = Integer.parseInt(br.readLine());
2023 39
JAVA PROGRAMMING- I
sub2 = Integer.parseInt(br.readLine());
sub3 = Integer.parseInt(br.readLine());
return;
super.display_data();
System.out.println(" Sub1 = "+sub1 + " sub2 = "+ sub2+ " sub3 = "+sub3);
return;
// 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);
super.input_data();
return;
super.display_data();
return;
result = "Distinction";
else
2023 41
JAVA PROGRAMMING- I
return;
ty.input_data();
ty.cal_result();
ty.display_data();
return;
Output:
run:
Roll No : 2
Name :Patel H M
Percentage = 43.0
2023 42
JAVA PROGRAMMING- I
Input:
package inheritance;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
// base class
class employee_general
// constructor
public employee_general()
empno = 101;
empno = pempno;
2023 43
JAVA PROGRAMMING- I
name = pname;
//input_data()
empno = Integer.parseInt(br.readLine());
name = br.readLine();
return;
// display_data()
System.out.println("Employee no : "+empno);
return;
2023 44
JAVA PROGRAMMING- I
//derived class
// constructor
public non_tech()
super();
extra_holiday = 5;
design = "Supervisor";
super(pempno , pname);
extra_holiday = pextra_holiday;
design = pdesign;
//input_data()
2023 45
JAVA PROGRAMMING- I
extra_holiday = Integer.parseInt(br.readLine());
design = br.readLine();
return;
// display_data()
return;
//derived class
// constructor
public tech()
super();
salary = 5000;
department = "civil";
2023 46
JAVA PROGRAMMING- I
super(pempno , pname);
salary = psalary;
department = pdepartment;
//input_data()
salary = Integer.parseInt(br.readLine());
department = br.readLine();
return;
// display_data()
2023 47
JAVA PROGRAMMING- I
return;
iti.input_data();
iti.display_data();
iit.input_data();
iit.display_data();
2023 48
JAVA PROGRAMMING- I
Output:
run:
Enter Employee no : 22
Employee no : 22
Enter Employee no : 70
Employee no : 70
2023 49
JAVA PROGRAMMING- I
Input:
package abstract1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author YASH
*/
int dim1;
public one_d()
dim1=10;
dim1=Integer.parseInt(br.readLine());
return;
2023 50
JAVA PROGRAMMING- I
float area;
System.out.println("Radius = "+dim1);
System.out.println("area = "+area);
float area;
System.out.println("Length = "+dim1);
System.out.println("area = "+area);
2023 51
JAVA PROGRAMMING- I
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
2023 53
JAVA PROGRAMMING- I
Input:
package myinterface;
/**
* @author YASH
*/
// Interface
interface Animal {
System.out.println("woof!Yaffle!yawp!");
myD.animalSound();
myD.sleep();
Output:
run:
woof!Yaffle!yawp!
2023 54
JAVA PROGRAMMING- I
Input:
package student;
import student.Mycircle.*;
/**
* @author YASH
*/
// default constructor
float area;
public my_circle()
// method overriding
return;
2023 55
JAVA PROGRAMMING- I
mc.cal_area();
mc.display_data();
Output:
run:
radious = 10
area = 314.0
2023 56
JAVA PROGRAMMING- I
Input:
* @author YASH
*/
class Animal
void eat()
System.out.println("Animal is eating");
return;
// derived class
void eat()
return;
d1.eat();
Output:
2023 57
JAVA PROGRAMMING- I
Input:
package myException;
/**
* @author YASH
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
class File_notFound_Demo {
try {
} catch (FileNotFoundException e) {
Output:
run:
2023 58
JAVA PROGRAMMING- I
Input:
import java.io.*;
try{
System.out.println("A[3]:"+a[3]);
catch(ArrayIndexOutOfBoundsException e){
finally{
System.out.println("finally block");
Output:
run:
finally block
2023 59
JAVA PROGRAMMING- I
Input:
package casting;
/**
* @author YASH
*/
double d = 100.04;
Output:
run:
2023 60
JAVA PROGRAMMING- I
Input:
package casting;
/**
* @author YASH
*/
class dog{
System.out.println("Example of Final.");
tulip(String str){
animal = str;
void getex(){
System.out.println("This is "+animal+".");
class FinalExample{
t.display();
t.getex();
}}
Output:
run:
Example of Final.
This is A Dog.
2023 61
JAVA PROGRAMMING- I
Input:
package casting;
/**
* @author YASH
*/
obj = null;
System.gc();
System.out.println("Hello World");
Output:
run:
Hello World
2023 62