CONTENT
[Link]: DATE TITLE PAGE NO SIGN
PRIME NUMBERS
01
MULTIPLY TWO
02 MATRICES
COUNT CHARACTERS, LINES
03 AND WORDS IN TEXT
GENERATE RANDOM NUMBERS
04
USING RANDOM CLASS
STRING MANIPULATION USING
05
CHARACTERARRAY
STRING OPERATION USING
06
STRING CLASS
STRING OPERATION USING
07
STRING BUFFER CLASS
08 MULTI THREAD APPLICATION
SYNCHRONIZED THREAD
09
10 EXCEPTION HANDLING
DISPLAYS INFORMATION ABOUT
11
FILE
12 CHANGE TEXT SIZE AND FONT
13 MOUSE EVENT HANDLING
SIMPLE CALCULATOR USING A
14
GRID LAYOUT
TRAFFIC LIGHT USING RADIO
15 BUTTON
PRIME NUMBERS
Ex No:01
PROGRAM
import [Link].*;
import [Link];
public class prime
{
public static void main(String arg[])
{
int i,n,counter,j;
Scanner scanner=new Scanner([Link]);
[Link]("Enter the n value:");
n=[Link]();
[Link]("prime numbers are:");
for(j=2;j<=n;j++)
{
counter=0;
for(i=1;i<=j;i++)
{
if(j%i==0)
{
counter++;
}
}
if(counter==2)
[Link](j+" ");
}
}
}
PRIME NUMBERS
Ex No:01
OUTPUT
Ex no:02 MULTIPLY TWO MATRICES
PROGRAM
import [Link].*;
class matrix
{
public static void main(String arg[])
{
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];
}
[Link](c[i][j]+" ");
}
}
}
}
Ex no:02 MULTIPLY TWO MATRICES
OUTPUT
Ex No:03 COUNT CHARACTERS, LINES AND WORDS IN TEXT
PROGRAM
import [Link].*;
class CountCharacter
{
public static void main(String arg[])
{
String string="the best\nof both\nworlds";
int count=0,lines=0,words=0;
for(int i=0;i<[Link]();i++)
{
if([Link](i)!=1)
count++;
}
lines=[Link]("[\n|\r]").length;
for(int i=0;i<[Link]()-1;i++)
{
if([Link](i)==' '&&[Link](i+1)!=' ')
words++;
}
[Link]("total number of characters in a string:"+count);
[Link]("total number of lines in a string:"+lines);
[Link]("total number of words in a string are:"+(words+1));
}
}
Ex No:03 COUNT CHARACTERS, LINES AND WORDS IN TEXT
OUTPUT
[Link] GENERATE RANDOM NUMBERS USING RANDOM CLASS
PROGRAM
import [Link];
class generaterandom
{
public static void main(String args[])
{
Random rand=new Random();
int x=[Link](1000);
int y=[Link](1000);
[Link]("Random integers:"+x);
[Link]("Random integers:"+y);
}
}
[Link] GENERATE RANDOM NUMBERS USING RANDOM CLASS
OUTPUT
[Link] STRING MANIPULATION USING CHARACTERARRAY
PROGRAM
import [Link].*;
import [Link].*;
class characterarray
{
public static voidmain(String args[])
{
char []k1={'s','o','f','t','w','a','r','e'};
String s1=new String(k1);
String s2=new String(k1);
int len=[Link];
char ch=[Link](2);
String s3=[Link](s2);
[Link]("char array length:"+len);
[Link]("character at a particular position:"+ch);
[Link]("concatenating two strings:"+s3);
}
}
[Link] STRING MANIPULATION USING CHARACTERARRAY
OUTPUT
[Link] STRING OPERATION USING STRING CLASS
PROGRAM
import [Link].*;
import [Link].*;
class string
{
public static void main(String args[])
{
String k1="software";
String k2="computer science";
String k3=[Link](k2);
String k4=[Link](0,4);
[Link]("concatinating two strings:"+k3);
[Link]("substring is:"+k4);
}
}
[Link] STRING OPERATION USING STRING CLASS
OUTPUT
[Link] STRING OPERATION USING STRING BUFFER CLASS
PROGRAM
import [Link].*;
import [Link].*;
class buffer
{
public static void main(String args[])
{
StringBuffer k1=new StringBuffer("software");
StringBuffer k2=new StringBuffer("computer science");
[Link]();
[Link](1,4);
[Link]("string length is: "+[Link]());
[Link]("reverse a string:"+k1);
[Link]("delete a substing from the given string:"+k2);
}
}
[Link] STRING OPERATION USING STRING BUFFER CLASS
OUTPUT
[Link] MULTI THREAD APPLICATION
PROGRAM
import [Link];
class Square extends Thread
{
int x;
Square(int n)
{
x=n;
}
public void run()
{
int sqr=x*x;
[Link]("Square of"+x+"="+sqr);
}
}
class Cube extends Thread
{
int x;
Cube(int n)
{
x=n;
}
public void run()
{
int cub=x*x*x;
[Link]("cube of"+x+"="+cub);
}
}
class Number extends Thread
{
public void run()
{
Random random=new Random();
for(int i=0;i<5;i++)
{
int randominteger=[Link](100);
[Link]("random integer generated:"+randominteger);
Square s=new Square(randominteger);
[Link]();
Cube c=new Cube(randominteger);
[Link]();
try
{
[Link](1000);
}
catch(InterruptedException ex)
{
[Link](ex);
}
}
}
}
public class LAB8
{
public static void main(String args[])
{
Number n=new Number();
[Link]();
}
}
[Link] MULTI THREAD APPLICATION
OUTPUT
[Link] SYNCHRONIZED THREAD
PROGRAM
import [Link];
class count extends Thread
{
String name="";
public Integer num=0;
public void run()
{
for(int i=0;i<10;i++)
{
try
{
[Link](500);
synchronized(num)
{
num++;
[Link](name+":"+num);
}
}
catch(InterruptedException e)
{
[Link]();
}
}
}
public static void main(String args[])
{
for(int i=1;i<=10;i++)
{
count t1=new count();
[Link]="thread"+i;
[Link]();
}
}
}
[Link] SYNCHRONIZED THREAD
OUTPUT
[Link] EXCEPTION HANDLING
PROGRAM
import [Link].*;
import [Link].*;
class exception
{
public static void main(String args[])
{
try
{
int a=30,b=0;
int c=a/b;
[Link]("result="+c);
int num=[Link]("scs");
[Link](num);
int[]arr=new int[5];
arr[5]=9;
int[]array=new int[-5];
}
catch(ArithmeticException e)
{
[Link]("cant divide a number by 0");
}
catch(NumberFormatException e)
{
[Link]("NumberFormatException");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("Arrayindex is out of bounds");
}
catch(NegativeArraySizeException e)
{
[Link]("Error:attempted to create an array with a negative size");
}
}
}
[Link] EXCEPTION HANDLING
OUTPUT
[Link] DISPLAYS INFORMATION ABOUT FILE
PROGRAM
import [Link].*;
class filedemo
{
public static void analyze(String s)
{
File f=new File(s);
if([Link]())
{
[Link]([Link]()+"is a file");
[Link]([Link]()?"is readable":"is not readable");
[Link]([Link]()?"is writable":"is not writable");
[Link]("Filesize:"+[Link]()+"bytes");
[Link]("File last modified:"+[Link]());
}
}
}
public class filedetails
{
public static void main(String args[])throws IOException
{
filedemo fd=new filedemo();
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the file name:");
String s=[Link]();
[Link](s);
}
}
[Link] DISPLAYS INFORMATION ABOUT FILE
OUTPUT
[Link] CHANGE TEXT SIZE AND FONT
PROGRAM
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class Text extends Applet
{
String fonts[];
public void init()
{
setBackground([Link]);
GraphicsEnvironment GE;
GE = [Link]();
fonts = [Link]();
}
public void paint(Graphics g)
{
int size = [Link];
int pos;
for(int i=1;i<=10;i++)
{
pos = (int)([Link]()*size);
Font my_font = new Font(fonts[pos],[Link],20);
[Link](my_font);
[Link]("Sample Text",100,(i*40));
}
}
}
/*
<applet code = [Link] width = 500 height = 600>
</applet>
*/
[Link] CHANGE TEXT SIZE AND FONT
OUTPUT
[Link] MOUSE EVENT HANDLING
PROGRAM
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class MouseEventPerformer extends JFrame implements MouseListener
JLabel l1;
public MouseEventPerformer()
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setLayout(new FlowLayout([Link]));
l1=new JLabel();
Font f=new Font("verdana",[Link],20);
[Link](f);
[Link]([Link]);
add(l1);
addMouseListener(this);
setVisible(true);
public void mouseExited(MouseEvent m)
[Link]("Mouse Exited");
public void mouseEntered(MouseEvent m)
{
[Link]("Mouse Entered");
public void mouseReleased(MouseEvent m)
[Link]("mouse released");
public void mousePressed(MouseEvent m)
[Link]("mouse pressed");
public void mouseClicked(MouseEvent m)
[Link]("mouse Clicked");
public static void main(String args[])
MouseEventPerformer mep=new MouseEventPerformer();
}
[Link] MOUSE EVENT HANDLING
OUTPUT
EX NO : 14 MY CALCULATOR
AIM:
Write a Java program that works as a simple calculator. Use a grid layout to arrange
buttons for the digits and for the , -,*, % operations. Add a text field to display the result. Handle
any possible exceptions like divided by zero.
PROGRAM
import [Link].*;
import [Link].*;
public class MyCalculator extends Frame implements ActionListener
double num1,num2,result;
Label lbl1,lbl2,lbl3;
TextField tf1,tf2,tf3;
Button btn1,btn2,btn3,btn4;
char op;
MyCalculator()
lbl1=new Label("Number 1: ");
[Link](50,100,100,30);
tf1=new TextField();
[Link](160,100,100,30);
lbl2=new Label("Number 2: ");
[Link](50,170,100,30);
tf2=new TextField();
[Link](160,170,100,30);
btn1=new Button("+");
[Link](50,250,40,40);
btn2=new Button("-");
[Link](120,250,40,40);
btn3=new Button("*");
[Link](190,250,40,40);
btn4=new Button("/");
[Link](260,250,40,40);
lbl3=new Label("Result : ");
[Link](50,320,100,30);
tf3=new TextField();
[Link](160,320,100,30);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
add(lbl1); add(lbl2); add(lbl3);
add(tf1); add(tf2); add(tf3);
add(btn1); add(btn2); add(btn3); add(btn4);
setSize(400,500);
setLayout(null);
setTitle("Calculator");
setVisible(true);
public void actionPerformed(ActionEvent ae)
num1 = [Link]([Link]());
num2 = [Link]([Link]());
if([Link]() == btn1)
result = num1 + num2;
[Link]([Link](result));
if([Link]() == btn2)
result = num1 - num2;
[Link]([Link](result));
if([Link]() == btn3)
result = num1 * num2;
[Link]([Link](result));
if([Link]() == btn4)
{
result = num1 / num2;
[Link]([Link](result));
public static void main(String args[])
{
MyCalculator calc=new MyCalculator();
}
}
OUTPUT :
[Link] : 15 TRAFFIC LIGHTSIMULATOR
AIM:
Write a Java program that simulates a traffic light. The program lets the user selects one
of three lights: red, yellow, or green with radio buttons. On selecting a button, an appropriate
message with Stop, Ready, or Go should appear above the buttons in the selected color. Initially,
there is no message shown
PROGRAM
import [Link].*;
import [Link].*;
class TrafficLightSimulator extends Frame implements ItemListener
Label lbl1, lbl2;
CheckboxGroup cbg;
TrafficLightSimulator()
Font fontL = new Font("Verdana", [Link], 70);
lbl1 = new Label();
[Link](170,30,300,200);
[Link](fontL);
add(lbl1);
Font fontR = new Font("Verdana", [Link], 20);
lbl2 = new Label("Select Lights");
[Link](20,250,130,50);
[Link](fontR);
add(lbl2);
cbg = new CheckboxGroup();
Checkbox cb1 = new Checkbox("Red Light", cbg, false);
[Link](160,250,120,50);
[Link]([Link]);
[Link](fontR);
add(cb1);
[Link](this);
Checkbox cb2 = new Checkbox("Orange Light", cbg, false);
[Link](300,250,150,50);
[Link]([Link]);
[Link](fontR);
add(cb2);
[Link](this);
Checkbox cb3 = new Checkbox("Green Light", cbg, false);
[Link](460,250,140,50);
[Link]([Link]);
[Link](fontR);
add(cb3);
[Link](this);
setTitle("Traffic Light Simulator");
setSize(650,400);
setLayout(null);
setVisible(true);
// To read selected item
public void itemStateChanged(ItemEvent i)
Checkbox chk = [Link]();
String str=[Link]();
char choice=[Link](0);
switch (choice)
case 'R':[Link]("STOP");
[Link]([Link]);
break;
case 'O':[Link]("READY");
[Link]([Link]);
break;
case 'G':[Link]("GO");
[Link]([Link]);
break;
// main method
public static void main(String[] args)
new TrafficLightSimulator();
}
OUTPUT :