Java Lab Codings
Java Lab Codings
CLASS.
import java.util.*;
class Circle{
double r,area,perimeter;
r=sc.nextFloat();
area = 3.14*r*r;
perimeter = 2*3.14*r;
}
GENERATING RANDOM NUMBERS USING RANDOM CLASS
import java.util.*;
class RandomNum{
int i;
for(i=1;i<=20;i++){
System.out.println(r.nextInt(i));
}
STRING MANIPULATION (SUBSTRING REMOVAL, STRING
REPLACEMENT ETC.,)
import java.util.*;
import java.lang.*;
class StrDemo {
System.out.println("Lowercase"+s1.toLowerCase());
System.out.println("uppercase"+s1.toUpperCase());
System.out.println("Replacement"+s1.replace('e','p'));
System.out.println("Trim:"+s1.trim());
System.out.println("Equals:"+s1.equals(s2));
System.out.println("String Concatination:"+s1.concat(s2));
System.out.println("Substring:"+s1.substring(2,6));
System.out.println("String Starting:"+s1.startsWith("W"));
System.out.println("String end:"+s1.endsWith("e"));
}
DRAWING RECTANGLES, OVALS ETC USING APPLET.
import java.awt.*;
import java.applet.*;
/*
</applet>
*/
g.setFont(f);
}
IMPLEMENTING THREAD BASED APPLICATIONS & EXCEPTION
HANDLING.
Thread runner;
public RunnableThread() {
System.out.println(runner.getName());
runner.start();
System.out.println(Thread.currentThread());
threada.start();
threadb.start();
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread());
}
APPLICATION USING SYNCHRONIZATION SUCH AS THREAD BASED,
CLASS BASED AND SYNCHRONIZED STATEMENTS.
class Table{
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
Table t;
MyThread1(Table t){
this.t=t;
t.printTable(5);
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
t.printTable(100);
t1.start();
t2.start();
}
IMPLEMENTING GUI BASED APPLICATIONS USING SWING
COMPONENTS
(JLABEL, JBUTTON, JTEXTFIELD)
import javax.swing.*;
import java.awt.event.*;
JLabel lb1;
JTextField tf1,tf2,tf3;
JButton b1,b2;
JSwingCalc(){
lb1.setBounds(50,10,150,20);
tf1=new JTextField();
tf1.setBounds(50,50,150,20);
tf2=new JTextField();
tf2.setBounds(50,100,150,20);
tf3=new JTextField();
tf3.setBounds(`150,20);
tf3.setEditable(false);
b1=new JButton("+");
b1.setBounds(50,200,50,50);
b2=new JButton("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(lb1);f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
String result=String.valueOf(c);
tf3.setText(result);
new JSwingCalc();
}}
IMPLEMENTING GUI BASED APPLICATION USING LAYOUT
import javax.swing.*;
import java.awt.event.*;
JFrame f;
JMenuBar mb;
JMenu edit;
JMenuItem cut,copy,paste,selectAll;
JTextArea ta;
MenuExample()
f=new JFrame();
cut=new JMenuItem("cut");
copy=new JMenuItem("copy");
paste=new JMenuItem("paste");
selectAll=new JMenuItem("selectAll");
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
mb=new JMenuBar();
edit=new JMenu("Edit");
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(selectAll);
mb.add(edit);
ta=new JTextArea();
ta.setBounds(5,5,360,320);
f.add(mb);
f.add(ta);
f.setJMenuBar(mb);
f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);
if(e.getSource()==cut)
ta.cut();
if(e.getSource()==paste)
ta.paste();
if(e.getSource()==copy)
ta.copy();
if(e.getSource()==selectAll)
ta.selectAll();
}
public static void main(String[] args) {
new MenuExample();
}
APPLICATION USING FILE STREAMS(SEQUENTIAL FILE)
import java.io.*;
class Seq {
String s="HELLO";
try{
for(int i=0;i<s.length();i++)
f1.write(s.charAt(i));
f1.close();
catch(IOException e){
System.out.println(e);
}
APPLICATION USING FILE STREAMS(RANDOM FILE)
import java.io.*;
class Ran{
RandomAccessFile f=null;
try{
f=new RandomAccessFile("ran.ddt","rw");
f.writeChar('x');
f.writeInt(55);
f.writeDouble(3.14);
f.seek(0);
System.out.println(f.readChar());
System.out.println(f.readInt());
System.out.println(f.readDouble());
f.seek(2);
System.out.println(f.readInt());
f.seek(f.length());
f.writeBoolean(false);
f.seek(4);
System.out.println(f.readBoolean());
f.close();
catch(IOException e){
System.out.println(e);
}}}