FINDING AREA AND PERIMETER OF A CIRCLE USING SCANNER
CLASS.
import java.util.*;
class Circle{
public static void main(String args[]){
double r,area,perimeter;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Radius of Circle...:");
r=sc.nextFloat();
area = 3.14*r*r;
perimeter = 2*3.14*r;
System.out.println("Area of Circle is :"+area);
System.out.println("Perimeter of Circle is :"+perimeter);
}
GENERATING RANDOM NUMBERS USING RANDOM CLASS
import java.util.*;
class RandomNum{
public static void main(String args[]){
int i;
Random r=new Random();
System.out.println("Random Numbers From 1 to 20...:");
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 {
public static void main(String args[]) {
String s1="Welcome", s2=”Friends”;
System.out.println("\t\t STRING MANIPULATION");
System.out.println("The Given String1 is:"+s1);
System.out.println("The Given String2 is:"+s2);
System.out.println("Lowercase"+s1.toLowerCase());
System.out.println("uppercase"+s1.toUpperCase());
System.out.println("Replacement"+s1.replace('e','p'));
System.out.println("The Length of string is:"+s1.length());
System.out.println("Trim:"+s1.trim());
System.out.println("The character position:"+s1.charAt(3));
System.out.println("Equals:"+s1.equals(s2));
System.out.println("The Index of character:"+s1.indexOf('e'));
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"));
System.out.println("Last Index of:"+s1.lastIndexOf('e'));
}
DRAWING RECTANGLES, OVALS ETC USING APPLET.
import java.awt.*;
import java.applet.*;
/*
<applet code="Smiley.java" width="500" height="500">
</applet>
*/
public class Smiley extends Applet{
public void paint(Graphics g){
Font f = new Font("Helvetica", Font.BOLD,20);
g.setFont(f);
g.drawString("Keep Smiling!!!", 50, 30);
g.drawOval(60, 60, 200, 200);
g.fillOval(90, 120, 50, 20);
g.fillOval(190, 120, 50, 20);
g.drawLine(165, 125, 165, 175);
g.drawArc(110, 130, 95, 95, 0, -180);
}
IMPLEMENTING THREAD BASED APPLICATIONS & EXCEPTION
HANDLING.
class RunnableThread implements Runnable {
Thread runner;
public RunnableThread() {
public RunnableThread(String threadName) {
runner = new Thread(this, threadName);
System.out.println(runner.getName());
runner.start();
public void run() {
System.out.println(Thread.currentThread());
public class RunnableExample {
public static void main(String args[]) {
Thread threada = new Thread(new RunnableThread(), "threada");
Thread threadb = new Thread(new RunnableThread(), "threadb");
RunnableThread thread3 = new RunnableThread("thread3");
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{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
public void run(){
t.printTable(5);
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
public void run(){
t.printTable(100);
public class TestSynchronization{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
IMPLEMENTING GUI BASED APPLICATIONS USING SWING
COMPONENTS
(JLABEL, JBUTTON, JTEXTFIELD)
import javax.swing.*;
import java.awt.event.*;
public class JSwingCalc implements ActionListener{
JLabel lb1;
JTextField tf1,tf2,tf3;
JButton b1,b2;
JSwingCalc(){
JFrame f= new JFrame();
lb1=new JLabel("Java Swing Calc Add/Sub");
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);
public void actionPerformed(ActionEvent e) {
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);
public static void main(String[] args) {
new JSwingCalc();
}}
IMPLEMENTING GUI BASED APPLICATION USING LAYOUT
MANAGERS AND MENUS.
import javax.swing.*;
import java.awt.event.*;
public class MenuExample implements ActionListener{
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);
public void actionPerformed(ActionEvent e) {
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 {
public static void main(String args[])throws Exception{
String s="HELLO";
try{
FileWriter f1=new FileWriter("sam.txt");
for(int i=0;i<s.length();i++)
f1.write(s.charAt(i));
System.out.println("content is updated in sam.txt file");
f1.close();
catch(IOException e){
System.out.println(e);
}
APPLICATION USING FILE STREAMS(RANDOM FILE)
import java.io.*;
class Ran{
public static void main(String args[]){
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);
}}}