Sie sind auf Seite 1von 12

# 1

Ex. No.10
AIM
ALGORITHM
Step 1: Create a package name animals
Step 2: Include file name mammalint.java
Step 3: It calculates the number of legs
Step 4: Stop the process.
PROGRAM
/* File name : Animal.java */
package animals;
interface Animal
{
public void eat();
public void travel();
}
/* File name : MammalInt.java */
package animals;
public class MammalInt implements Animal
{
public void eat()
{
System.out.println("Mammal eats");
}
public void travel()
{
System.out.println("Mammal travels");
}
public int noOfLegs()
{
return 0;
}
public static void main(String args[])
{
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}

OUTPUT
Mammal eats
Mammal travels

RESULT
Thus the design of java program using Javadoc comments for Java Packages was
written and executed successfully.

2

Ex. No.11
AIM
To write a java program to implement the sum of n numbers.
ALGORITHM
Step 1: Start the program.
Step 2: Declare n=10 and find the sum of 10 numbers.
Step 3: Initialize s=0.
Step 4: For i value from 1 to 10 repeat the process by using for loop.
Step 5: Display the loop.
Step 6: Stop the program.
PROGRAM
import java.io.*;
class simple
{
public static void main(String args[])
{
int i, s=0, n=0;
for (i=1, i<=n; i++)
{
s=s+i;
System.out.println("sum of n numbers:"+s);
}
}
}
OUTPUT
Sum of n number: 1
Sum of n number: 3
Sum of n number: 6
Sum of n number: 10
Sum of n number: 15
Sum of n number: 21
Sum of n number: 28
Sum of n number: 36
Sum of n number: 45
Sum of n number: 55

RESULT
Thus the implementation of java program for the sum of n numbers was
executed successfully.

3

Ex. No.12a
EMPLOYEE DETAILS USING SINGLE INHERITANCE
AIM
To write a java program to generate an employee details using single inheritance.
ALGORITHM
Step 1: Declare the class employee
Step 2: Create the data members and member functions of the employee class.
Step 3: Create a Derived class labourer class it extends the class employee.
Step 4: Create the data members and member functions of the Derived class labourer.
Step 5: The derived class will use the datamembers of the both classes.
PROGRAM
import java.io.*;
class employee
{
private String empname;
private int empcode;
{
empname= s;
empcode=p;
}
void display()
{
System.out.println("The employee name is :" +empname);
System.out.println("The employee code is :"+empcode);
}
}
class labourer extends employee
{ private String labourtype;
private int laboursalary;
{
System.out.println("Enter the employee name:");
System.out.println("Enter the employee code :");
System.out.println("Enter the labourtype :");
System.out.println("Enter the labour salary :");
}
void display1()
{
display();
System.out.println(" The labour type is : "+ labourtype);
System.out.println(" The labour salary is :"+laboursalary); }
}
class single
{ public static void main (String s[])throws IOException
4

{
System.out.println("Enter the number of employees");
labourer l[]= new labourer[n];
for(int i=0; i<n; i++)
l[i]= new labourer();
for(int i=0; i<n; i++)
for(int i=0;i<n; i++)
l[i].display1();
}
}
OUTPUT
D:\java>java single
Enter the number of employees
2
Enter the employee name:
sd
Enter the employee code :
1
Enter the labourtype :
acc
Enter the labour salary :
23455
Enter the employee name:
siva
Enter the employee code :
2
Enter the labourtype :
sales
Enter the labour salary :
35676
The employee name is :sd
The employee code is :1
The labour type is : acc
The labour salary is :23455
The employee name is :siva
The employee code is :2
The labour type is : sales
The labour salary is :35676

RESULT
Thus the java program to generate an employee details using single inheritance
was written and executed successfully.

5

Ex. No.12b
AREA AND CIRCUMFERENCE OF THE CIRCLE USING INTERFACE
AIM
To write a java program with a class named as circle that implements an
interface named as circleinterface and define the methods named as area and circum
in the class to find the area and circumference of the circle.
ALGORITHM
Step1: Create an interface named circleinterface with the function named area and circum.
Step2: Create a class named circle, which implements the interface circleinterface to
compute the area and circumference of the circle.
Step3: Create instance for circle and calculate the area of circle.
Step5: Create instance for circle and calculate the circumference of the circle.
PROGRAM
import java.io.*;
import java.util.*;
interface circleinterface{
final static float pi=3.14F;
float area(float x);
float circum(float x);}
class circle implements circleinterface{
public float area(float x)
{ return (pi *x * x); }
public float circum(float x)
{
return (2 * pi * x);
}
}
class Interfacecircle{
public static void main(String args[]){
try{
Scanner get = new Scanner(System.in);
System.out.println("Enter theradius of the circle ");
circle r=new circle();
circleinterface a;
a=r;
System.out.println("The area of circle is:"+a.area(5));
System.out.println("The circumference of circle is:"+a.circum(5));
}catch(Exception e)
{}
}
}
OUTPUT
D:\java>java Interfacecircle
Enter the radius of the circle
The area of circle is:78.5
The circumference of circle is:31.400002

RESULT
Thus the program to calculate the area and circumference of the circle using interface
was executed successfully.

6

Ex. No.12c
STUDENT DETAILS USING MULTIPLE INHERITANCE
AIM
To write a java program to illustrate multiple inheritance for student details.
ALGORITHM
Step 1: Create a interface Exam
Step 2: Create a class Student with the following member variables. Student name,
roll number and student mark.
Step 3: Create the derived class Result and create constructor to assign the values to the
variables.
Step 4: Write a member function percent_cal to calculate the total and average mark of the
student
Step 5: Write a member function to Display to display the result.
PROGRAM
import java.lang.*;
import java.io.*;
interface Exam
{
void percent_cal();
}
class Student
{
String name;
int roll_no,mark1,mark2;
Student(String n, int r, int m1, int m2)
{
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
}
void display()
{
System.out.println ("Name of Student: "+name);
System.out.println ("Roll No. of Student: "+roll_no);
System.out.println ("Marks of Subject 1: "+mark1);
System.out.println ("Marks of Subject 2: "+mark2);
}
}
class Result extends Student implements Exam
{
Result(String n, int r, int m1, int m2)
{
super(n,r,m1,m2);
}
public void percent_cal()
{
int total=(mark1+mark2);
float percent=total*100/200;
System.out.println ("Percentage: "+percent+"%");
}
7

void display()
{
super.display();
}
}
class Multiple
{
public static void main(String args[])
{
Result R = new Result("balu",12,93,84);
R.display();
R.percent_cal();
}
}
OUTPUT
Name of Student: balu
Roll No. of Student: 12
Marks of Subject 1: 93
Marks of Subject 2: 84
Percentage: 88.0%

RESULT
Thus the java program for the student details using multiple inheritance was
executed successfully.

8

Ex. No.13a
EXCEPTION HANDLING IN JAVA
AIM
To write a java program for handling Arithmetic exception.
ALGORITHM
Step 1: Start the program.
Step 2: Declare the variables a, b, result.
Step 3: Read the values a, b, result,
Step 4: Inside the try block check the condition.
a. if(a-b!=0) then calculate the value of d and display.
b. otherwise throw the exception.
Step 5: Catch the exception and display the appropriate message.
Step 6: Stop the program.
PROGRAM
import java.util.*;
class Division {
public static void main(String[] args) {
int a, b, result;
Scanner input = new Scanner(System.in);
System.out.println("Input two integers");
a = input.nextInt();
b = input.nextInt();
try {
result = a / b;
System.out.println("Result = " + result);
}
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}
}
}
OUTPUT
D:\java>java Division
Input two integers
5
2
Result = 2

D:\java>java Division
Input two integers
5
0
Exception caught: Division by zero.

RESULT
Thus the java program for handling Arithmetic exception was executed
successfully.

9

Ex. No.13b
ARRAY INDEX BOUND EXCEPTION IN JAVA
AIM
To write a java program for handling the array index bound of exception.
ALGORITHM
Step 1: Start the program
Step 2: Create a class Exceptions
Step 3: Declare the array size
Step 4: Get the array of values
Step 5: If the input values are greater than declared value then the
ArrayIndexOutOfBoundsException will caught.
Step 6: Stop the program
PROGRAM
import java.io.*;
import java.util.Scanner;
class Exceptions {
public static void main(String[] args) {
String lang[]=new String[4];
Scanner in = new Scanner(System.in);
System.out.println("Enter the languages");
for (int c = 1; c < 5; c++)
{
lang[c] = in.nextLine();
}
System.out.println("You entered languages are");
try {
for (int c = 1; c <= 5; c++) {
System.out.println(lang[c]);
}
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
}
}OUTPUT
D:\java>java Exceptions
Enter the languages
perl
python
c
c++
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Exceptions.main(Exceptions.java:12)

RESULT
Thus the program for handling the array index bound of exception was written
and executed successfully.

10

Ex. No.14
JAVA FILE I/O HANDLING

AIM
To write a Java program to implement the concept of File I/O handling to read from a
file.
ALGORITHM
Step 1: Inside the main method of the program, declare a FileInputStream object.
Step 2: Create a new DataInputStream object.
Step 3: Assign the data from FileInputSteam to the DataInputStream object using readLine
method.
Step 4: Print the contents of the DtaInputStream object.
PROGRAM
import java.io.*;
class FileInput
{
public static void main (String args[]) {
String thisLine;
for (int i=0; i < args.length; i++) {
try {FileInputStream fin = new FileInputStream(args[i]);
try {DataInputStream myInput = new DataInputStream(fin);
try {
while ((thisLine = myInput.readLine()) != null) {
System.out.println(thisLine);
}
}
catch (Exception e) {
System.out.println("Error: " + e);
}}
catch (Exception e) {
System.out.println("Error: " + e);
}}
catch (Exception e) {
System.out.println("failed to open file " + args[i]);
System.out.println("Error: " + e);}
}
}}
OUTPUT
Contents of new.txt
Java can read several types of information from files:
binary, Java objects, text, zipped files, etc.
D:\>javac FileInput.java
D:\>java FileInput new.txt
Java can read several types of information from files:
binary, Java objects, text, zipped files, etc.
RESULT
Thus the Java program to implement the concept of File I/O handling to read
content from a file was implemented and executed successfully.

11

Ex. No.15
DESIGN OF MULTI-THREADED PROGRAMS IN JAVA
AIM
To write a java program with three threads and initiate these threads from the main
ALGORITHM
Step 1: Create the three classes named Frst ,sec,third that implements the Runnable
interface
Step 2: Create the Thread in each class .
Step 3: Create a methos run that will execute the thread messages.
Step 4: Initlize the thread from main function.
Step 5: Display the thread execution.
PROGRAM
class Frst implements Runnable
{
Frst()
{
System.out.println("Good Morning");
t.start();
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("Good Morning"+i);
try{
t.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}}}
}
class sec implements Runnable
{
sec()
{
System.out.println("hello");
t.start();
}
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("hello"+i);
try{
t.sleep(2000);
}
12

catch(Exception e)
{
System.out.println(e);
}}}
}
class third implements Runnable
third()
System.out.println("welcome");
t.start();
}public void run()
{for(int i=0;i<10;i++)
{System.out.println("welcome"+i);
try{
t.sleep(3000);
}catch(Exception e)
{System.out.println(e);
}}}}
{public static void main(String arg[])
{new Frst();
new sec();
new third();
}}
OUTPUT
Good Morning
Good Morning0
hello
hello0
welcome
welcome0
Good Morning1
hello1
Good Morning2
welcome1
Good Morning3
hello2
Good Morning4
Good Morning5
welcome2

RESULT
Thus the design of multithread program using java was written and executed
successfully.