0% found this document useful (0 votes)
4 views

Java Project File

The document contains multiple Java programs demonstrating various concepts such as method overloading, inheritance (hybrid, single, multi-level, and hierarchical), encapsulation, abstraction, arrays (single and multi-dimensional), and exception handling. Each program includes a brief description of its purpose, followed by the code and corresponding output. The programs illustrate fundamental programming principles and Java syntax.

Uploaded by

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

Java Project File

The document contains multiple Java programs demonstrating various concepts such as method overloading, inheritance (hybrid, single, multi-level, and hierarchical), encapsulation, abstraction, arrays (single and multi-dimensional), and exception handling. Each program includes a brief description of its purpose, followed by the code and corresponding output. The programs illustrate fundamental programming principles and Java syntax.

Uploaded by

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

PROGRAM 2

#WRITE A PROGRAM OF METHOD OVERLOADING

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

A has single inheritance with C and shares Hierarchy with B

B has single inheritance with C and shares hierarchy with A

D has single inheritance with A and multi-level inheritance with C

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 string:itee bhardwaj

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

inside class B values =

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

ninja model: 1000r

bike type: sports

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;

void setStudent_name(float n_val)


{
return;
}
}
class Encap_Test
{
public static void main(String [] args)
{
Encapsue_Function_Demo obj_ref=new Encapsue_Function_Demo();
obj_ref.setStdnt_roll_no(202100);
System.out.println("roll_no:"+obj_ref.getStdnt_roll_no());
}
}

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);
}

public void Square() {


Scanner kb1 = new Scanner(System.in);
System.out.println("enter the length of sqaure");
double s = kb1.nextInt();
double areaOfSquare = s * s;
System.out.println("area of the square:" + areaOfSquare);
}

public void Rectangle() {


Scanner kb2 = new Scanner(System.in);
System.out.println("enter the length of rectangle");
double l = kb2.nextInt();
System.out.println("enter breadth of rectangle");
double b = kb2.nextInt();
double areaOfRectangle = l * b;
System.out.println("the area of rectangle is" + areaOfRectangle);
}

public void Triangle() {


Scanner kb3 = new Scanner(System.in);
System.out.println("enter the base of triangle");
double base = kb3.nextInt();
System.out.println("enter the height of triangle");
double h = kb3.nextInt();
double areaOfTriangle = base * h * 0.5;
System.out.println("the area of triangle is:" + areaOfTriangle);
}

public static void main(String[] args) {


ShapeArea geometry = new ShapeArea();
geometry.Circle();
geometry.Square();
geometry.Rectangle();
geometry.Triangle();
}
}

OUTPUT

enter the radius of the circle


23
area of the circle is:1661.06
enter the length of sqaure
31
area of the square:961.0
enter the length of rectangle
24
enter breadth of rectangle
12
the area of rectangle is288.0
enter the base of triangle
43
enter the height of triangle
54
the area of triangle is:1161.0
PROGRAM 10
#WRITE A PROGRAM TO SHOW ABSTRACTION
METHOD
class Employee
{
private String name;
private String address;
private int number;
public Employee(String name, String
address, int number)
{
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public double computePay()
{
System.out.println("Inside Employee computePay");
return 0;
}
public void mailCheck( )
{
System.out.println("Mailing a check to"+ this. name+""+this.address);
}
public String toString( )
{
return name + ""+address+""+number;
}
public String getName( )
{
return name;
}
public String getAddress( )
{
return address;
}
public void setAddress(String newAddress)
{
address = newAddress;
}
public int getNumber( )
{
return number;
}
}

class Salary extends Employee


{
private double Salary; // Annual salary
public Salary(String name, String address,
int number, double salry)

{
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

Call mailCheck using Salary reference--

Within mailCheck of Salary class

Mailing Check toMohd Mohtashim with salry0.0

Call mailCheck using Employee reference--

Within mailCheck of Salary class

Mailing Check toJohn Adams with salry0.0


PROGRAM 11
#WRITE A PROGRAM TO SHOW SINGLE DIMENSION
ARRAY
class Test
{
static int[] get()
{
return new int[]{10,30,50,90,60};
}
public static void main(String [] args)
{
int arr[]=get();
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}

OUTPUT
10

30

50

90

60
PROGRAM 12

#WRITE A PROGRAM TO SHOW MULTI DIMENSIONAL


ARRAY

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

#WRITE A PROGRAM TO SHOW EXCEPTION HANDLING

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

You might also like