Rajesh Pathak. Java File
Rajesh Pathak. Java File
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 1
INDEX
PROGRAM 1 - Enter 2 Numbers & display sum of both. 4
PROGRAM 4 - Enter 1 Numbers & display in words, without using if else & switch 5
PROGRAM 6 - Enter 2 numbers & display b/w prime numbers - in ascending order 6
PROGRAM 9 - this operator Example, differentiate b/w global & argument variable 8
PROGRAM 10 - this operator Example, differentiate b/w Local & global variable 8
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 2
PROGRAM 18 - Array Program- input 10 values & display using array. 12
PROGRAM 20 - Applet Program for display Hello message with formatted Text. 14
PROGRAM 26 - Calc Program using Custom Layout with Frame & event Handling. 19
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 3
Program 1 - Enter 2 Numbers & display sum of both
import java.util.Scanner;
class Sum
{
public static void main(String aa[])
{
int a,b,c;
Scanner r = new Scanner(System.in);
System.out.print("Plz Enter 2 Numbers : ");
a = r.nextInt(); // for 1st Num
b = r.nextInt(); // for 2nd Num
c = a + b;
System.out.printf("Sum is = %d",c);
}
}
we can perform Runtime inputs in java with the help of Scanner class, stored inside
java.util package & add in jdk1.5 for Runtime inputs.
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 4
c = a * i;
x = a * j;
System.out.printf("%02d * %02d = %03d \t %02d * %02d = %03d\n",a,i,c,a,j,x);
}
}
}
import java.util.Scanner;
class Rev
{
public static void main(String aa[]) //- with command line arguments.
{
Scanner r = new Scanner(System.in);
System.out.print("Plz enter any Number : ");
int x, a = r.nextInt(); // for 1st num
System.out.print("Number in Reverse : ");
while(a>0)
{
x = a % 10;
System.out.print(x);
a = a / 10;
}
}
}
// Program 4 - Enter 1 Numbers & display in words, without using if else & switch
// case , input - 1234 , output - one two three four only.
import java.util.Scanner;
class Word
{
public static void main(String aa[]) //- with command line arguments.
{ // 0 1 2 3 4 5 6 7 8 9
String s = "only.";
String ar[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
Scanner r = new Scanner(System.in);
System.out.print("Plz enter any Number : ");
int x, a = r.nextInt(); // for 1st num
while(a>0)
{
x = a % 10;
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 5
s = ar[x]+" "+s;
a = a / 10;
}
System.out.println(s);
}
}
class Prime1
{
public static void main(String aa[])
{
n: for(int i=1;i<=50;i++) // n is the loop name/label
{
for(int j=2;j<=i/2;j++)
if(i%j==0)
continue n; // contd.. the n loop
System.out.println(i);
}
}
}
__________________________________________________________________________
Program 6 - Enter 2 numbers & display b/w prime numbers - in ascending order
import java.util.Scanner;
class Prime2
{
public static void main(String aa[])
{
Scanner r = new Scanner(System.in);
System.out.print("Plz Enter 2 Numbers : ");
int a = r.nextInt(); // 1st Num
int b = r.nextInt(); // 2nd Num
if(a>b) // a = 50 , b = 10
{
a = a + b; // 50 + 10; a = 60
b = a - b; // 60 - 10; b = 50
a = a - b; // 60 - 50; a = 10
}
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 6
n: for(int i=a;i<=b;i++) // n is the loop label/name
{
for(int j=2;j<=i/2;j++)
if(i%j==0)
continue n; // contd.. the n loop
System.out.println(i);
}
}
}
Program9 – this operator Example, differentiate b/w global & argument variable
class Th1
{
int a;
Th1()
{
a = 5;
}
Th1(int a)
{
this.a = a; // currentobject.a = argument.a
}
void disp()
{
System.out.println("a = "+a);
}
public static void main(String aa[])
{
Th1 o = new Th1(); // no argument passing Constructor
Th1 n = new Th1(100); // one argument passing Constructor calling
o.disp(); // a = 5
n.disp(); // a = 100?
}
}
_____________________________________________________________
Program10 – this operator Example, differentiate b/w Local & global variable
class Th2
{
int a = 1000;
void disp()
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 8
{
int a = 100;
System.out.println("a = "+a); // local a
System.out.println("a = "+this.a); // currentobject.a - global of current class
}
public static void main(String aa[])
{
Th2 o = new Th2();
o.disp(); // a = 100/1000
}
}
class S
{
int a = 1000;
}
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 9
class S1 extends S
{
int a = 100;
void disp()
{
int a = 10;
System.out.println("a = "+a); // local a - 10
System.out.println("a = "+this.a); // currentobject.a - global of current class - 100
System.out.println("a = "+super.a); // super class a - 1000
}
public static void main(String aa[])
{
S1 n = new S1();
n.disp();
}
}
__________________________________________________________________
// Program 16- with Exception handling – using single catch block
// 100% use - this is best algo for exception handling
package my;
class Ex2
{
public static void main(String aa[])
{
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 11
try
{
int a = Integer.parseInt(aa[0]); // NumberFormat + ArrayIndex
int x = 100 / a; // ArithmeticException
System.out.println("x is - "+x);
}
catch(Exception ex) // Exception is the base class of all Exceptions,
{ // it can handle all type of Exception
System.out.println("Exception is - "+ex);
}
System.out.println("Thanxxxxxxxxxxxxxxxxxxx");
}
}
// javac -d . Ex2.java
// java my.Ex2
Program 20 – Applet Program for display Hello message with formatted Text.
import java.awt.*;
import java.applet.*;
public class Hello extends Applet
{
public void paint(Graphics g)
{
setBackground(new Color(253,245,198));
g.setColor(new Color(70,0,0));
g.setFont(new Font("Courier New",1,40));
g.drawString("Welcome to Java Applet ",100,100);
g.setColor(new Color(0,70,0));
g.setFont(new Font("Times New Roman",2,24));
g.drawString("Designed by - Aman Verma....",10,200);
}
}
//<applet code=Hello height=300 width=700></applet>
__________________________________________________________________
Program 21 – face Drawing program using Applet
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 14
import java.awt.*;
public class Face extends java.applet.Applet {
public void paint(Graphics g) {
setBackground(Color.pink);
g.drawOval(50,50,200,300);
g.drawOval(70,140,70,25); // left eye
g.drawOval(160,140,70,25); // right eye
g.fillOval(95,142,22,23); // left pupil
g.fillOval(185,142,22,23); // right pupil
g.drawOval(140,180,25,80); // for nosee
g.drawArc(110,250,80,50,200,140); // for mouth
g.drawOval(25,150,25,110); // for left ear
g.drawOval(250,150,25,110); // for right ear
}
}
//<applet code=Face height=400 width=300></applet>
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 15
add(b5);
}
}
//<applet code=Layout1 height=350 width=500></applet>
_____________________________________________________________________
Program 23 – GridLayout program using Applet
import java.applet.*;
import java.awt.*;
public class Layout2 extends Applet
{
Button b1 = new Button("Click Me");
Button b2 = new Button("Press Me");
Button b3 = new Button("Hit Me");
Button b4 = new Button("Leave Me");
Button b5 = new Button("Cancel Me");
public void init()
{
setFont(new Font("Courier New",0,15));
setBackground(new Color(245,218,167));
setLayout(new GridLayout(2,2,5,5));
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
}
}
//<applet code=Layout2 height=350 width=500></applet>
import java.applet.*;
import java.awt.*;
public class Login extends Applet
{
Button b1 = new Button("Sign In");
Button b2 = new Button("Cancel");
Label a1 = new Label("User Id");
Label a2 = new Label("Password");
TextField t1 = new TextField();
TextField t2 = new TextField();
public void init()
{
setFont(new Font("Courier New",0,15));
setBackground(new Color(245,218,167));
setLayout(null); // remove the default Layout also
a1.setBounds(100,100,90,24);
a2.setBounds(100,150,90,24);
t1.setBounds(200,100,180,24);
t2.setBounds(200,150,140,24);
b1.setBounds(150,200,80,24);
b2.setBounds(240,200,80,24);
t2.setEchoChar('$'); // for set the password field - echo char = display char
add(a1);
add(a2);
add(t1);
add(t2);
add(b1);
add(b2);
}
}
//<applet code=Login height=350 width=500></applet>
______________________________________________________________________
Program 25– Calc Program using Custom Layout with event Handling.
import java.awt.*;
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 17
import java.awt.event.*; // step - 1
public class Calc extends java.applet.Applet implements ActionListener // step - 2
{
Label a1 = new Label("1st Num");
Label a2 = new Label("2nd Num");
Label a3 = new Label("Result");
TextField t1 = new TextField();
TextField t2 = new TextField();
TextField t3 = new TextField();
Button b1 = new Button("Sum");
Button b2 = new Button("Sub");
Button b3 = new Button("Multi");
Button b4 = new Button("Clear");
public void init() {
setLayout(null);
setFont(new Font("Courier New",0,14));
setBackground(new Color(250,210,140));
a1.setBounds(100,70,90,24);
a2.setBounds(100,120,90,24);
a3.setBounds(100,170,90,24);
t1.setBounds(190,70,90,24);
t2.setBounds(190,120,90,24);
t3.setBounds(190,170,90,24);
b1.setBounds(70,220,70,24);
b2.setBounds(150,220,70,24);
b3.setBounds(230,220,70,24);
b4.setBounds(310,220,70,24);
t3.setEditable(false);
add(a1);
add(a2);
add(a3);
add(t1);
add(t2);
add(t3);
add(b1);
add(b2);
add(b3);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int a,b,c;
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 18
try { a = Integer.parseInt(t1.getText()); }catch(Exception ex) { a = 0; }
try { b = Integer.parseInt(t2.getText()); }catch(Exception ex) { b = 0; }
// System.out.println("event source object is - "+e.getSource());
// System.out.println("event Action command is - caption - "+e.getActionCommand());
String cmd = e.getActionCommand();
if(cmd.equals("Sum"))
{
c = a + b;
t3.setText(""+c);
}
if(cmd.equals("Sub"))
{
c = a - b;
t3.setText(""+c);
}
if(cmd.equals("Multi"))
{
c = a * b;
t3.setText(""+c);
}
if(cmd.equals("Clear"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
}
//<applet code=Calc height=300 width=500></applet>
Program 26– Calc Program using Custom Layout with Frame & event Handling.
import java.awt.*;
import java.awt.event.*;
public class CalcF extends Frame implements ActionListener
{
Label a1 = new Label("1st Num");
Label a2 = new Label("2nd Num");
Label a3 = new Label("Result");
NumField t1 = new NumField();
NumField t2 = new NumField();
TextField t3 = new TextField();
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 19
String ar[] = {"Sum","Sub","Multi","Div","Clear"};
CalcF()
{
setLayout(null);
setBackground(new Color(223,211,170));
setForeground(new Color(70,0,0));
setFont(new Font("Courier New",0,15));
a1.setBounds(100,100,90,24);
a2.setBounds(100,150,90,24);
a3.setBounds(100,200,90,24);
t1.setBounds(190,100,90,24);
t2.setBounds(190,150,90,24);
t3.setBounds(190,200,90,24);
t3.setEditable(false);
add(a1);
add(a2);
add(a3);
add(t1);
add(t2);
add(t3);
for(int i=0,x=50;i<ar.length;i++,x+=75)
{
Button b = new Button(ar[i]);
b.setBounds(x,250,70,24);
add(b);
b.addActionListener(this);
}
setBounds(150,150,500,320);
setVisible(true);
addWindowListener(new Win());
}
class Win extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
}
}
public void actionPerformed(ActionEvent e)
{
int a,b,c;
try { a = Integer.parseInt(t1.getText()); }catch(Exception ex) { a = 0; }
try { b = Integer.parseInt(t2.getText()); }catch(Exception ex) { b = 0; }
String cmd = e.getActionCommand(); // return button caption - like - Sum , Sub , Multi ,
Div , Clear
if(cmd.equals("Sum"))
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 20
{
c = a + b;
t3.setText(""+c);
}
if(cmd.equals("Sub"))
{
c = a - b;
t3.setText(""+c);
}
if(cmd.equals("Multi"))
{
c = a * b;
t3.setText(""+c);
}
if(cmd.equals("Div"))
{
try {
c = a / b;
t3.setText(""+c);
}catch(Exception ex) {t3.setText("Infinity"); }
}
if(cmd.equals("Clear"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
public static void main(String aa[])
{
new CalcF();
}
}
___________________________________________________________
Program 26– Notepad Program using Frame.
import java.awt.*;
import java.awt.event.*;
class Notepad extends Frame
{
MenuBar mb = new MenuBar();
Menu f1 = new Menu("File");
Menu f2 = new Menu("Edit");
Menu f3 = new Menu("Help");
MenuItem a1 = new MenuItem("New");
MenuItem a2 = new MenuItem("Open");
MenuItem a3 = new MenuItem("Save");
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 21
MenuItem a4 = new MenuItem("Save As");
MenuItem a5 = new MenuItem("Exit");
MenuItem a6 = new MenuItem("Cut");
MenuItem a7 = new MenuItem("Copy");
MenuItem a8 = new MenuItem("Paste");
MenuItem a9 = new MenuItem("Select All");
MenuItem a10 = new MenuItem("Help Topic");
mb.add(f1);
mb.add(f2);
mb.add(f3);
setMenuBar(mb);
add(ta);
setBounds(10,10,1000,600);
setVisible(true);
addWindowListener(new Win());
}
class Win extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{ dispose(); }
}
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 22
public static void main(String aa[])
{
new Notepad();
}
}
__________________________________________________________________
Program 27– Socket Programming UDP Server Program.
import java.net.*;
class UDPSer
{
public static void main(String aa[])
{
byte msg[];
DatagramPacket pac;
try
{
int ECHO = 7; // for msg send & receive
DatagramSocket s = new DatagramSocket(ECHO); // server socket design with echo Port
while(true)
{
// for receive
msg = new byte[50];
pac = new DatagramPacket(msg,50);
s.receive(pac); // msg received
System.out.println("Client : "+new String(msg));
// for send
msg = new byte[50];
System.out.print("Enter : ");
System.in.read(msg); // msg runtime input
pac = new DatagramPacket(msg,50,pac.getAddress(),pac.getPort());
s.send(pac); // msg send
}
}
catch(Exception ex) { System.out.println(ex);}
}
}
import java.sql.*;
class Data1
{
public static void main(String aa[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.51:1521:xe","students","ipem");
Statement s = c.createStatement();
String sql = "insert into tblEmp values(1002,'Mr. Abhishek .',22000,'23-12-1994')";
int x = s.executeUpdate(sql);
System.out.printf(x+" Record Successfully Inserted");
}catch(Exception ex) { System.out.println(ex); }
}
}
// download the ojdbc14.jar & paste inside own folder
// terminal - jar -xf ojdbc14.jar
Javac Data1.java
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 26
Java Data1
import java.sql.*;
class Data4
{
public static void main(String aa[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.51:1521:xe","students","ipem");
Statement s = c.createStatement();
String sql = "delete from tblemp where empno=1001";
int x = s.executeUpdate(sql);
System.out.println(x+" Record Successfully Deleted");
}catch(Exception ex) { System.out.println(ex); }
}
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 27
}
Program 34– JDBC Program for fetch Record from database table
import java.sql.*;
class Data1
{
public static void main(String aa[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.51:1521:xe","students","ipem");
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("select empno,ename,sal,to_char(dob,'dd-Mon-yyyy') as
dob from tblemp");
ResultSetMetaData mt = rs.getMetaData(); // mt hold the rs meta data
for(int i=1;i<=mt.getColumnCount();i++)
System.out.printf("%-10s | ",mt.getColumnName(i));
while(rs.next())
{
System.out.println();
for(int i=1;i<=mt.getColumnCount();i++)
System.out.printf("%-10s | ",rs.getString(i));
}
}catch(Exception ex) { System.out.println(ex); }
}
}
______________________________________________________________________
Program 35– Servlets Program..
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 28
{
String u = (String)req.getAttribute("id");
PrintWriter out = res.getWriter(); // out hold the response writer for response write
out.println("<body bgcolor=lightyellow><pre><h1>Hello "+u+", Welcome to
Servlets</h1>");
}
}
Java File Designed by Rajesh Pathak (RollNo – 1709101800) BCA 5thB Semester. Page 29