LAB PROGRAMS
Lab Program1: Student details
import [Link];
class student
{
int rno,sum;
String name;
int marks[]=new int[6];
float avg;
char grade;
student()
{
rno=101;
name="xyz";
marks[0]=78;marks[1]=89;marks[2]=98;
marks[3]=87;marks[4]=88;marks[5]=76;
}
void read()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter student number:");
rno=[Link]();
[Link]("Enter student name:");
[Link]();
name=[Link]();
[Link]("Enter six subject marks:");
for(int i=0;i<6;i++)
{
marks[i]=[Link]();
}
}
void display()
{
[Link]("Student roll number: "+rno);
[Link]("Student name: "+name);
[Link]("Student six subject marks:");
for(int i=0;i<6;i++)
{
[Link](marks[i]);
}
[Link]("Student total marks: "+sum);
[Link]("Student average marks:"+avg);
[Link]("Student grade: "+grade);
}
void average()
{
sum=0;
for(int i=0;i<6;i++)
sum+=marks[i];
avg=sum/6.0f;
}
void gradecalc()
{
switch((int)avg/10)
{
case 10:
case 9: grade='A';break;
case 8:
case 7: grade='B';break;
case 6:
case 5: grade='C';break;
case 4:
case 3: grade='D';break;
case 2:
case 1:
case 0: grade='F';break;
}
}
}
class studentdemo
{
public static void main(String[] args)
{
/*student s=new student();
[Link]();
[Link]();
[Link]();
student s1=new student();
[Link]();
[Link]();
[Link]();
[Link]();*/
int n;
Scanner sc=new Scanner([Link]);
[Link]("Enter number of students:");
n=[Link]();
student s[]=new student[n];
for(int i=0;i<n;i++)
{
s[i]=new student();
s[i].read();
s[i].average();
s[i].gradecalc();
s[i].display();
}
}
}
Lab Program 2: Transpose of matrix
import [Link];
class transpose
{
int m,n;
int x[][];
transpose()
{
m=3;
n=3;
}
transpose(int m,int n)
{
this.m=m;
this.n=n;
}
void read()
{
Scanner sc=new Scanner([Link]);
x=new int[m][n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
x[i][j]=[Link]();
}
void display()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
[Link](x[i][j]+" ");
}
[Link]();
}
}
void transposeresult()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
[Link](x[j][i]+" ");
}
[Link]();
}
}
}
class transposedemo
{
public static void main(String[] args)
{
int m,n;
Scanner sc=new Scanner([Link]);
[Link]("Enter size of matrix:");
m=[Link]();
n=[Link]();
transpose t=new transpose(m,n);
[Link]("Enter "+m+"*"+n+" matrix:");
[Link]();
[Link]("Given "+m+"*"+n+" matrix:");
[Link]();
[Link]("Transpose:");
[Link]();
}
}
Lab Program 3: Complex number operations
import [Link];
class complex
{
int real,img;
complex()
{
real=2;
img=3;
}
complex(int real,int img)
{
[Link]=real;
[Link]=img;
}
void read()
{
Scanner sc=new Scanner([Link]);
real=[Link]();
img=[Link]();
}
void display()
{
[Link](real+"+i"+img);
}
complex sum(complex c1,complex c2)
{
complex result=new complex();
[Link]=[Link]+[Link];
[Link]=[Link]+[Link];
return result;
}
complex sub(complex c1,complex c2)
{
complex result=new complex();
[Link]=[Link];
[Link]=[Link];
return result;
}
}
class complexdemo
{
public static void main(String[] args)
{
complex c1=new complex();
complex c2=new complex();
complex c3=new complex();
[Link]("Enter real and imaginary values of first
complex number");
[Link]();
[Link]("Enter real and imaginary values of second
complex number");
[Link]();
//[Link]();
//[Link]();
[Link]("First complex number:");
[Link]();
[Link]("Second complex number:");
[Link]();
c3=[Link](c1,c2);
[Link]("Addition=");
[Link]();
c3=[Link](c1,c2);
[Link]("Subraction=");
[Link]();
}
}
Lab Program 4:
class Staff
{
String name, addr;
int age;
Staff()
{
name="Gopal";
age=28;
addr="kurnool";
}
Staff(String n,int a,String ad)
{
name=n;
age=a;
addr=ad;
}
void Sdisplay()
{
[Link]("Name: "+name);
[Link]("Age: "+age);
[Link]("Address: "+addr);
}
}
class NTstaff extends Staff
{
String qual;
int yoe;
int salary;
NTstaff()
{
qual="[Link]";
yoe=0;
salary=10000;
}
NTstaff(String n,int a,String ad,String q,int y,int s)
{
super(n,a,ad);
qual=q;
yoe=y;
salary=s;
}
void NTdisplay()
{
[Link]();
[Link]("Qualification: "+qual);
[Link]("Years of experience: "+yoe);
[Link]("Salary: "+salary);
}
}
class Tstaff extends Staff
{
String qual;
int yoe;
Tstaff()
{
qual="[Link]";
yoe=2;
}
Tstaff(String n,int a,String ad,String q,int y)
{
super(n,a,ad);
qual=q;
yoe=y;
}
void Tdisplay()
{
[Link]();
[Link]("Qualification: "+qual);
[Link]("Years of experience: "+yoe);
}
}
class RTstaff extends Tstaff
{
double bs,hra,da,tax,gs;
RTstaff()
{
bs=16500;
hra=0.1*bs;
da=0.5*bs;
tax=0.15*bs;
}
RTstaff(String n,int a, String ad,String q,int y, int b)
{
super(n,a,ad,q,y);
bs=b;
hra=0.1*bs;
da=0.5*bs;
tax=0.15*bs;
}
void calcgs()
{
[Link]();
gs=bs+hra+da-tax;
[Link]("Gross salary: "+gs);
}
}
class TTstaff extends Tstaff
{
double cs;
TTstaff()
{
cs=23000;
}
TTstaff(String n,int a, String ad,String q,int y, int s)
{
super(n,a,ad,q,y);
cs=s;
}
void calccs()
{
[Link]();
[Link]("Consolidated salary: "+cs);
}
}
class inherlab
{
public static void main(String args[])
{
RTstaff r1=new RTstaff();
[Link]("-------------------------------");
[Link]("Regular Teaching Staff Details:");
[Link]("-------------------------------");
[Link]();
RTstaff r2=new
RTstaff("Ram",35,"Hyderabad","PhD",5,40000);
[Link]("*******************************");
[Link]();
TTstaff t1=new TTstaff();
[Link]("-------------------------------");
[Link]("Temporary Teaching Staff Details:");
[Link]("-------------------------------");
[Link]();
TTstaff t2=new TTstaff("Raju",29,"Mumbai","PhD",5,45000);
[Link]("*******************************");
[Link]();
NTstaff n1=new NTstaff();
[Link]("-------------------------------");
[Link]("Non-Teaching Staff Details:");
[Link]("-------------------------------");
[Link]();
NTstaff n2=new
NTstaff("Swathi",25,"Banglore","[Link]",3,15000);
[Link]("*******************************");
[Link]();
}
}
Lab Program 6: Package
package std;
public class Student
{
String name;
int rno,m1,m2,m3;
char grade;
int total;
public Student(String s,int no,int a,int b,int c)
{
name=s;
rno=no;
m1=a;
m2=b;
m3=c;
}
public int getTotal()
{
total=m1+m2+m3;
return total;
}
public void display(double avg,char grade)
{
[Link]("--------------------------------------");
[Link]("Student details:");
[Link]("Name of the student: "+name);
[Link]("Roll num of the student: "+rno);
[Link]("Marks in three subjects: "+m1+"\t"+m2+"\t"+m3);
[Link]("Total marks : "+total);
[Link]("Average marks: "+avg);
[Link]("Grade: "+grade);
}
}
package [Link];
public class Grade
{
int total;
double avg;
char grade;
public double average(int t)
{
total=t;
avg=total/3.0;
return avg;
}
public char getGrade()
{
if(avg>=70)
grade='A';
else if(avg>=50)
grade='B';
else if(avg>=35)
grade='C';
else
grade='F';
return grade;
}
}
import std.*;
import [Link].*;
import [Link];
class StudentResult
{
public static void main(String arg[ ])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Name of the student:");
String name=[Link]();
[Link]("Enter Roll number of the student:");
int no=[Link]();
[Link]("Enter Marks in first subject:");
int s1=[Link]();
[Link]("Enter Marks in second subject:");
int s2=[Link]();
[Link]("Enter Marks in third subject:");
int s3=[Link]();
Student s=new Student(name,no,s1,s2,s3);
int t=[Link]();
Grade gr=new Grade();
double avg=[Link](t);
char g=[Link]();
[Link](avg,g);
}
}
Lab Program 8: Strings
package practice;
import [Link];
public class StringLab {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "Welcome! This is CS212 Java Course";
[Link]("The given sentence is :\n"+s);
[Link]("After converting to lowercase letters
:\n"+[Link]());
[Link]("After converting to uppercase letters
:\n"+[Link]());
[Link]("Index of the word Course is :
"+[Link]("Course"));
s=[Link]("CS212","CSE212");
[Link]("Sentence after replacing CS212 with
CSE212 is\n"+s);
int sum=0;
for(int i=0;i<[Link]();i+=2)
sum+=[Link](i);
[Link]("Sum of ASCII values at even position
is : "+sum);
[Link]("Type your name:");
Scanner sc=new Scanner([Link]);
String name=[Link](); // Ramana Maharshi
//int sp=[Link](" ");
//[Link]("Your name is :
"+[Link](sp+1)+","+[Link](0)+".");
String str[]=[Link](" ");
[Link]("Your name is :
"+str[1]+","+str[0].charAt(0)+".");
}
Lab Program 9:
package practice;
import [Link];
import [Link];
public class Exceptiondemo7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
baoperations();
}
catch(ArithmeticException e)
{
[Link]("Arithmetic Exception Caught,
please give the value other than zero as second integer");
[Link]("Exception caught: "+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("\nException caught: "+e);
}
catch(InputMismatchException e)
{
[Link]("Excetion caught: "+e);
}
catch(Exception e)
{
[Link]("\nException caught: "+e);
}
finally
{
[Link]("Finally block executed.");
}
}
private static void baoperations() throws ArithmeticException,
ArrayIndexOutOfBoundsException, InputMismatchException{
// TODO Auto-generated method stub
Scanner sc=new Scanner([Link]);
int fn,sn;
int x[]= {10,20,30,40,50};
[Link]("Enter two integers: ");
fn=[Link]();
sn=[Link]();
[Link]("Addition: "+(fn+sn));
[Link]("Subtraction: "+(fn-sn));
[Link]("Multiplication: "+(fn*sn));
[Link]("Divison: "+(fn/sn));
for(int i=0;i<=[Link];i++)
[Link](x[i]+" ");
[Link]("Program Executed without
exception.");
[Link]();
}
Lab Program 10:
package practice;
import [Link];
public class ExceptionLab {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner([Link]);
[Link]("Enter the name of the student: ");
String name=[Link]();
[Link]("Enter course name : ");
String course=[Link]();
[Link]("Enter Registered number XXXX format:
");
int regnum=[Link]();
try
{
studentDetails(name,course,regnum);
}
catch(SeatsFilledException e)
{
[Link]("Exception caught: "+e);
}
[Link]();
}
public static void studentDetails(String n,String c,int r)
throws SeatsFilledException
{
if((r%100)>60)
throw new SeatsFilledException(r%100);
[Link]("Details of student are: ");
[Link]("Name of the student : "+n);
[Link]("Course name : "+c);
[Link]("Registered number : "+r);
}
}
class SeatsFilledException extends Exception
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
int maxcount;
SeatsFilledException(int c)
{
maxcount=c;
}
public String toString()
{
return "Seats are filled, limit exceeded! "+maxcount;
}
}
Lab Program 11: Multi-thread program using Synchronization
Concept
package practice;
public class ThreadLab1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
resource ob=new resource();
Mthread1 t1=new Mthread1(ob);
Mthread2 t2=new Mthread2(ob);
[Link]("Thread1 started");
[Link]();
[Link]("Thread2 started");
[Link]();
}
}
class resource
{
synchronized void MTable(int n)
{
[Link](n+"'s Multiplication table:");
for(int i=1;i<=10;i++)
{
[Link](n+" * "+i+" = "+(n*i));
try
{
[Link](1000);
}
catch(InterruptedException e)
{
[Link](e);
}
}
}
}
class Mthread1 extends Thread
{
resource r;
Mthread1(resource ob)
{
r=ob;
}
public void run()
{
[Link](2);
}
}
class Mthread2 extends Thread
{
resource r;
Mthread2(resource ob)
{
r=ob;
}
public void run()
{
[Link](5);
}
}
Lab Program 12: Producer-Consumer Problem
package practice;
public class ThreadLab2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Queue q = new Queue();
Producer p = new Producer(q);
Consumer c = new Consumer(q);
[Link]();
[Link]();
}
class Queue //Producer-consumer problem using Interthread
communication
{
int n;
boolean full = false;
synchronized void get()
{
if(full==false)
{
try
{
wait();
}
catch(InterruptedException e)
{
[Link]("InterruptedException
caught");
}
}
[Link]("Consumed: " + n);
full = false;
notify();
}
synchronized void put(int n)
{
if(full==true)
{
try
{
wait();
}
catch(InterruptedException e)
{
[Link]("InterruptedException
caught");
}
}
this.n = n;
full = true;
[Link]("Produced: " + n);
notify();
}
}
class Producer extends Thread
{
Queue q;
Producer(Queue q)
{
this.q = q;
}
public void run()
{
int i = 0;
while(true)
{
[Link](i++);
/*if(i==10)
break;*/
}
}
}
class Consumer extends Thread
{
Queue q;
Consumer(Queue q)
{
this.q = q;
}
public void run()
{
while(true)
{
[Link]();
}
}
}
Lab Program 13: Files
13.a:
package practice;
import [Link].*;
public class FileLab1 {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
FileWriter fw=new FileWriter("[Link]");
BufferedWriter bw=new BufferedWriter(fw);
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter number of the students:");
int n;
n=[Link]([Link]());
String s;
[Link](" Number "+" Name "+" CGPA\n");
[Link](" ---------- "+" -------- "+" ------- \n");
for(int i=0;i<n;i++)
{
[Link]("Enter Student "+(i+1)+"
details:");
[Link]("Enter Student Number:");
s=[Link]();
[Link](" "+s+" ");
[Link]("Enter Student Name:");
s=[Link]();
[Link](" "+s+" ");
[Link]("Enter Student CGPA:");
s=[Link]();
[Link](" "+s);
[Link]("\n");
}
[Link]();
[Link]("The Student data is successfully
written to the file.");
}
13.b:
package practice;
import [Link].*;
public class FileLab2 {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
FileReader fr=new FileReader("[Link]");
BufferedReader br=new BufferedReader(fr);
[Link]("The student details are:");
String s;
while((s=[Link]())!=null) // End of file is null
value
[Link](s);
[Link]();
[Link]("Student data is successfully read
from the file.");
}
13.c:
package practice;
import [Link].*;
public class FileLab3 {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
FileReader fr=new FileReader("[Link]");
FileWriter fw=new FileWriter("student_copy.txt");
FileReader fr1=new FileReader("student_copy.txt");
BufferedReader br=new BufferedReader(fr);
BufferedReader br1=new BufferedReader(fr1);
BufferedWriter bw=new BufferedWriter(fw);
String s;
while((s=[Link]())!=null)
{
[Link](s);
[Link]("\n");
}
[Link]();
[Link]();
[Link]("Student data is copied from one
file to another file successfully.");
[Link]("Student details:");
while((s=[Link]())!=null)
{
[Link](s);
}
[Link]();
[Link]();
}
Lab Program 14: Applets
package Practice;
import [Link].*;
import [Link];
public class AppletLab extends Applet{
Font f;
public void init()
{
f=new Font("Verdana",[Link],28);
}
public void paint(Graphics g)
{
[Link]([Link]);
[Link](f);
[Link]("We are CSE students of GPREC",30,50);
[Link]([Link]);
[Link](50,60,100,200);
[Link](150,100,200,150);
[Link](200,300,80,60);
[Link](10,250,80,50,10,10);
[Link](10, 350, 100, 100);
[Link](200, 450, 100, 100);
[Link](60, 470, 100, 100, 10, 10);
[Link]([Link]);
[Link](300,150,30,60);
[Link](170,150,30,60);
[Link](220,150,60,60);
[Link](270,200,30,30);
[Link]([Link]);
int x[ ]={800,700,600,500};
int y[ ]={150,120,100,120};
int n=4;
[Link](x,y,n);
[Link]([Link]);
[Link](f);
[Link]("Working with Graphics class using
Applet",100,650);
}
}
Lab Program 15:
15.a)
package Practice;
import [Link].*;
import [Link].*;
import [Link].*;
public class AWTLab1 extends Applet implements KeyListener{
String msg="Key Pressed: ";
Font f;
public void init()
{
setBackground(Color.DARK_GRAY);
setSize(500,500);
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key pressed");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key released");
}
public void keyTyped(KeyEvent ke)
{
msg+=[Link]();
repaint();
}
public void paint(Graphics g)
{
f = new Font("Verdana",[Link],24);
[Link](f);
[Link]([Link]);
[Link](msg,100,150);
}
}
15.b)
package Practice;
import [Link].*;
import [Link].*;
import [Link].*;
public class AWTLab2 extends Applet implements MouseListener,
MouseMotionListener{
String msg="";
int mx=0;
int my=10;
Font f;
public void init()
{
setBackground(Color.DARK_GRAY);
setSize(500,500);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mx=10;
my=150;
msg="Mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mx=10;
my=100;
msg="Mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
mx=0;
my=50;
msg="Mouse exited";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mx=[Link]();
my=[Link]();
msg="Mouse released: "+mx+","+my;
showStatus(msg);
repaint();
}
public void mousePressed(MouseEvent me)
{
mx=[Link]();
my=[Link]();
msg="Mouse pressed: "+mx+", "+my;
showStatus(msg);
repaint();
}
public void mouseDragged(MouseEvent me)
{
showStatus("Mouse dragged: "+[Link]()+", "+[Link]());
}
public void mouseMoved(MouseEvent me)
{
showStatus("Mouse moved: "+[Link]()+", "+[Link]());
}
public void paint(Graphics g)
{
f = new Font("Verdana",[Link],24);
[Link](f);
[Link]([Link]);
[Link](msg,mx,my);
}
}
Lab Program 16:
16.a)
package Practice;
import [Link].*;
import [Link].*;
import [Link].*;
public class AWTLab3 extends Applet implements ActionListener{
Label l1,l2,l3;
TextField t1,t2;
Button b1,b2;
Font f;
public void init()
{
setForeground([Link]);
setBackground(Color.DARK_GRAY);
setSize(600,500);
l1 = new Label("Enter a value: ");
l2 = new Label("Factorial:");
l3 = new Label("Applet application to find Factorial of a
number");
t1 = new TextField(10);
t2 = new TextField(10);
b1 = new Button("Calculate");
b2 = new Button("Clear");
f = new Font("Arial",[Link],24);
setFont(f);
add(l3);
add(l1);
add(t1);
add(b1);
add(b2);
add(l2);
add(t2);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
int n=[Link]([Link]());
int fact=1;
if([Link]()==b1)
{
if(n==0||n==1)
{
fact=1;
[Link]([Link](fact));
}
else
{
for(int i=1;i<=n;i++)
fact=fact*i;
[Link]([Link](fact));
}
}
else if([Link]()==b2)
{
[Link]("");
[Link]("");
}
}
}
16.b)
package Practice;
import [Link].*;
import [Link].*;
import [Link].*;
public class AWTLab4 extends Applet implements ActionListener{
Label l1,l2,l3,l4,l5,l6,l7;
List lt1,lt2;
TextField t1,t2,t3;
Choice c1,c2;
Button b1;
String msg="";
Font f;
public void init()
{
setForeground([Link]);
setBackground(Color.DARK_GRAY);
setSize(600,1000);
l1=new Label("Name:");
l2=new Label("Roll Number:");
l3=new Label("CGPA:");
l4=new Label("Gender:");
l5=new Label("Semester:");
l6=new Label("Branch:");
l7=new Label("Select languages:");
t1=new TextField(25);
t2=new TextField(15);
t3=new TextField(15);
c1=new Choice();
[Link]("Male");
[Link]("Female");
c2=new Choice();
[Link]("I");
[Link]("II");
[Link]("III");
[Link]("IV");
[Link]("V");
[Link]("VI");
[Link]("VII");
[Link]("VIII");
lt1=new List(4,false);
lt2=new List(3,true);
[Link]("CSE");
[Link]("ECE");
[Link]("EEE");
[Link]("ME");
[Link]("C");
[Link]("C++");
[Link]("Java");
b1=new Button("Submit");
f = new Font("Arial",[Link],24);
setFont(f);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(c1);
add(l5);
add(c2);
add(l6);
add(lt1);
add(l7);
add(lt2);
add(b1);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
[Link](f);
[Link]([Link]);
msg="Your Name is : "+[Link]();
[Link](msg,20,400);
msg="Your Roll Number is : "+[Link]();
[Link](msg, 20,430);
msg="CGPA is : "+[Link]();
[Link](msg,20,460);
msg="Gender is : "+[Link]();
[Link](msg,20,490);
msg="Semester is : "+[Link]();
[Link](msg,20,520);
msg="Your branch is : "+[Link]();
[Link](msg,20,550);
msg="Language known to you are : ";
String s2[ ]=[Link]();
for(int i=0;i<[Link];i++)
msg+=s2[i]+", ";
[Link](msg,20,580);
}
}