1 Java Lab Programs - 4th Sem 27pages
1 Java Lab Programs - 4th Sem 27pages
II B.SC – IV SEMESTER
1
INDEX
2
8 9,10
Multiple Inheritance
12 To create a package creation for Book details giving Book name, Author 21-24
name, price and year of publishing.
Program - 1
Aim: Write a program to read Student Name, Reg.No, Marks[5] and calculate Total, Percentage, Result. Display all
the details of students
OUTPUT
1
Program - 2
Aim: Write a java Program is to perform the following string operations
f) Read string
g) Find out whether there is a substring or not.
h) Compare existing by another string and display status
i) Replace existing string character with another character
j) Count number of characters of a string
import java.lang.String;
public class StringMethods
{
public static void main(String[] args)
{
String str ="nalanda degree college";
String str1="nalanda";
String str2="degree";
System.out.println(str);
if(str1== str2)
System.out.println("both are equal");
else
System.out.println("both are not same");
System.out.println(str.substring(8,14));
System.out.println(str.substring(15,22));
String replace=str.replace('c','k');
System.out.println(str);
System.out.println(replace);
System.out.println(str.length());
}
}
Output:\
2
Program – 3
Aim: This Program is to implement multiplication, addition of N * N Matrices.
import java.io.*;
public class MatrixMultiplicationExample1
{
public static void main(String args[])
{
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" "); //printing matrix element
}
System.out.println();//new line
}
}
}
Output:
3
Program – 4
Aim: Java Program to demonstrate the use of the parameterized constructor.
class Student3
int id=23;
String name="nalanda";
void display()
System.out.println(id+" "+name);
s2.name="degree college";
s1.display();
s2.display();
}
}
Output:
4
Program – 5
import java.io.*;
class OverloadDemo
{
void area(float x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
}
void area(float x, float y)
{
System.out.println("the area of the rectangle is "+x*y+" sq units");
}
void area(double x)
{
double z = 3.14 * x * x;
System.out.println("the area of the circle is "+z+" sq units");
}
}
class OverloadDemo1
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.area(5);
ob.area(11,12);
ob.area(2.5);
}
}
Output
5
Program – 6
Aim: Implement inheritance between Person (Aadhar, Surname, Name, DOB, and Age) and Student (Admission
Number, College, Course, Year)classes where ReadData(), DisplayData() are overriding methods.
class Person
{
String name;
int addhar;
int dob;
int age;
Person()
{
this.name = name;
this.addhar = addhar;
this.dob = dob;
this.age = age;
}
void display()
{
int id;
String course, college;
int year;
Student()
{
this.id = id;
6
this.course = course;
this.college = college;
this.year= year;
}
void display( )
{
System.out.println(" Person name= " + name);
System.out.println(" Person addhar= " + addhar);
System.out.println(" Person dob= "+ dob);
System.out.println(" Person age= " + age);
System.out.println(" Student id = " + id);
System.out.println(" Student course = " + course);
System.out.println(" Student college = " + college);
System.out.println(" Student year = " + year);
}
}
class InherTest
{
public static void main(String []args)
{
Student s = new Student();
s.display();
}
}
Output:
7
Program – 7
import java.util;
interface Area
{
final static float pi=3.14F;
float compute(float x,float y);
}
class Rectangle implements Area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x,float y)
{
return(pi*x*y);
}
}
class Inter
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area area;
area=rect;
System.out.println("Area of rectangle=" + area.compute(10,10));
}
}
Output
8
Program – 8
class Student
{
int rollnumber;
void getnumber(int n)
{
rollnumber=n;
}
void putnumber()
{
System.out.println("Roll no:" + rollnumber);
}
}
void putmarks()
{
System.out.println("marks obtained");
System.out.println("part1 = " + part1);
System.out.println("part2 = " + part2);
}
}
interface Sports
{
float sportwt=6.0F;
void putwt();
}
void display()
{
total =part1+part2+sportwt;
putnumber();
putmarks();
putwt();
System.out.println("total score= " + total);
}
}
class MulInheritance
{
public static void main(String args[])
student1.getnumber(1234);
student1.getmarks(27.5F,33.0F);
student1.display();
}
}
Output:
10
Program –9
Aim :Java program for to display Serial Number from 1 to N by creating two Threads
{
public void run()
{
for (int i = 1; i<=5; i++)
{
if (i==1)
yield();
}
}
class B extends Thread
{
public void run()
{
for (int j=1; j<=5; j++)
{
System.out.println("\tFrom Thread B:j = " +j);
if (j==3)
stop();
}
System.out.println("Exit from B ");
}
}
class C extends Thread
{
public void run()
{
for (int k=1; k<=5; k++)
{
System.out.println("\tFrom Thread C: k = "+k);
if (k==1)
try
{
sleep (1000);
}
catch(Exception e)
{
11
}
}
System.out.println("Exit from C ");
}
}
class ThreadMethods
{
public static void main(String args[])
{
A threadA = new A();
B threadB = new B( );
C threadC =new C();
threadA.start();
threadB.start();
threadC.start();
Output:
12
Program–10
Aim:
Java program to demonstrate the following exception handlings
a. Divided by Zero
b. Array Index Out of Bound
c. File Not Found
d. Arithmetic Exception
e. User Defined Exception
import java.lang.Exception;
class Error
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x,y;
try
{
x=a/(b-c);
}
catch (ArithmeticException e)
{
System.out.println("division by zero" );
}
y=a/(b+c);
Output:
13
b.Array Index Out of Bound
import java.lang.Exception;
public class ArrayIndexOutOfBound
{ public static void main(String[] args)
{
int ar[] = { 1, 2, 3, 4, 5 }; try
System.out.print(ar[i]+" ");
catch (Exception e)
{ System.out.println("\nException caught");
}
}
Output:
14
c. File Not Found:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFileExample
{
public static void main(String[] args)
{
BufferedReader objReader = null;
try
{
String strCurrentLine;
objReader = new BufferedReader(new FileReader("D:\\DukesDiary.txt"));
while ((strCurrentLine = objReader.readLine()) != null)
{
System.out.println(strCurrentLine);
}
}
catch (IOException e)
{
e.printStackTrace();
}
Finally
{
try
{
if (objReader != null)
objReader.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
Output:
15
d. Arithmetic Exception
class eg_nested_try
{
public static void main(String args[])
{
try
{
int a=2,b=4, c=2,x=7,z;
int p[]= {2};
p[3]=33;
try
{
z=(x/(b*b)-(4*a*c));
}
}
}
Output:
16
E: User Defined Exception
import java. lang. Exception;
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}
class TestMyException
{
public static void main(String args[])
{
int x = 5, y = 1000;
try
{
float z = (float) x/ (float) y;
if(z< 0.01)
{
throw new MyException(" number is too small");
}
}
catch(MyException e)
{
System.out.println("Caught my exception") ;
System.out.println(e. getMessage()) ;
}
finally
{
System.out.println("i am always here");
}
}
}
Output:
17
Program – 11
Aim: Create an Applet to display different shapes such as Circle, Oval, Rectangle, Square and Triangle.
import java.applet.*;
import java.awt.*;
setBackground(Color.white);
//Draw a square
g.setColor(Color.black);
g.drawString("Square",75,200);
int x[]={50,150,150,50};
int y[]={50,50,150,150};
g.drawPolygon(x,y,4);
g.setColor(Color.yellow);
g.fillPolygon(x,y,4);
/Draw a pentagon
g.setColor(Color.black);
g.drawString("Pentagon",225,200);
x=new int[]{200,250,300,300,250,200};
18
y=new int[]{100,50,100,150,150,100};
g.drawPolygon(x,y,6);
g.setColor(Color.yellow);
g.fillPolygon(x,y,6);
//Draw a circle
g.setColor(Color.black);
g.drawString("Circle",400,200);
g.drawOval(350,50,125,125);
g.setColor(Color.yellow);
g.fillOval(350,50,125,125);
//Draw an oval
g.setColor(Color.black);
g.drawString("Oval",100,380);
g.drawOval(50,250,150,100);
g.setColor(Color.yellow);
g.fillOval(50,250,150,100);
//Draw a rectangle
g.setColor(Color.black);
g.drawString("Rectangle",300,380);
x=new int[]{250,450,450,250};
y=new int[]{250,250,350,350};
g.drawPolygon(x,y,4);
g.setColor(Color.yellow);
g.fillPolygon(x,y,4);
19
//Draw a triangle
g.setColor(Color.black);
g.drawString("Traingle",100,525);
x=new int[]{50,50,200};
y=new int[]{500,400,500};
g.drawPolygon(x,y,3);
g.setColor(Color.yellow);
g.fillPolygon(x,y,3);
Output:
20
Program – 12
Aim:
Write a program to create Book (ISBN,Title, Author, Price, Pages, Publisher)structure and store book details in a file
and perform the following operations
a. Add book details
b. Search a book details for a given ISBN and display book details, if available
c. Update a book details using ISBN
d. Delete book details for a given ISBN and display list of remaining Books
(or)
Java program to create a package for Book details giving Book name, Author name, price and year of publishing.
package BookDetails;
{ this.title=title;
this.author=author;
this.year=year;
this.publisher=publisher;
this.price=price;
21
}
System.out.println("Year: "+year);
this.title=title;
22
this.author=author;
this.year=year;
this.publisher=publisher;
this.price=price;
getTitle();
getAuthor();
getYear();
getPublisher();
getPrice();
23
import BookDetails.Book1;
b1.show();
b2.show();
Output
24