JAVA Record KG College

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 94

INDEX

Ex.No DATE CONTENTS PAGE No SIGNATURE

1. 26.11.2020 Random Numbers

2. 26.11.2020 Rational Numbers

3. 01.12.2020 Employee Package

4. 04.12.2020 Implement Polymorphism

5. 08.12.2020 Frame With Different Shape

6. 11.12.2020 Mouse Event

7. 18.12.2020 Simple Calculator

8. 21.12.2020 Student Information

9. 21.12.2020 Animate Image

10. 31.12.2020 Convertion of Currency

11. 12.01.2021 JDBC Connectivity

12. 18.01.2021 Producer Consumer

13. 23.01.2021 Tree Viewer

14. 23.01.2021 Viewing Image

15. 29.01.2021 Panlindrome Words

16. 01.02.2021 SI Unit Coverter

17. 05.02.2021 Superclass Shape

18. 09.02.2021 Date Class

19. 12.02.2021 Fibonacci Number

20. 12.02.2021 Notepad Editor


KGiSL-IIM

RANDOM NUMBERS
Program:

import java.util.Random;
public static void main(String[] args) {
Random P=new Random();
int a[]=new int[25];
int i;
for(i=0;i<25;i++)
{ a[i]=P.nextInt(100); }
System.out.println("The Generated Random Numbers are");
for(i=0;i<25;i++)
{ System.out.print(a[i]+","); }
System.out.println("");
for(i=0;i<24;i++)
{
if(a[i]>a[i+1])
{
System.out.println(a[i]+" Is Greater than "+a[i+1]);
}
else if(a[i] < a[i + 1])
{
System.out.println(a[i]+" Is Lesser than "+a[i+1]);
}
Else
{
System.out.println(a[i]+" Is Eqal to "+a[i+1]);
}}}}

Reg. No: 2038M0066


KGiSL-IIM

Output:

The Generated Random Numbers are


74, 80, 94, 46, 55, 47, 99, 14, 72, 69, 27, 69, 77, 46, 20, 41, 75, 50, 72, 84, 50, 90, 29, 49, 78,
74 Is Lesser than 80
80 Is Lesser than 94
94 Is Greater than 46
46 Is Lesser than 55
55 Is Greater than 47
47 Is Lesser than 99
99 Is Greater than 14
14 Is Lesser than 72
72 Is Greater than 69
27 Is Lesser than 69
69 Is Lesser than 77
77 Is Greater than 46
46 Is Greater than 20
20 Is Lesser than 41
41 Is Lesser than 75
75 Is Greater than 50
50 Is Lesser than 72
72 Is Lesser than 84
84 Is Greater than 50
50 Is Lesser than 90
90 Is Greater than 29
29 Is Lesser than 49
49 Is Lesser than 78

Reg. No: 2038M0066


KGiSL-IIM

RATIONAL NUMBERS
Program:

import java.util.Scanner;
public class Prog_2 {
public static void main(String[] args)
{ int num1,num2;
Scanner input=new Scanner(System.in);
System.out.println("Enter the first value:");
num1=input.nextInt();
System.out.println("Enter the second value:");
num2=input.nextInt();
int num3=findGCD(num1,num2);
System.out.println("Common Factoris:"+num3);
num2=num2/num3;
num1=num1/num3;
System.out.println("Result is:"+num1+"/"+num2);
}
private static int findGCD(int n1,int n2)
{
if(n2==0)
{ returnn1; }
returnfindGCD(n2,n1%n2);
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Enter the first value:


90
Enter the second value:
42
Common Factor is:6
Result is:15/7

Reg. No: 2038M0066


KGiSL-IIM

EMPLOYEE INFORMATION USING PACKAGE


Program:

import java.util.Scanner; //Package


public class HEmpDet {
Scanner s=new Scanner(System.in);
int empno;
String ename;
int Bpay;
int Allowance;
int dedections;
int Gp;
int Netpay;

HEmpDet()
{
System.out.println(" ");
System.out.println(" Employee Program");
System.out.println(" ");
}

void get( )
{
{
System.out.println("Enter The EmpNo :");
this.empno=s.nextInt();
System.out.println("Enter The EmpName :");
this.ename=s.next();
System.out.println("Enter The Emp Bpay :");
Bpay=s.nextInt();
System.out.println("Enter The Emp Allowance:");
Allowance=s.nextInt();
System.out.println("Enter The EmpDedections:");
dedections=s.nextInt();
Gp=Bpay-dedections;
Netpay=Gp+Allowance;
System.out.println(" ");
}
}
String get(int a )
{
String fin; fin=Integer.toString(empno)+"/"+ename+"/"+Integer.toString(Bpay)
+"/"+Integer.toSt ring(Allowance)+"/"+Integer.toString(dedections)+"/"+Integer.toString(Gp)
+"/"+Inte ger.toString(Netpay);
return fin;

Reg. No: 2038M0066


KGiSL-IIM

}
import javaprog.HEmpDet; //Package Declaration in FORM
public class Prog_3 extends javax.swing.JFrame {
String s1;
public Prog_3() {
HEmpDet s=new HEmpDet();
s.get();
s1=s.get(1);
String s2[]=s1.split("/");
initComponents();
jLabel1.setText(s2[0]); jLabel2.setText(s2[1]);
jLabel3.setText(s2[2]); jLabel13.setText(s2[3]);
jLabel14.setText(s2[4]); jLabel15.setText(s2[5]);
jLabel16.setText(s2[6]);
}

private void initComponents() // Designer Code {


jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
}
public static void main(String args[])
{ public void run() {
new Prog_3().setVisible(true);
}
});
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Employee Program

Enter The EmpNo :


1
Enter The EmpName :
GAYU
Enter The Emp Bpay :
15000
Enter The Emp Allowance :
2500
Enter The Emp Dedections:
3000

Reg. No: 2038M0066


KGiSL-IIM

VEHICLE CLASS
Program:

Import java,io,*; //Main class


public class Prog_4 {
public static void main(String[] args)
{ HVehical v1 = new bike();
HVehical v2=new Car(),v3=new Bus();
System.out.println("Bike have " + v1);
System.out.println("Bike have " + v2);
System.out.println("Bike have " + v3);
}}

Import java.io.*; //Veicle class


public class HVehical { int tires,seats,doors; }
class bike extends HVehical
{
bike()
{
tires=2; seats=2; doors=0;
}
public String toString()
{
return("tires="+tires+"seats="+seats+"doors="+doors);
}}
class Car extends HVehical
{
Car()
{
tires=4; seats=5; doors=4;
}
public String toString()
{
return ("tires=" + tires + ", seats=" + seats + ", doors=" + doors);
}}
class Bus extends HVehical
{
Bus()
{
tires=6; seats=64; doors=3;
}
public String toString()
{
return ("tires=" + tires + ", seats=" + seats + ", doors=" + doors);
}}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Bike have tires=2seats=2doors=0


Bike have tires=4, seats=5, doors=4
Bike have tires=6, seats=64, doors=3

Reg. No: 2038M0066


KGiSL-IIM

DRAWING SHAPES USING MENU


Program:

package user_frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Scanner;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class User_frame {
public static void main(String[] args)
{ Scanner sc=new Scanner(System.in);
int x,y,w,h;
System.out.println("Enter the X position: ");
x=sc.nextInt();
System.out.println("Enter the Y position: ");
y=sc.nextInt();
System.out.println("Enter the Width: ");
w=sc.nextInt();
System.out.println("Enter the Height:");
h=sc.nextInt();
draw_frame(x,y,h,w);
}

public static void draw_frame(int x,int y,int w,int h)


{
JFrame j=new JFrame("User Frame"); j.setBounds(x, y,
w, h);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.getContentPane().add(new MyCanvas());
j.setVisible(true);
}
}
class MyCanvas extends JComponent
{ @Override
public void paint(Graphics g)
{ g.drawRect (10, 10, 200,
200);
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Enter the X position:


12
Enter the Y position:
23
Enter the Width:
34
Enter the Height:
56

Reg. No: 2038M0066


KGiSL-IIM

MOUSE EVENTS
Program:

import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*
public class Prog_8 extends Applet implements MouseListener,MouseMotionListener
{
String txt=""; int
x=10,y=30;
public void init()
{ addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
txt="Mouse Clicked";
setForeground(Color.pink);
repaint();
}
public void mouseEntered(MouseEvent me)
{
txt="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
txt="Mouse Exited";
setForeground(Color.DARK_GRAY);
repaint();
}
public void mousePressed(MouseEvent me)
{
txt="Mouse Pressed";
setForeground(Color.BLUE);
repaint();
}
public void mouseMoved(MouseEvent me)
{
txt="Mouse Moved";
setForeground(Color.RED);
repaint();
}
public void mouseDragged(MouseEvent me)

Reg. No: 2038M0066


KGiSL-IIM

{
txt="Mouse Dragged";
setForeground(Color.GREEN);
repaint();
}
public void mouseReleased(MouseEvent me)
{
txt="Mouse Released";
setForeground(Color.MAGENTA);
repaint();
}
public void paint(Graphics g)
{ g.drawString(txt,30,40);
showStatus("Mouse events Handling");}}

Reg. No: 2038M0066


KGiSL-IIM

SIMPLE CALCULATOR
Program:
package calculator1;
import java.awt.*;
import java.awt.event.*;
public class Calculator1 implements ActionListener{
int c,n;
String s1,s2,s3,s4,s5;
Frame f;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
Panel p;
TextField tf;
GridLayout g;
Calculator1() {
f = new Frame("My calculator");
p = new Panel();
f.setLayout(new FlowLayout());
b1 = new Button("0");
b1.addActionListener(this);
b2 = new Button("1");
b2.addActionListener(this);
b3 = new Button("2");
b3.addActionListener(this);
b4 = new Button("3");
b4.addActionListener(this);
b5 = new Button("4");
b5.addActionListener(this);
b6 = new Button("5");
b6.addActionListener(this);
b7 = new Button("6");
b7.addActionListener(this);
b8 = new Button("7");
b8.addActionListener(this);
b9 = new Button("8");
b9.addActionListener(this);
b10 = new Button("9");
b10.addActionListener(this);
b11 = new Button("+");
b11.addActionListener(this);
b12 = new Button("-");
b12.addActionListener(this);
b13 = new Button("*");
b13.addActionListener(this);
b14 = new Button("/");
b14.addActionListener(this);
b15 = new Button("%");
b15.addActionListener(this);
b16 = new Button("=");
b16.addActionListener(this);

Reg. No: 2038M0066


KGiSL-IIM
b17 = new Button("C");
b17.addActionListener(this);
tf = new TextField(20);
f.add(tf);
g = new GridLayout(4,4,10,20);
p.setLayout(g);
p.add(b1);p.add(b2);p.add(b3);p.add(b4);p.add(b5);p.add(b6);p.add(b7);p.add(b8);p.add(b9);
p.add(b10);p.add(b11);p.add(b12);p.add(b13);p.add(b14);p.add(b15);p.add(b16);p.add(b17);
f.add(p);
f.setSize(300,300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
s3 = tf.getText();
s4 = "0";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b2) {
s3 = tf.getText();
s4 = "1";
s5 = s3+s4;
tf.setText(s5);}
if(e.getSource()==b3){
s3 = tf.getText();
s4 = "2";
s5 = s3+s4;
tf.setText(s5);}
if(e.getSource()==b4){
s3 = tf.getText();
s4 = "3";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b5){
s3 = tf.getText();
s4 = "4";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b6){
s3 = tf.getText();
s4 = "5";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b7) {
s3 = tf.getText();
s4 = "6";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b8) {

Reg. No: 2038M0066


KGiSL-IIM

s3 = tf.getText();
s4 = "7";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b9) {
s3 = tf.getText();
s4 = "8";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b10){
s3 = tf.getText();
s4 = "9";
s5 = s3+s4;
tf.setText(s5); }
if(e.getSource()==b11) {
s1 = tf.getText();
tf.setText("");
c=1; }
if(e.getSource()==b12) {
s1 = tf.getText();
tf.setText("");
c=2;}
if(e.getSource()==b13) {
s1 = tf.getText();
tf.setText("");
c=3;}
if(e.getSource()==b14) {
s1 = tf.getText();
tf.setText("");
c=4; }
if(e.getSource()==b15) {
s1 = tf.getText();
tf.setText("");
c=5; }
if(e.getSource()==b16) {
s2 = tf.getText();
if(c==1) {
n = Integer.parseInt(s1)+Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
else
if(c==2){
n = Integer.parseInt(s1)-Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
else
if(c==3) {
n = Integer.parseInt(s1)*Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
if(c==4){
try {
if(c==2){
n = Integer.parseInt(s1)-Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
else
if(c==3) {
n = Integer.parseInt(s1)*Integer.parseInt(s2);
tf.setText(String.valueOf(n)); }
if(c==4){
try {
int p=Integer.parseInt(s2);
if(p!=0){
n = Integer.parseInt(s1)/Integer.parseInt(s2);
tf.setText(String.valueOf(n));}
else
tf.setText("infinite");
}
catch(Exception i){}
}
if(c==5)
{
n = Integer.parseInt(s1)%Integer.parseInt(s2);
tf.setText(String.valueOf(n));
}
}
if(e.getSource()==b17)
{
tf.setText("");
}
}
public static void main(String[] abc)
{
Calculator1 v = new Calculator1();
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Reg. No: 2038M0066


KGiSL-IIM

STUDENT INFORMATION - TEXT FILE


Program:

import java.awt.*;
import java.io.*;
import javax.swing.*;
public class Progr_12 extends javax.swing.JFrame {
public Progr_12() {
initComponents();
}
private void initComponents()
{ jPanel1 = new javax.swing.JPanel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jTextField5 = new javax.swing.JTextField();
jTextField6 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String filename= "D://StudentTxtF.txt";
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)));
out.println("Roll No: "+jTextField1.getText());
out.println("Name : "+jTextField2.getText());
out.println("DOB : "+jTextField3.getText());
out.println("Father : "+jTextField4.getText());
out.println("Dept : "+jTextField5.getText());
out.println("Phone : "+jTextField6.getText());
out.println(" ");
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
JOptionPane.showMessageDialog(null, "Success"); out.close();
} catch (IOException e)
{ JOptionPane.showMessageDialog(null, e);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{ System.exit(0);
}

public static void main(String args[])


{ public void run() {
new Progr_12().setVisible(true);
}
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Text File
ANIMATE IMAGES
Program:

import java.awt.*;
public class Progr_15 extends
Frame{ xPixel = 20;
static int yPixel = 20;
Image myImage;
public Progr_15()
{ try{
myImage = Toolkit.getDefaultToolkit().getImage("D:\\pictures\\img1.jpg");
}
catch(Exception e){}
setSize(800,600);
setVisible(true);
moveImage();
}
public void paint(Graphics g){
boolean b = g.drawImage(myImage, xPixel, yPixel, this);
}
void moveImage(){
for ( int i=0 ; i< 50000 ; i++){
//System.out.println("next set of Pixels " + xPixel);
for ( int j=1 ; j<5000000 ; j++)
{}
xPixel+=1;
yPixel +=1;
repaint();
}
}
public static void main(String[] args) {
// TODO code application logic here
Progr_15 me = new Progr_15();
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Reg. No: 2038M0066


KGiSL-IIM

CURRENCY CONVERSION
Program:

package currencyconverter;
import java.util.Scanner;
public class CurrencyConverter
{ public static final double
USD=65.00; public static final double
INR=1.53; public static void
main(String[] args) { double
ammount,usdResult,inrResult; int a=0;
Scanner sc=new Scanner(System.in);
do {
System.out.println("Enter the choice:");
System.out.println("1. USD to INR Conversion\n2. INR to USD Conversion\n3. Exit");
a=sc.nextInt();
switch(a)
{
case 1:
System.out.println("Enter the Ammount:");
ammount=sc.nextDouble();
System.out.println("Indian Value is:"+usdToInr(ammount)+"INR");
break;
case 2:
System.out.println("Enter the Ammount:");
ammount=sc.nextDouble();
System.out.println("Indian Value is:"+inrToUsd(ammount)+"INR");
break;
case 3:
break;
default:
System.out.println("Invalid Choice");
break;
}
}
while (a!=3);
}
private static double usdToInr(double ammount)
{ return (ammount*USD);
}
private static double inrToUsd(double ammount)
{ return ((ammount/100)*INR);
}
1. }

Reg. No: 2038M0066


KGiSL-IIM

Output:

Enter the choice:


1. USD to INRConversion
2. INR to USDConversion
3. Exit
1
Enter the Ammount:
23
Indian Value is:1495.0INR
Enter the choice:
1. USD to INRConversion
2. INR to USDConversion
3. Exit
2
Enter the Ammount:
34
Indian Value is:0.5202INR
Enter the choice:
2. USD to INRConversion
3. INR to USDConversion
Exit

Reg. No: 2038M0066


KGiSL-IIM

JDBC CONNECTION
Program:

import java.awt.Color; import


java.sql.Connection; import
javax.swing.JOptionPane;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;
public class Progr_20 extends javax.swing.JFrame
{ Connection conn=null;
OraclePreparedStatement pst=null;
OracleResultSet rs=null;
public Progr_20()
{ initComponents(); }
private void initComponents() { jButton1 =
new javax.swing.JButton(); jTextField1 =
new javax.swing.JTextField(); jTextField2 =
new javax.swing.JTextField(); jTextField3 =
new javax.swing.JTextField(); jTextField4 =
new javax.swing.JTextField(); jTextField5 =
new javax.swing.JTextField(); jLabel1 =
new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{ conn=HDB_Mod.conDB();
try{
String sql="insert into jstudent values(?,?,?,?,?)";
pst=(OraclePreparedStatement) conn.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.setString(2,jTextField2.getText());
pst.setString(3,jTextField3.getText());
pst.setString(4,jTextField4.getText());
pst.setString(5,jTextField5.getText());
rs=(OracleResultSet) pst.executeQuery();
JOptionPane.showMessageDialog(null, "Success");
}
catch(Exception e)
{ JOptionPane.showMessageDialog(null, e);
}}
public static void main(String args[])
{ public void run() {
new Progr_20().setVisible(true);
}}}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Reg. No: 2038M0066


KGiSL-IIM

PRODUCER CONSUMER
Program:

package thread_2; //main class


public class Thread_2 {
public static void main(String[] args) {
printnua;
printnub;
a=new printnu("Consumer# got");
b=new printnu("Producer# put");
a.start();
b.start();
}
}

package thread_2; //sub class


public class printnu extends Thread{
Stringname;
public printnu(String thr)
{ name=thr;
}
@Override
public void run()
{ int i;
for ( i = 0; i < 11; i++)
{ System.out.println(name+": "+i);
try{
Thread.sleep(5000);
}catch(InterruptedException e){}
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Consumer #1 got: 0
Producer #1 put: 0
Consumer #1 got: 1
Producer #1 put:1
Producer #1 put:2
Consumer #1 got: 2
Producer #1 put: 3
Consumer #1 got: 3
Producer #1 put: 4
Consumer #1 got: 4
Producer #1 put: 5
Consumer #1 got: 5
Producer #1 put: 6
Consumer #1 got: 6
Producer #1 put: 7
Consumer #1 got: 7
Producer #1 put: 8
Consumer #1 got: 8
Producer #1 put: 9
Consumer #1 got: 9

Reg. No: 2038M0066


KGiSL-IIM

TREE VIEWER
Program:

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class Progr_13 {


public static void main(String[] args) {
JFrame frame = new JFrame("Create tree viewer");
DefaultMutableTreeNode parent = newDefaultMutableTreeNode("color",true);
DefaultMutableTreeNode black = new DefaultMutableTreeNode("black");
DefaultMutableTreeNode blue = new DefaultMutableTreeNode("blue");
DefaultMutableTreeNode nBlue = new DefaultMutableTreeNode("nBlue");
DefaultMutableTreeNode dBlue = new DefaultMutableTreeNode("dBlue");
DefaultMutableTreeNode green = new DefaultMutableTreeNode("green");
DefaultMutableTreeNode nGreen = new DefaultMutableTreeNode("nGreen");
DefaultMutableTreeNode dGreen = new DefaultMutableTreeNode("dGreen");
DefaultMutableTreeNode white = new DefaultMutableTreeNode("white");
parent.add(black);
parent.add(blue);
blue.add(nBlue);
blue.add(dBlue);
parent.add(green);
green.add(nGreen);
green.add(dGreen);
parent.add(white);
JTree tree = new JTree(parent); frame.add(tree);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setSize(200,200);
frame.setVisible(true);
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Reg. No: 2038M0066


KGiSL-IIM

IMAGE VIEWER
Program:

import javax.swing.*;
public class Progr_14 extends javax.swing.JFrame
{ public Progr_14() {
initComponents();
}
private void initComponents()
{ jButton1 = new
javax.swing.JButton(); jLabel2 = new
javax.swing.JLabel(); jList1 = new
javax.swing.JList();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{ try{
if (jList1.getSelectedIndex() != -1)
{ String loc;
loc=jList1.getSelectedValue().toString();
ImageIcon icon1 = new ImageIcon("D:\\pictures\\"+loc+".jpg");
jLabel2.setIcon(icon1); }
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
public static void main(String args[])
{ public void run() {
new Progr_14().setVisible(true);
}
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Reg. No: 2038M0066


KGiSL-IIM

PALINDROME
Program:

import java.io.*;
import java.util.*;

public class Progr_18 {


public static void main(String[] args) throws IOException
{ intcnt=0;
int val=0;
try {
FileInputStream fstream = new FileInputStream("D:\\links.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = null;
while ((strLine = br.readLine()) != null)
{
String m[]=strLine.split(" ");

for(String i:m)
{
String reverse = new StringBuffer(m[val]).reverse().toString();
if(i.equals(reverse)){
System.out.println(i);
cnt+=1;
}
val+=1;
}
}
System.out.println("No of Words:"+cnt);
}
catch (FileNotFoundException ex)
{ Logger.getLogger(Progr_18.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

amma appa
malayalam
No of Words:3

Reg. No: 2038M0066


KGiSL-IIM

SI UNITS
Program:

package unitconversion; //main class


import java.util.Scanner;
public class UnitConversion {
public static void main(String[] args)
{ Scanner in = new
Scanner(System.in);
System.out.println("Convert from:");
String fromUnit = in.nextLine();
System.out.println("Convert to: ");
String toUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit,toUnit);
//UnitConverter to = new UnitConverter(toUnit);
System.out.println("Value:");
double val = in.nextDouble();
double meters =from.toMeters(val);
double converted = from.fromMeters(meters);
System.out.println(val + " " + fromUnit + "="+converted +" "+ toUnit);
}
}

package unitconversion; //sub class


class UnitConverter {
static double INCHES = 0.0254001;
static double FEET = 0.3048;
static double MILES = 1609.35;
static double MILLIMETERS = 0.001;
static double CENTIMETERS = 0.01;
static double METERS = 1;
static double KILOMETERS = 1000;

Reg. No: 2038M0066


KGiSL-IIM

private double val ,meters ,converted;


String afromUnit,atoUnit;
public UnitConverter(String fromUnit,String toUnit)
{
this.afromUnit=fromUnit;
this.atoUnit=toUnit;
}
public double toMeters(double val)
{
if(afromUnit.equals("in"))
{
meters = (val*INCHES);
}
else if(afromUnit.equals("ft"))
{
meters = (val*FEET);
}
else if(afromUnit.equals("mi"))
{
meters = (val*MILES);
}
elseif(afromUnit.equals("mm"))
{
meters =(val*MILLIMETERS);
}
else if(afromUnit.equals("cm"))
{
meters = (val*CENTIMETERS);
}
else if(afromUnit.equals("m"))

Reg. No: 2038M0066


KGiSL-IIM

{
meters = (val*METERS);
}
else
{
meters = (val*KILOMETERS);
}
return meters;
}
public double fromMeters(double meters)
{
if(atoUnit.equals("in"))
{
converted = Math.round(meters*39.369923740457715);
}
else if(atoUnit.equals("ft"))
{
converted = Math.round(meters*3.280839895013123);
}
else if(atoUnit.equals("mi"))
{
converted = Math.round(meters*0.0006213688756330196);
}
else if(atoUnit.equals("mm"))
{
converted = Math.round(meters*1000);
}
else if(atoUnit.equals("cm"))
{
converted = Math.round(meters*100);
else if(atoUnit.equals("m"))
{
converted = Math.round(meters*1);;
}
else
{
converted = Math.round(meters*0.001);
}
return converted;
}

}
Reg. No: 2038M0066
KGiSL-IIM

Output:

Convert from:
in
Convert to:
ft
Value:
56
56.0 in=5.0 ft

Reg. No: 2038M0066


KGiSL-IIM

SHAPES
Program:

package fb;
import java.awt.*;
importjavax.swing.*;
public class fb extends
javax.swing.JFrame{ static fb FS=newfb();
String shp,clr;
public fb()
{ initComponents
();
}
@SuppressWarnings("unchecked")
private void initComponents() {
mnuBr_Shps = new javax.swing.JMenuBar();
mnu_Shp = new javax.swing.JMenu();
mnu_ShpRect = new javax.swing.JMenuItem();
mnu_ShpOvl = new javax.swing.JMenuItem();
mnu_ShpArc = new javax.swing.JMenuItem();
mnu_FldShp = new javax.swing.JMenu();
mnu_FldShpRect = new javax.swing.JMenuItem();
mnu_FldShpOvl = new javax.swing.JMenuItem();
mnu_FldShpArc = new javax.swing.JMenuItem();
mnu_Clr = new javax.swing.JMenu();
mnu_ClrRd = new javax.swing.JMenuItem();
mnu_ClrGrn = new javax.swing.JMenuItem();
mnu_ClrBlu = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
mnu_Shp.setText("Shapes"); mnu_ShpRect.setText("Rectangle");
mnu_ShpRect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sbmnu_ShpActionPerformed(evt);
}
});
mnu_Shp.add(mnu_ShpRect);
mnu_ShpOvl.setText("Oval");
mnu_ShpOvl.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ sbmnu_ShpActionPerformed(evt);
}
});
mnu_Shp.add(mnu_ShpOvl);
mnu_ShpArc.setText("Arc");
mnu_ShpArc.addActionListener(new java.awt.event.ActionListener() { public
voidactionPerformed(java.awt.event.ActionEvent evt)
{
sbmnu_ShpActionPerformed(evt);
}
}); mnu_Shp.add(mnu_ShpArc);
mnuBr_Shps.add(mnu_Shp);
mnu_FldShp.setText("Filled Shapes");
mnu_FldShpRect.setText("Filled Rectangle");
mnu_FldShpRect.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ sbmnu_ShpActionPerformed(evt);
}
}); mnu_FldShp.add(mnu_FldShpRect);
mnu_FldShpOvl.setText("Filled Oval");
mnu_FldShpOvl.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ sbmnu_ShpActionPerformed(evt);
}
}); mnu_FldShp.add(mnu_FldShpOvl);
mnu_FldShpArc.setText("Filled Arc");
mnu_FldShpArc.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ sbmnu_ShpActionPerformed(evt);
}
});
mnu_FldShp.add(mnu_FldShpArc);
mnuBr_Shps.add(mnu_FldShp);
mnu_Clr.setText("Colors");
mnu_ClrRd.setText("Red");
mnu_ClrRd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{ mnu_ClrActionPerformed(evt);
}
}); mnu_Clr.add(mnu_ClrRd);
mnu_ClrGrn.setText("Green");
mnu_ClrGrn.setToolTipText("");
mnu_ClrGrn.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ mnu_ClrActionPerformed(evt);
}
});
mnu_Clr.add(mnu_ClrGrn);
mnu_ClrBlu.setText("Blue");
mnu_ClrBlu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{ mnu_ClrActionPerformed(evt);
}
});
mnu_Clr.add(mnu_ClrBlu);
mnuBr_Shps.add(mnu_Clr);
setJMenuBar(mnuBr_Shps);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayo
ut.Alignment.LEADING)
.addGap(0, 279, Short.MAX_VALUE)
);
pack();
}
private void sbmnu_ShpActionPerformed(java.awt.event.ActionEvent evt)
{ Graphics g =getGraphics();
g.clearRect(0,50,getWidth(), getHeight());
if(clr=="red")
{
g.setColor(Color.red);
}
else if(clr=="green")
{ g.setColor(Color.green);
}
else if(clr=="blue")
{ g.setColor(Color.blue);
}
if(evt.getSource()==mnu_ShpRect)
{
g.drawRect(100, 120, 100, 100);
}
else if(evt.getSource()==mnu_ShpOvl)
{ g.drawOval(100,120,100,100);
}
else if(evt.getSource()==mnu_ShpArc)
{ g.drawArc(100, 120, 100, 100, 90,
120);
}

else if(evt.getSource()==mnu_FldShpRect)
{ g.fillRect(100,120,100,100);
}
else if(evt.getSource()==mnu_FldShpOvl){
g.fillOval(100,120,100,100);
}
else if(evt.getSource()==mnu_FldShpArc)
{ g.fillArc(100, 120, 100, 100, 90, 120);
}
}
private void mnu_ClrActionPerformed(java.awt.event.ActionEvent evt)
{ if(evt.getSource()==mnu_ClrRd)
{
clr="red";
}
else if(evt.getSource()==mnu_ClrGrn)
{ clr="green";
}
else if(evt.getSource()==mnu_ClrBlu)
{ clr="blue";
}
}
public static void main(String args[])
{ try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels())
{ if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrmShp.class.getName()).log(java.util.logging.Level.SE
VERE, null,ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrmShp.class.getName()).log(java.util.logging.Level.SE
VERE, null,ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrmShp.class.getName()).log(java.util.logging.Level.SE
VERE, null,ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrmShp.class.getName()).log(java.util.logging.Level.SE
VERE, null,ex);
}
SwingUtilities.invokeLater(() -> { FS.setVisible(true);
FS.setBounds(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2]),Inte
ger.parseInt(args[3]));
});
}

Reg. No: 2038M0066


KGiSL-IIM

private javax.swing.JMenuBar mnuBr_Shps;


private javax.swing.JMenu mnu_Clr;
private javax.swing.JMenuItem mnu_ClrBlu;
private javax.swing.JMenuItem mnu_ClrGrn;
private javax.swing.JMenuItem mnu_ClrRd;
private javax.swing.JMenu mnu_FldShp;
private javax.swing.JMenuItem mnu_FldShpArc;
private javax.swing.JMenuItem mnu_FldShpOvl;
private javax.swing.JMenuItem mnu_FldShpRect;
private javax.swing.JMenu mnu_Shp;
private javax.swing.JMenuItem mnu_ShpArc;
private javax.swing.JMenuItem mnu_ShpOvl;
private javax.swing.JMenuItem mnu_ShpRect;
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Reg. No: 2038M0066


KGiSL-IIM

DATE CLASS
Program:

public class Prog_5 extends javax.swing.JFrame { public Prog_5() {


initComponents();
}
private void initComponents() { jLabel1 = new javax.swing.JLabel(); jButton1 = new
javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { Date d1 = new Date();
jLabel3.setText(""+d1);
}
public static void main(String args[]) { public void run() {
new Prog_5().setVisible(true);
}
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Reg. No: 2038M0066


KGiSL-IIM

PRIME AND FIBONACCI NUMBER


Program:

import java.io.PipedReader;
import java.io.PipedWriter;

class Progr_17 extends Thread


{
private PipedWriter out=new PipedWriter();
public PipedWriter getPipedWriter()
{
return out;
}
public void run()
{
Thread t=Thread.currentThread();
t.setName("A1");
System.out.println(t.getName()+" Thread started...");
int fibo=0,fibo1=0,fibo2=1;
while(true)
{ try
{
fibo=fibo1+fibo2;
if(fibo>100000)
{
out.close();
break;
}
out.write(fibo);
sleep(1000);
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
fibo1=fibo2;
fibo2=fibo;
}
System.out.println(t.getName()+" Thread exiting...");
}
}

class Prime extends Thread


{
private PipedWriter out1=new PipedWriter(); public PipedWriter getPipedWriter()

Reg. No: 2038M0066


KGiSL-IIM

{
return out1;
}
public void run()
{
Thread t=Thread.currentThread();
t.setName("Prime");
System.out.println(t.getName() +" Thread Started...");
int prime=1;
while(true)
{
try
{
if(prime>100000)
{
out1.close();
break;
}
if(isPrime(prime))
out1.write(prime);
prime++; sleep(0);
}
catch(Exception e)
{
System.out.println(t.getName()+" Thread exiting...");
System.exit(0);
}
}
}
public boolean isPrime(int n)
{
int m=(int)Math.round(Math.sqrt(n));
if(n==1||n==2)
returntrue;
for(int i=2;i<=m;i++) if(n
%i==0)
return false;
returntrue;
}
}

class MultiThreadDemo
{
public static void main(String[] args)throws Exception
{

Reg. No: 2038M0066


KGiSL-IIM

Thread t=Thread.currentThread();
t.setName("Main"); System.out.println(t.getName()
+" ThreadStarted..."); Progr_17
fibObj=newProgr_17();
Prime primeObj=new Prime();
PipedReader pr=new PipedReader(fibObj.getPipedWriter());
PipedReader pr1=new PipedReader(primeObj.getPipedWriter());
fibObj.start();
primeObj.start();
int fib=pr.read(),prm=pr1.read();
System.out.println("The numbers common to PRIME and FIBONACCI:");
while((fib!=-1)&&(prm!=-1))
{
while(prm<=fib)
{ if(fib==prm)
System.out.println(prm);
prm=pr1.read();
}
fib=pr.read();
}
System.out.println(t.getName()+ " Thread exiting...");
}
}

Reg. No: 2038M0066


KGiSL-IIM

Output:

Main Thread Started...


A1 Thread started...
Prime Thread Started...
The numbers common to PRIME and FIBONACCI:
1
2
3
5
13
89
233
1597
28657
A1 Thread exiting... Main Thread exiting... Prime Thread exiting...

Reg. No: 2038M0066


KGiSL-IIM

NOTEPAD EDITOR
Program:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyNotepade extends Frame implements ActionListener
{
private TextArea txta_show;
private Menu file;
private MenuItem New,Open,Save,Exit;
private MenuBar mb=new MenuBar();
public MyNotepade()
{
file=new Menu("File");
New=new MenuItem("New");
Open=new MenuItem("Open");
Save=new MenuItem("Save");
Exit=new MenuItem("Exit");
file.add(New);
file.add(Open);
file.add(Save);
file.add(Exit);
mb.add(file);
New.addActionListener(this);
Open.addActionListener(this);
Save.addActionListener(this);
Exit.addActionListener(this);
setTitle("Notpad");
setSize(400,400);
setLocation(100,100);
setMenuBar(mb);
txta_show=new TextArea();
add(txta_show);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
} });
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==New)
{
txta_show.setText(" ");}
else if(ae.getSource()==Open)
{
try{
FileDialog fd=new FileDialog(this,"Open File",FileDialog.LOAD);
fd.setVisible(true);
String dir=fd.getDirectory();
String fname=fd.getFile();
FileInputStream fis=new FileInputStream(dir+fname);
DataInputStream dis=new DataInputStream(fis);
String str=" ",msg=" "; while((str=dis.readLine())!
=null)
{
msg=msg+str;
msg+="\n";
}
txta_show.setText(msg);
dis.close();
}
catch(Exception e){}
}
else if(ae.getSource()==Save)
{
try
{
FileDialog fd=new FileDialog(this,"Save File",FileDialog.SAVE);
fd.setVisible(true);
String txt=txta_show.getText();
String dir=fd.getDirectory();
String fname=fd.getFile();
FileOutputStream fos=new FileOutputStream(dir+fname);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeBytes(txt);
dos.close();
}
catch(Exception e){}
}
else if(ae.getSource()==Exit)
{
System.exit(0);
}}}
class Notepad
{
public static void main(String []args)
{
new MyNotepade().setVisible(true);
}}
Output:

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066


KGiSL-IIM

Reg. No: 2038M0066

You might also like