Java Project File
Java Project File
class Adder
{
static int add(int a, int b)
{
return a+b;
}
static int add(int a, int b, int c)
{
return a+b+c;
}
}
class Overloading
{
public static void main(String [] args)
{
System.out.println(Adder.add(12,13));
System.out.println(Adder.add(7,9,4));
}
}
OUTPUT
25
20
PROGRAM 7
#WRITE A PROGRAM TO SHOW HYBRID INHERITANCE
class C
{
public void Print()
{
System.out.println("C is the parent class to all A,B,D");
}
}
class A extends C
{
public void Print()
{
System.out.println("A has single inheritance with C and shares Hierarchy
with B");
}
}
class B extends C
{
public void Print()
{
System.out.println(" B has single inheritance with C and shares hierarchy
with A");
}
}
class D extends A
{
public void Print()
{
System.out.println(" D has single inheritance with A and multi-level
inheritance with C");
}
public static void main(String [] args)
{
A w=new A();
B x=new B();
C y=new C();
D z=new D();
y.Print();
w.Print();
x.Print();
z.Print();
}
}
class E extends A
{
public void Print()
{
System.out.println("D has single inheritance with A and multi-level
inheritance with C");
}
public static void main(String [] args)
{
A w=new A();
B x=new B();
C y=new C();
D z=new D();
y.Print();
w.Print();
x.Print();
z.Print();
}
}
OUTPUT
C is the parent class to all A,B,D
PROGRAM 1
#WRITE A PROGRAM TO SHOW INPUT AND OUTPUT
FUNCTION
import java.io.*;
class Status
{
public static void main(String [] args)throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter string");
String str=br.readLine();
System.out.println("enter number");
int i=Integer.parseInt(br.readLine());
System.out.println("entered string:"+str);
System.out.println("entered integer:"+i);
}
}
OUTPUT
enter string
itee bhardwaj
enter number
09
entered integer:9
PROGRAM 3
#WRITE A PROGRAM TO SHOW CONSTRUCTOR
class Employee
{
int id;
String name;
Employee(int i,String n)
{
id=i;
name=n;
}
void display()
{
System.out.println(id+""+name);
}
public static void main(String [] args)
{
Employee e1=new Employee(1102,"sonu verma");
Employee e2=new Employee(1123,"riyaan");
e1.display();
e2.display();
}
}
OUTPUT
1102sonu verma
1123riyaan
PROGRAM 4
#WRITE A PROGRAM TO SHOW SINGLE INHERITANCE
class A
{
int a,b;
void display()
{
System.out.println("\ninside class A values =\n"+a+"\n"+b);
}
}
class B extends A
{
int c;
void show()
{
System.out.println("inside class B values =\n"+a+"\n"+b+"\n"+c);
}
}
class SingleInheritance
{
public static void main(String [] args)
{
B obj=new B();
obj.a=10;
obj.b=20;
obj.c=30;
obj.display();
obj.show();
}
}
OUTPUT
inside class A values =
10
20
10
20
30
PROGRAM 5
#WRITE A PROGRAM TO SHOW MULTILEVEL
INHERITANCE
class Bike
{
public void Bike()
{
System.out.println("Segment:1000cc");
}
public void BikeType()
{
System.out.println("bike type: sports");
}
}
class Ninja extends Bike
{
public Ninja()
{
System.out.println("make ninja");
}
public void brand()
{
System.out.println("manufacture:kawaski");
}
public void speed()
{
System.out.println("max speed:290kmph");
}
}
class NinjaR extends Ninja
{
public NinjaR()
{
System.out.println("ninja model: 1000r");
}
public void speed()
{
System.out.println("max speed:280kmph");
}
public static void main(String [] args)
{
NinjaR obj=new NinjaR();
obj.BikeType();
obj.brand();
obj.speed();
}
}
OUTPUT
make ninja
manufacture:kawaski
max speed:280kmph
PROGRAM 6
#WRITE A PROGRAM TO SHOW HIERARCHICAL INHERITANCE
class Employee
{
double leaves=25.00;
}
class PEmployee extends Employee
{
float totalHoursPerDay=(float)8.00;
}
class TEmployee extends Employee
{
float totalHoursPerDay=(float)10.50;
}
class EmployeeSalary
{
public static void main(String [] args)
{
PEmployee permanent=new PEmployee();
TEmployee temporary=new TEmployee();
System.out.println("permanent Employee Total Number of leaves are:\
n"+permanent.leaves);
System.out.println("number of working hours for permanent employees
are:\n"+permanent.totalHoursPerDay);
System.out.println("temporary employees total number of leaves are:\
n"+temporary.leaves);
System.out.println("number of working hours for temporary employees
are:\n"+temporary.totalHoursPerDay);
}
}
OUTPUT
permanent Employee Total Number of leaves are:
25.0
number of working hours for permanent employees are:
8.0
temporary employees total number of leaves are:
25.0
number of working hours for temporary employees are:
10.5
PROGRAM 8
#WRITE A PROGRAM TO SHOW ENCAPSULATION
class Encapsue_Function_Demo
{
int roll_no;
int getStdnt_roll_no()
{
return roll_no;
}
void setStdnt_roll_no(int n_val)
{
roll_no=n_val;
}
}
class StudentInfo
{
public String student_name;
OUTPUT
roll_no:202100
PROGRAM 9
#WRITE A PROGRAM TO SHOW MULTIPLE
INHERITANCE(INTERFACE)
import java.util.Scanner;
interface Area
{
public void Square();
public void Circle();
public void Rectangle();
public void Triangle();
}
class ShapeArea implements Area {
public void Circle() {
Scanner kb = new Scanner(System.in);
System.out.println("enter the radius of the circle");
double r = kb.nextInt();
double areaOfCircle = 3.14 * r * r;
System.out.println("area of the circle is:" + areaOfCircle);
}
OUTPUT
{
super(name, address, number);
setSalary(Salary);
}
public void mailCheck( )
{
System.out.println("Within mailCheck of Salary class");
System.out.println("Mailing Check to" + getName( ) + " with salry" +
Salary);
}
public double getSalary()
{
return Salary;
}
public void setSalary(double newSalary)
{
if(newSalary>=0)
{
Salary = newSalary;
}
}
public double computePay( )
{
System.out.println("Computing salary pay for" + getName());
return 0;
}
}
class AbstractSample
{
public static void main(String [ ] args)
{
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3,3600.00);
Employee e = new Salary ("John Adams", "Boston, MA", 224,4500.00);
System.out.println("Call mailCheck using Salary reference--");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck( );
}
}
OUTPUT
Constructing an Employee
Constructing an Employee
OUTPUT
10
30
50
90
60
PROGRAM 12
class TestArray
{
public static void main(String [] args)
{
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+"");
}
System.out.println();
}
}
}
OUTPUT
123
245
445
PROGRAM 13
import java.lang.*;
class TryCatchExample1
{
public static void main(String [] args)
{
int data=50/0;
System.out.println("rest of the code");
}
}
OUTPUT
Exception in thread "main" java.lang.ArithmeticException: / by zero