[go: up one dir, main page]

0% found this document useful (0 votes)
78 views7 pages

5-8 PRACTICALS of Java

The document describes 9 experiments related to Java programming concepts: 1. Defines classes for vehicles like cars, two-wheelers, etc. with inheritance. 2. Handles exceptions using try-catch blocks. 3. Implements nested try statements. 4. Shows partial implementation of an interface. 5. Creates a thread that implements the Runnable interface.

Uploaded by

silky
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)
78 views7 pages

5-8 PRACTICALS of Java

The document describes 9 experiments related to Java programming concepts: 1. Defines classes for vehicles like cars, two-wheelers, etc. with inheritance. 2. Handles exceptions using try-catch blocks. 3. Implements nested try statements. 4. Shows partial implementation of an interface. 5. Creates a thread that implements the Runnable interface.

Uploaded by

silky
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
You are on page 1/ 7

EXPERIMENT-5

Objective

Consider we have a Class of Cars under which Santro Xing, Alto and Wagon R represents
individual Objects. In this context each Car Object will have its own, Model, Year of
Manufacture, Colour, Top Speed, etc. which form Properties of the Car class and the associated
actions i.e., object functions like Create(), Sold(), display() form the Methods of Car Class.

Source code:
class Vehicle
{
String regno;
int model;
Vehicle(String r, int m)
{
regno=r;
model=m;
}
void display()
{
System.out.println("Registration no: "+regno);
System.out.println("Model no: "+model);
}
}

class Twowheeler extends Vehicle


{
int noofwheel;
Twowheeler(String r,int m,int
n)
{
super(r,m);
noofwheel=n;
}
void display()
{
System.out.println("Two wheeler tvs");
super.display();
System.out.println("No. of wheel : " +noofwheel);
}
}

class Threewheeler extends Vehicle


{
int noofleaf;
Threewheeler(String r,int m,int n)
{
super(r,m);
noofleaf=n;
}
void display()
{
System.out.println("Three wheeler auto");
super.display();
System.out.println("No. of leaf:" +noofleaf);
}
}

class Fourwheeler extends Vehicle


{
int noofleaf;
Fourwheeler(String r,int m,int n)
{
super(r,m);
noofleaf=n;
}
void display()
{
System.out.println("Four wheeler car");
super.display();
System.out.println("No. of leaf:" +noofleaf);
}
}

class VehicleDemo
{
public static void main(String arg[])
{
Twowheeler t1;
Threewheeler th1;
Fourwheeler f1;
t1=new Twowheeler("TN74 12345", 1,2);
th1=new Threewheeler("TN74 54321", 4,3);
f1=new Fourwheeler("TN34 45677",5,4);
t1.display();
th1.display();
f1.display();
}
}
}

Output:

Two wheeler tvs


Registration no: TN74 12345
Model no: 1
No. of wheel : 2
Three wheeler
auto
Registration no: TN74
54321 Model no: 4
No. of leaf:3
Four wheeler
car
Registration no: TN34
45677 Model no: 5
No. of leaf:
EXPERIMENT-6

Objective

WAP to handle the exception using try and multiple catch block.

Source Code

MultipleCatchBlock1.java

public class MultipleCatchBlock1 {

public static void main(String[] args) {

try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}

Output:

Arithmetic Exception
occurs rest of the code
EXPERIMENT-7

Objective

WAP that implement the Nested try statements.

Source Code
class NestTry
{
public static void main(String args[])
{
try
{
int a =
args.length; int b
= 42 / a;
System.out.println("a = " +
a); try
{ // nested try block
if(a==1) a = a/(a-a); // division by zero
if(a==2)
{
int c[] = { 1 };
c[42] = 99;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index out-of-bounds: " + e);
}
}
catch(ArithmeticException e)
{
System.out.println("Divide by 0: " + e);
}
}
}

Output:

Array index out-of-bounds


EXPERIMENT-8

Objective

WAP that show the partial implementation of interface.

interface MyInterface
{
/* compiler will treat them as:
* public abstract void method1();
* public abstract void method2();
*/
public void method1();
public void method2();
}
class Demo implements MyInterface
{
/* This class must have to implement both the abstract methods
* else you will get compilation error
*/
public void method1()
{
System.out.println("implementation of method1");
}
public void method2()
{
System.out.println("implementation of method2");
}
public static void main(String arg[])
{
MyInterface obj = new
Demo(); obj.method1();
}
}

Output:

implementation of method1
EXPERIMENT-9

Objective

WAP to create a thread that implement the Runnable interface.

Source Code

public class ExampleClass implements Runnable {

@Override
public void run() {
System.out.println("Thread has ended");
}

public static void main(String[] args) {


ExampleClass ex = new ExampleClass();
Thread t1= new Thread(ex);
t1.start();
System.out.println("Hi");
}
}

Output:

Hi

Thread has ended

You might also like