[Go to site: main page, start]

0% found this document useful (0 votes)
6 views8 pages

Java Programs

The document contains multiple Java programs demonstrating various concepts such as multiple inheritance using interfaces, method overloading, method overriding, abstract classes, vector operations, exception handling, user-defined exceptions, and multithreading. Each program is well-structured with appropriate classes and methods, showcasing the implementation of the respective concepts. Sample outputs are provided for each program to illustrate their functionality.

Uploaded by

2007gayatri007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Java Programs

The document contains multiple Java programs demonstrating various concepts such as multiple inheritance using interfaces, method overloading, method overriding, abstract classes, vector operations, exception handling, user-defined exceptions, and multithreading. Each program is well-structured with appropriate classes and methods, showcasing the implementation of the respective concepts. Sample outputs are provided for each program to illustrate their functionality.

Uploaded by

2007gayatri007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Write a Java Program to implement the concept of Multiple Inheritance using Interfaces

[Link];
// Interface 1
interfaceAddition{
voidadd(inta,int b);
}
//Interface2
interface Multiplication
{ voidmultiply(inta,intb);
}
//Classimplementingbothinterfaces
classCalculateimplementsAddition,Multiplication{
public void add(int a, int b)
{ [Link]("Sum="+(a+b));
}
public void multiply(int a, int b)
{ [Link]("Product="+(a*b));
}
}

// Main class
public class Main{
public static void main(String[] args)

{ Scannersc=newScanner([Link]);

[Link]("Enterfirstnumber:");
int x = [Link]();
[Link]("Entersecondnumber:");
int y = [Link]();
Calculateobj=newCalculate();
[Link](x,y);
[Link](x,y);
}
}
Enter first number: 6
Entersecondnumber:3
Sum = 9
Product=18
[Link] a Java program to implement method overloading.

[Link]; class Area {

//Areaof Square
voidcalculateArea(intside){
[Link]("AreaofSquare="+(side*side));
}
//Areaof Rectangle
void calculateArea(int length, int breadth)
{ [Link]("AreaofRectangle="+(length*breadth));
}

//Areaof Circle
void calculateArea(double radius)
{ doublearea=3.14*radius*radius;
[Link]("AreaofCircle="+area);
}
}
publicclassMain{

publicstaticvoidmain(String[]args){
Scannersc=newScanner([Link]); Area
obj = new Area();
[Link]("Entersideofsquare:"); int
side = [Link](); [Link](side);
[Link]("Enterlengthandbreadthofrectangle:"); int
length = [Link]();
int breadth = [Link]();
[Link](length,breadth);
[Link]("Enterradiusofcircle:");
double radius = [Link]();
[Link](radius);
}
}
Entersideofsquare:4 Area
of Square = 16
Enterlengthandbreadthofrectangle:56
Area of Rectangle = 30
Enterradiusofcircle:5
Area of Circle = 78.5
Write a Java program to implement method overriding.

class Animal
{ voidsound(){
[Link]("Animalmakesasound");
}
}

classDogextendsAnimal{ voi
d sound() {
[Link]("Dogbarks");
}
}

publicclassOverrideDemo{
publicstaticvoidmain(String[]args){ Dog
d = new Dog();
[Link]();
}
}
Write a Java program to implement Abstract class with an abstract method

[Link]; abstractclassEmployee{

abstractvoidcalculateSalary();
}

//Subclass
classManagerextendsEmployee{

voidcalculateSalary(){
Scanner sc = new Scanner([Link]);
[Link]("Enterbasicsalary:");
double basic = [Link]();

doublesalary=basic+(0.2*basic);//20%allowance
[Link]("Total Salary = " + salary);
}
}

// Main class
publicclassMain{
publicstaticvoidmain(String[]args){

Employeee=newManager();//referenceofabstractclass
[Link]();
}
}
Enterbasicsalary:25000 Total
Salary = 30000.0
Write a Java program to implement Vector Operations

[Link];
publicclassMain{
public static void main(String[] args)

{ Vector<Integer>v=newVector<>();

//Addingelements
[Link](10);
[Link](20);
[Link](30);
[Link](40);

[Link]("Vectorelements:"+v);

//Accesselement
[Link]("Elementatindex1:"+[Link](1));

//Insertelementatspecificposition
[Link](25,2);
[Link]("Afterinserting25atindex2:"+v);
//Removeanelement
[Link](3);
[Link]("Afterremovingelementatindex3:"+ v);

}
}
Vectorelements:[10,20,30,40]
Elementatindex1: 20
Afterinserting25atindex2:[10,20,25,30,40]
Afterremovingelementatindex3:[10,20,25,40]
Write a Java program to implement a Arithmetic and Array Index Out of Bound Exception

[Link]; public class Main {

public static void main(String[] args)

{ Scannersc=newScanner([Link]);

//ArithmeticException
try {
[Link]("Entervaluefora:");
int a = [Link]();

[Link]("Entervalueforb:");
int b = [Link]();

int c = a / b;
[Link]("Result="+c);
}
catch(ArithmeticExceptione){
[Link]("ArithmeticException:Divisionbyzeronotallowed");
}

//ArrayIndexOutOfBoundsException try {
intarr[]={10,20,30};

[Link]("Enterarrayindex:");
int index = [Link]();

[Link]("Element="+arr[index]);
}
catch (ArrayIndexOutOfBoundsException e)
{ [Link]("ArrayIndexOutOfBounds Exception");
}
}
}
Enter value for a: 5
Entervaluefor b:66
Result = 0
Enterarrayindex:6
ArrayIndexOutOfBoundsException
Write a Java Program to create a user define Exception called Pay Out of Bound and throw
the Exception
import [Link].*;

class PayOutOfBoundException extends Exception


{
PayOutOfBoundException(String s)
{
super(s);
}
}

class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int pay;

[Link]("Enter Pay:");
pay = [Link]();

try
{
if(pay > 50000)
throw new PayOutOfBoundException("Pay Out of Bound");
else
[Link]("Pay is Valid");
}
catch(PayOutOfBoundException e)
{
[Link](e);
}
}
}
Write a Java Program to implement the concept of Multithreading with the use of any
three multiplication tables and assign three different priorities to them

class Table extends Thread


{
int num;

Table(int n)
{
num = n;
}

public void run()


{
for(int i = 1; i <= 10; i++)
{
[Link](num + " x " + i + " = " + (num * i));
}
[Link]();
}
}

class MultiThreadTable
{
public static void main(String args[])
{
Table t1 = new Table(2);
Table t2 = new Table(3);
Table t3 = new Table(4);

[Link](Thread.MIN_PRIORITY);
[Link](Thread.NORM_PRIORITY);
[Link](Thread.MAX_PRIORITY);

[Link]();
[Link]();
[Link]();
}
}

You might also like