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

Document from ✨...?

The document is a practical lab record for B.Sc. (Computer Science with Data Analytics) students at Kathir College of Arts and Science, detailing various programming exercises in Java. It includes implementations of string extraction, multiple inheritance, exception handling, multithreading, GUI components, and file operations. Each exercise is structured with an aim, algorithm, program code, output, and results sections.

Uploaded by

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

Document from ✨...?

The document is a practical lab record for B.Sc. (Computer Science with Data Analytics) students at Kathir College of Arts and Science, detailing various programming exercises in Java. It includes implementations of string extraction, multiple inheritance, exception handling, multithreading, GUI components, and file operations. Each exercise is structured with an aim, algorithm, program code, output, and results sections.

Uploaded by

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

KATHIR COLLEGE OF ARTS AND

SCIENCE
(AFFILIATED TO BHARATHIAR UNIVERSITY)

“WISDOMTREE”, NEELAMBUR, COIMBATORE–641062.

NAME :

REGNO :

B.SC (COMPUTER SCIENCE WITH DATA ANALYTICS)


CORE LAB - IV
PROGRAMMING LAB – JAVA
KATHIR COLLEGE OF ARTS AND SCIENCE
(AFFILIATED TO BHARATHIAR UNIVERSITY)

“WISDOM TREE”, NEELAMBUR, COIMBATORE – 641 062.

DEPARTMENT OF COMPUTER SCIENCE WITH DATA ANALYTICS

NAME :

REG.NO :

SUBJECT CODE AND NAME :

CLASS :

Certified that this is the bonafide record of work done by


Mr. /Ms. Of B.SC (Computer Science
with Data Analytics) in
Laboratory during the academic year 2023-2024.

Faculty Incharge Head of the Department

Submitted for the Practical Examination held on___________

Internal Examiner External Examiner


Index

PROGRAM PAGE SIGNATURE OF THE


S.NO DATE
NAME NO FACULTY

1 STRING EXTRACTION

2 MULTIPLE INHERITENCE USING


INTERFACES

3 PAYOUT-OF-BOUNDS EXCEPTION

4 MULTITHREADING WITH
PRIOPITIES

5 DRAW SEVERAL SHAPES IN THE


CREATED WINDOW

6 BUTTON CLICKING WITH TEXT


FIELDS

7 MULTIPLE SELECT LIST-BOX

8 FRAME WITH TEXT FIELDS

9 MENU BARS AND PULL DOWN


MENUS

10 MOUSE CLICKING EVENTS

11 DRAWING SHAPES WITH MOUSE


CLICKS

12 APPENDING TEXT TO AN EXISTING


FILE
Ex.No :
Date :

AIM:

ALGORITHM:
STACK EXTRACTION
Program:

import java.io.*;
class prgone
{
public static void main(String args[])throws IOException
{
DataInputStream x=new DataInputStream(System.in);
String s,s1;
int start,end;
System.out.println("Enter the string");
s=x.readLine();
System.out.println("Enter starting position");
start=Integer.parseInt(x.readLine());
System.out.println("Enter the ending position");
end=Integer.parseInt(x.readLine());
s1=s.substring(start,end);
System.out.println("the given string is:"+s);
System.out.println("The extracted string is:"+s1);
}
}
OUTPUT:

D:\java\bin>javac prgone.java
D:\java\bin>java prgone

Enter the string:

KATHIR COLLEGE OF ARTS AND SCIENCE

Enter starting position: 7


Enter the ending position: 11
The given string is: KATHIR COLLEGE OF ARTS AND SCIENCE
The extracted string is: OLLE

D:\java\bin>

RESULT:
Ex.No :
Date :

AIM:

ALGORITHM:
MULTIPLE INHERITANCE USING INTERFACES

PROGRAM:
import java.io.*;
interface A
{
int a=1;
public void getdata();
}
interface B
{
int b=20;
public void putdata();
}
class C implements A,B
{
public void getdata()
{
System.out.println("the value of A:"+a);
}
public void putdata()
{
System.out.println("the value of B:"+b);
}
public void cal()
{
int sum=a+b;
System.out.println("the sum of 2 number are="+sum);
}
}
class prgtwo
{
public static void main(String args[])
{
C c1=new C();
c1.getdata();
c1.putdata();
c1.cal();
}
}
OUTPUT:

RESULT:
Ex.No :
Date :

AIM:

ALGORITHM:
PAYOUT-OF-BOUNDS EXCEPTION

PROGRAM:

import java.io.*;
class prg extends Exception
{
prg()
{
String s;
System.out.println("sorry your cheque is dishonoured");
}
}
public class prgthree
{
public static void main(String p[])throws IOException
{
DataInputStream d=new DataInputStream(System.in);
int bal=10000,wd;
System.out.println();
System.out.println("..........");
System.out.println("pay out of bounds");
System.out.println("..........");
System.out.println("enter the withdraw amount");
wd=Integer.parseInt(d.readLine());
if(wd>=bal)
{
System.out.println();
new prg();
System.out.println();
}
else
{
System.out.println();
System.out.println("your cheque is passed");
System.out.println();
}
}
}
OUTPUT:

RESULT:
Ex.No :
Date :

AIM:

ALGORITHM:
MULTITHREADING WITH PRIORITIES
PROGRAM:

import java.io.*;
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(i+"*"+5+"="+(i*5));
}
System.out.println("END OF THE 1st THREAD");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=7;j++)
{
System.out.println(j+"*"+7+"="+(j*7));
}
System.out.println("END OF THE 2nd THREAD");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=13;k++)
{
System.out.println(k+"*"+13+"="+(k*13));
}
System.out.println("END OF THE 3rd THREAD");
}
}
public class prgfour
{
public static void main(String args[])throws IOException
{
A ThreadA=new A();
B ThreadB=new B();
C ThreadC=new C();
ThreadA.setPriority(Thread.MAX_PRIORITY);
ThreadB.setPriority(Thread.NORM_PRIORITY);
ThreadC.setPriority(Thread.MIN_PRIORITY);
ThreadA.start();
ThreadB.start();
ThreadC.start();
}
}
Output:

RESULT:
Ex.No:
Date :

AIM:

ALGORITHM:
DRAW SEVERAL SHAPES IN THE CREATED WINDOW

PROGRAM:

import java.io.*;
import java.awt.*;
import java.applet.*;
/*<applet code="prgfive"width=400 height=400></applet>*/
public class prgfive extends Applet
{
public void paint(Graphics g)
{
Font f1=new Font("Helvitical",Font.BOLD+Font.ITALIC,30);
g.setFont(f1);
g.setColor(Color.red);
g.drawString("Shapes for you|||",15,300);
g.drawRect(10,25,50,30);
g.drawLine(75,40,130,40);
g.drawOval(150,20,70,35);
g.drawOval(220,20,70,35);
g.drawArc(295,20,60,60,0,90);
g.drawLine(380,20,380,60);
g.drawRect(420,20,30,30);
g.fillOval(10,90,35,35);
g.fillOval(105,90,55,35);
g.fillRect(255,90,30,30);
g.fillRect(315,100,50,20);
}
}
OUTPUT:

RESULT:
Ex.No :
Date :

AIM:

ALGORITHM:
BUTTON CLICKING WITH TEXT FIELDS
PROGRAM:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="prgsix.class",width=500
height=500></applet>*/
public class prgsix extends Applet implements ActionListener
{
TextField name,street,city,pincode;
Button mydetails;
public void init()
{
Label n=new Label("name",Label.RIGHT);
Label s=new Label("street",Label.RIGHT);
Label c=new Label("city",Label.RIGHT);
Label p=new Label("pincode",Label.RIGHT);
Button mydetails=new Button("mydetails");
name=new TextField(15);
street=new TextField(15);
city=new TextField(15);
pincode=new TextField(15);
add(n);
add(name);
add(s);
add(street);
add(c);
add(city);
add(p);
add(pincode);
add(mydetails);
name.addActionListener(this);
street.addActionListener(this);
city.addActionListener(this);
pincode.addActionListener(this);
mydetails.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("mydetails"))
{
name.setText("SASI");
street.setText("PARUVAI STREET");
city.setText("PALLADAM");
pincode.setText("641668");
}
}
}
OUTPUT:

RESULT:
Ex.No :
Date :

AIM:

ALGORITHM:
MULTIPLE SELECT LIST-BOX
Program:

import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class selectlist extends Frame
{
List l;
Button display;
String selected []={};
public selectlist()
{
setLayout(new FlowLayout());
setTitle("ListBox Example");
setBackground(Color.red);
l=new List(7,true);
l.add("violet");
l.add("indigo");
l.add("blue");
l.add("green");
l.add("yellow");
l.add("orange");
l.add("red");
add(l);
display=new Button("display");
add(display);
display.addActionListener(new Buttonhandler());
l.addItemListener(new listHandler());
addWindowListener(new Windowhandler());
}
class Windowhandler extends WindowAdapter
{
public void WindowClosing(WindowEvent we)
{
System.exit(0);
}
}
class listHandler implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
selected=l.getSelectedItems();
}
}
class Buttonhandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{

String arg=ae.getActionCommand();
if(arg.equals("display"))
{
repaint();
}
}
}
public void paint(Graphics g)
{
g.setColor(Color.black);
for(int i=0,x=25;i<selected.length;i++,x+=25)
{
g.drawString(selected[i],100,150+x);
}
}
public static void main(String args[])
{
selectlist s;
s=new selectlist();
s.setBounds(1,1,400,400);
s.show();
}
}
OUTPUT:

RESULT:
Ex.No:
Date :

AIM:

ALGORITHM:
FRAME WITH TEXT FIELDS

PROGRAM:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class prg8 extends Frame implements ActionListener
{
Label p1,p2,p3,p4,p5;
TextField t1,t2,t3,t4,t5;
Button b1,b2;
prg8()
{
p1=new Label("Name");
p2=new Label("Age");
p3=new Label("Qualification");
p4=new Label("Address");
p5=new Label("Pincode");
t1=new TextField(20);
t2=new TextField(20);
t3=new TextField(20);
t4=new TextField(20);
t5=new TextField(20);
b1=new Button("Enter");
b2=new Button("Exit");
setLayout(new GridLayout(10,15));
add(p1);
add(t1);
add(p2);
add(t2);
add(p3);
add(t3);
add(p4);
add(t4);
add(p5);
add(t5);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setSize(500,500);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
t1.setText("SHIVAM");
t2.setText("20");
t3.setText("B.SC");
t4.setText("INDIA");
t5.setText("641 111");
}
else
{
System.exit(0);
}
}
public static void main(String args[])
{
new prg8();
}
}
OUTPUT:

RESULT:
Ex.No :
Date :

AIM:

ALGORITHM:
MENU BARS AND PULL DOWN MENUS

PROGRAM:

import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class prg9 extends Frame
{
prg9()
{
TextArea t1;
setTitle("KATHIR COLLEGE OF ARTS AND SCIENCE");
t1=new TextArea(10,40);
MenuBar mb=new MenuBar();
Menu m=new Menu("file");
MenuItem m1=new MenuItem("new ctrl+n");
MenuItem m2=new MenuItem("open ctrl+c");
MenuItem m3=new MenuItem("save ctrl+s");
MenuItem mn=new MenuItem("save As");
MenuItem nu=new MenuItem("print ctrl+p");
Menu mmm=new Menu("exit");
Menu mm=new Menu("edit");
MenuItem mm1=new MenuItem("cut ctrl+x");
MenuItem mm2=new MenuItem("copy ctrl+c");
MenuItem mm3=new MenuItem("paste ctrl+v");
mb.add(m);
m.add(m1);
m.add(m2);
m.add(m3);
m.add(mn);
m.addSeparator();
m.add(nu);
mb.add(mm);
m.add(mmm);
mm.add(mm1);
mm.add(mm2);
mm.add(mm3);
setMenuBar(mb);
add(t1);
}
public static void main(String args[])
{
prg9 md=new prg9();
md.setVisible(true);
md.setSize(800,700);
}
public boolean handleEvent(Event e)
{
if(e.id==Event.WINDOW_DESTROY)
System.exit(0);
return true;
}
}
OUTPUT:

RESULT:
Ex.No:
Date :

AIM:

ALGORITHM:
MOUSE CLICKING EVENT

PROGRAM:

import java.io.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class prg10 extends Frame implements MouseListener
{
Label ll;
prg10()
{
ll=new Label(" ");
setLayout(new FlowLayout());
add(ll);
addMouseListener(this);
addWindowListener(new W());
}
class W extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
public void mouseReleased(MouseEvent e)
{
ll.setText("Mouse Released");
}
public void mouseEntered(MouseEvent e)
{
ll.setText("Mouse entered");
}
public void mousePressed(MouseEvent e)
{
ll.setText("Mouse Pressed");
}
public void mouseExited(MouseEvent e)
{
ll.setText("Mouse exited");
}
public void mouseClicked(MouseEvent e)
{
ll.setText("Mouse Clicked");
}
public static void main(String args[])
{
prg10 p1=new prg10();
p1.setVisible(true);
p1.setSize(600,500);
}
}
OUTPUT:

RESULT:
Ex.No:
Date :

AIM:

ALGORITHM:
DRAWING SHAPES WITH MOUSE CLICKS

PROGRAM:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="prg11.class"width="300", height="300">
</applet>*/
public class prg11 extends Applet implements
KeyListener,MouseListener
{
int x=1;
int x1,x2,y1,y2;
int i=0,j=0;
public void init()
{
addMouseListener (this);
addKeyListener(this);
}
public void keyPressed (KeyEvent ke)
{
char ch=ke.getKeyChar();
if(ch=='o')
x=1;
if(ch=='c')
x=2;
if(ch=='r')
x=3;
if(ch=='s')
x=4;
}
public void keyEntered(KeyEvent ke){}
public void keyReleased(KeyEvent ke){}
public void keyTyped(KeyEvent ke){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
i=x1;
j=y1;
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
repaint();
}
public void mouseEntered(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void paint(Graphics g)
{
g.drawString("Circle(c),Oval(o),Rectangle(r),Square(s)",50,50);
if(x==1)
g.drawOval(x1,y1,x2-x1,y2-y1);
if(x==2)
g.drawOval(x1,y1,x2-x1,y2-y1);
if(x==3)
g.drawRect(x1,y1,x2-x1,y2-y1);
if(x==4)
g.drawRect(x1,y1,x2-x1,y2-y1);
}
}
OUTPUT:
RESULT:
Ex.No :

Date :

AIM:

ALGORITHM:
APPENDING TEXT TO AN EXISTING FILE
PROGRAM:

import java.io.*;
class prg12
{
public static void main(String args[])
{
RandomAccessFile newfile;
try
{
newfile=new RandomAccessFile("new.txt","rw");
newfile.seek(newfile.length());
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the text to be updated");
String s=in.readLine();
newfile.writeBytes(s);
newfile.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
OUTPUT:

RESULT:

You might also like