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

1 Java Lab Programs - 4th Sem 27pages

Uploaded by

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

1 Java Lab Programs - 4th Sem 27pages

Uploaded by

Leela Rallapudi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Object oriented Programming Through java Lab Manual

II B.SC – IV SEMESTER

1
INDEX

Prg.no Nameof the programs Page


numbers
1 Display the students details
1

java Program for string operations


a) Read string
b) Find out whether there is a substring or not. 2
2
c) Compare existing by another string and display status
d) Replace existing string character with another character
e) Count number of characters of a string

This Program is to implement multiplication, addition of N * N


3 Matrices 3

Java Program to demonstrate the use of the parameterized


4
constructor. 4

5 Calculate area of the following shapes using method overloading.


a. Triangle b. Rectangle c. Circle d. Square 5

6 Implement inheritance between Person (Aadhar, Surname,


Name, DOB, and Age) and Student (Admission Number, College, 6,7
Course, Year)classes where ReadData(), DisplayData() are
overriding methods.

7 Java Program to demonstrate Implementing Interfaces


8

2
8 9,10
Multiple Inheritance

To display Serial Number from 1 to N by creating two Threads 11,12


9

Java program for exception handlings


a. Divided by Zero 13

10 b. Array Index Out of Bound


14
c. File Not Found
15
d. Arithmetic Exception
16
e. User Defined Exception
17
To display different shapes such as Circle, Oval, Rectangle, Square and
11 Triangle. 18-20

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

import java. util. Scanner; public class StudentDetails


{

public static void main(String args[])


{
String name;
int roll, math, cs, eng;
Scanner SC=new Scanner(System.in);

System.out.print("Enter Name: ");


name=SC.nextLine();
System.out.print("Enter Roll Number: ");
roll=SC.nextInt();

System.out.print("Enter marks in Maths, cs and English: ");


math=SC.nextInt();
cs =SC.nextInt();
eng=SC.nextInt();

int total = math + eng+ cs;


float per=(float)total/300*100;
System.out.println("Roll Number:" + roll +"\tName: "+name);
System.out.println("Marks (Maths, cs, English): " +math+","+cs+","+eng);
System.out.println("Total: "+total +"\tPercentage: "+per);
}
}

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

public static void main(String args[])

Student3 s1=new Student3();

Student3 s2=new Student3();


s2.id=25;

s2.name="degree college";

s1.display();

s2.display();

}
}

Output:

4
Program – 5

Aim: Calculate area of the following shapes using method overloading.


a. Triangle
b. Rectangle
c. Circle
d. Square

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()
{

System.out.println(" Person name= " + name);


System.out.println(" Person addhar= " + addhar);
System.out.println(" Person dob= " + dob);
System.out.println(" Person age= " + age);
}
}

class Student extends Person


{

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

Aim: Java program for implementing Interfaces

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

System.out.println("Area of Circle=" + area.compute(50,20));


area=cir;

}
}

Output

8
Program – 8

Aim: Java program on Multiple Inheritance

class Student
{
int rollnumber;

void getnumber(int n)
{
rollnumber=n;
}

void putnumber()
{
System.out.println("Roll no:" + rollnumber);
}
}

class Test extends Student


{
float part1,part2;

void getmarks(float m1,float m2)


{
part1=m1;
part2=m2;
}

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

class Results extends Test implements Sports


{
9
float total;

public void putwt()


{
System.out.println(" Sportwt=" +sportwt);
}

void display()
{
total =part1+part2+sportwt;

putnumber();

putmarks();

putwt();
System.out.println("total score= " + total);
}
}

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

Results student1 = new Results();

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

class A extends Thread

{
public void run()
{
for (int i = 1; i<=5; i++)
{
if (i==1)
yield();

System.out.println("\t From Thread A: i= " +i);


}
System.out.println("exit from A ");

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

System.out.println("Start thread A");

threadA.start();

System.out.println("Start thread B");

threadB.start();

System.out.println("Start thread C");

threadC.start();

System.out.println("end of the main thread");


}
}

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

a.Divided by Zero ,Arithmetic 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);

System.out.println("y =" + y);


}
}

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

for (int i = 0; i <= ar.length; i++)

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

System.out.println("the value of z is = "+z);


}
catch(ArithmeticException e)
{
System.out.println("Division by zero in Arithmetic expression");
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index is out-of-bounds ");

}
}
}

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.*;

public class Shapes extends Applet

//Function to initialize the applet

public void init()

setBackground(Color.white);

//Function to draw and fill shapes

public void paint(Graphics g)

//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;

public class Book1

public String title;

public String author;

public int year;

public String publisher;

public int price;

public Book1(String title,String author,int year,String publisher,int price)

{ this.title=title;

this.author=author;

this.year=year;

this.publisher=publisher;

this.price=price;

21
}

public void getTitle()

System.out.println("Title of Book: "+title);

public void getAuthor()

System.out.println("Author of Book: "+author);

public void getYear()

System.out.println("Year: "+year);

public void getPublisher()

System.out.println("Publisher of Book: "+publisher);

public void getPrice()

System.out.println("Price of Book: "+price);

public void setTitle(String title)

this.title=title;

public void setAuthor(String author)

22
this.author=author;

public void setYear(int year)

this.year=year;

public void setPublisher(String publisher)

this.publisher=publisher;

public void setCost(int price)

this.price=price;

public void show()

getTitle();

getAuthor();

getYear();

getPublisher();

getPrice();

23
import BookDetails.Book1;

public class Book2

public static void main(String args[])

Book1 b1=new Book1("Machine Learning","Goeduhub",2020,"Goeduhub",500);

b1.show();

Book1 b2=new Book1("Python Programmimg","Author 1",2018,"Goeduhub",475);

b2.show();

Output

24

You might also like