import java.util.*;
import java.io.*;
class c1
{
int length=20;
void ln()
{
System.out.print(length);
}
}
interface intf
{
int breadth=40;
void br();
}
public class c3 extends c1 implements intf
{
public static void main(String[] args)
{
c3 c= new c3();
c.ln();
c.br();
}
public void br()
{
System.out.print(breadth);
}
}
Java Assignments
Developing Clock In java using Multi threading
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
public class Clock extends Applet implements Runnable
{
Thread timer; // The thread that displays clock
SimpleDateFormat formatter; // Formats the date displayed
Date currentDate; // Used to get date to display
Color handColor,numberColor; // Color of main hands and dial// Color of second hand and numbers
int xcenter = 80, ycenter = 55; // Center position
public void init()
{
formatter = new SimpleDateFormat (“EEE MMM dd hh:mm:ss yyyy”);
currentDate = new Date();
handColor = Color.blue;
numberColor = Color.darkGray;
}
public void paint(Graphics g)
{
int xh, yh, xm, ym, xs, ys;
int s = 0, m = 10, h = 10;
String today;
currentDate = new Date();
formatter.applyPattern(“s”);
s = Integer.parseInt(formatter.format(currentDate));
formatter.applyPattern(“m”);
m = Integer.parseInt(formatter.format(currentDate));
formatter.applyPattern(“h”);
h = Integer.parseInt(formatter.format(currentDate));
xs = (int) (Math.cos(s * Math.PI / 30 – Math.PI / 2) * 45 + xcenter);
ys = (int) (Math.sin(s * Math.PI / 30 – Math.PI / 2) * 45 + ycenter);
xm = (int) (Math.cos(m * Math.PI / 30 – Math.PI / 2) * 40 + xcenter);
ym = (int) (Math.sin(m * Math.PI / 30 – Math.PI / 2) * 40 + ycenter);
xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 – Math.PI / 2) * 30 + xcenter);
yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 – Math.PI / 2) * 30 + ycenter);
formatter.applyPattern(“EEE MMM dd HH:mm:ss yyyy”);
today = formatter.format(currentDate);
g.setColor(numberColor);
g.drawString(today, 5, 125);
g.drawLine(xcenter, ycenter, xs, ys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, xm, ym);
g.drawLine(xcenter-1, ycenter, xm, ym);
g.drawLine(xcenter, ycenter-1, xh, yh);
g.drawLine(xcenter-1, ycenter, xh, yh);
g.setColor(handColor);
g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);
g.setColor(numberColor);
g.drawString(“9”, xcenter-45, ycenter+3);
g.drawString(“3”, xcenter+40, ycenter+3);
g.drawString(“12”, xcenter-5, ycenter-37);
g.drawString(“6”, xcenter-3, ycenter+45);
}
public void start()
{
timer = new Thread(this);
timer.start();
}
public void stop()
{
timer = null;
}
public void run()
{
Thread me = Thread.currentThread();
while (timer == me)
{
try
{
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {}
repaint();
}
}
}
Database connectivity in Java with MYSQL
import java.sql.*;
public class jdjd
{
static BufferedReader br;
Connection conn;
Statement stmt;
jdjd () throws Exception
{
String driver=”sun.jdbc.odbc.JdbcOdbcDriver”;
String url=”jdbc:odbc:dbase”;
Class.forName(driver);
conn=DriverManager.getConnection(url);
stmt=conn.createStatement();
}
void addrec() throws Exception
{
String s1,s2,sql;
System.out.println(“Enter Roll No. : “);
s1=br.readLine();
System.out.println(“Enter Name : “);
s2=br.readLine();
sql=”insert into student values(” + s1 + “, ‘” + s2 + “‘)”;
stmt.executeUpdate(sql);
}
void viewrec() throws Exception
{
int i,cols;
String sql=”select * from student”;
ResultSet rs=stmt.executeQuery(sql);
ResultSetMetaData rsmd=rs.getMetaData();
cols=rsmd.getColumnCount();
for(i=1;i<=cols;i++)
{
System.out.println(” ” + rsmd.getColumnClassName(i));
}
while(rs.next())
{
System.out.println();
for(i=1;i<=cols;i++)
{
System.out.println(” ” + rs.getString(i));
}
}
}
void search() throws Exception
{
int i,cols;
String s1;
System.out.println(“Enter Roll No. : “);
s1=br.readLine();
String sql=” select name from student where rno=”+s1+” “;
ResultSet rs=stmt.executeQuery(sql);
ResultSetMetaData rsmd=rs.getMetaData();
cols=rsmd.getColumnCount();
for(i=1;i<=cols;i++)
{
System.out.println(” ” + rsmd.getColumnClassName(i));
}
while(rs.next())
{
System.out.println();
for(i=1;i<=cols;i++)
{
System.out.println(” ” + rs.getString(i));
}
}
}
void update() throws Exception
{
int i,cols;
String s1,s2;
System.out.println(“Enter Roll No. to Update: “);
s1=br.readLine();
System.out.println(“Enter New Name : “);
s2=br.readLine();
String sql=” update student set name = ‘” + s2 + “‘ where rno=” + s1 + ” “;
stmt.executeUpdate(sql);
}
void delrec() throws Exception
{
String s1,s2,sql;
System.out.println(“Enter Roll No. : “);
s1=br.readLine();
sql=”delete from student where rno=”+s1;
stmt.executeUpdate(sql);
}
void closeAll() throws Exception
{
stmt.close();
conn.close();
}
public static void main (String args[])
{
try
{
br=new BufferedReader(new InputStreamReader(System.in));
String temp;
jdjd d=new jdjd();
do
{
System.out.println(“1.Add Record”);
System.out.println(“2.View Record”);
System.out.println(“3.Delete Record”);
System.out.println(“4.Search Record”);
System.out.println(“5.Update Record”);
System.out.println(“5.Exit”);
System.out.println(“Enter UR Choice”);
temp=br.readLine();
if(temp.equals(“1”))
{
d.addrec();
}
else if(temp.equals(“2”))
{
d.viewrec();
}
else if(temp.equals(“3”))
{
d.delrec();
}
else if(temp.equals(“4”))
{
d.search();
}
else if(temp.equals(“5”))
{
d.update();
}
}while(!temp.equals(“6”));
d.closeAll();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
File Handling program using Java
import java.util.*;
class record implements Serializable
{
static Scanner input=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int roll;
String name;
void insert()
{
try
{
System.out.println(“\n Enter Roll No:: “);
roll=input.nextInt();
System.out.println(“\n Enetr Name:: “);
name=br.readLine();
FileOutputStream fout=new FileOutputStream(“Student.txt”,true);
byte buff[]=name.getBytes();
fout.write((char)roll);
fout.write(‘\n’);
fout.write(buff);
fout.write(‘\n’);
fout.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void delete(int r)
{
try
{
FileInputStream fin = new FileInputStream(“student.txt”);
FileOutputStream fout=new FileOutputStream(“temp.txt”);
while(true)
{
int a=fin.read();
if(a==-1)
break;
if(a!=r)
{
fout.write((byte)a);
a=fin.read();
fout.write(a);
while(true)
{
a=fin.read();
if(a==’\n’)
break;
fout.write(a);
}
fout.write(a);
}
else
{
a=fin.read();
do{
a=fin.read();
}while(a!=’\n’);
}
}
fin.close();
fout.close();
File f=new File(“student.txt”);
File f1=new File(“temp.txt”);
if(f.delete())
f1.renameTo(new File(“student.txt”));
else
System.out.println(“Error”);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void showdata(int r)
{
try
{
FileInputStream fin=new FileInputStream(“student.txt”);
while(true)
{
int a=fin.read();
if(a==-1)
break;
if(a==r)
{
System.out.println(“roll no:: “+a);
a=fin.read();
System.out.println(“Name:: “);
do
{
a=fin.read();
System.out.print((char)a);
}while(a!=’\n’);
}
else
{
do
{
a=fin.read();
}while(a!=’\n’);
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void show()
{
try
{
FileInputStream fin=new FileInputStream(“student.txt”);
while(true)
{
int a=fin.read();
if(a==-1)
break;
System.out.println(“roll no:: “+a);
a=fin.read();
System.out.println(“Name:: “);
do
{
a=fin.read();
System.out.print((char)a);
}while(a!=’\n’);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
public class Student extends record
{
public static void main(String args[])
{
try
{
while(true){
System.out.println(“\nEnter your choice : “);
System.out.println(“1:Insert.”);
System.out.println(“2:Delete.”);
System.out.println(“3:View record.”);
System.out.println(“4:View All.\n”);
int choice=input.nextInt();
record r=new record();
switch(choice)
{
case 1:
r.insert();
break;
case 2:
System.out.println(“Enter roll no. : “);
int roll=input.nextInt();
r.delete(roll);
break;
case 3:
System.out.println(“Enter roll no. : “);
roll=input.nextInt();
r.showdata(roll);
break;
case 4:
r.show();
break;
default:
System.out.println(“Invalid Entry !!”);
break;
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Exception Handling In java
import java.util.*;
class myex extends Exception
{
myex(String m)
{
super(m);
}
}
class myex1 extends Exception
{
myex1(String m)
{
super(m);
}
}
public class Bank
{
static Scanner input=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int bal;
static int accno;
String name;
Bank()
{
bal=0;
accno=2873;
name=””;
}
void newacc()
{
try
{
System.out.println(“\n Enter Name:: “);
name=br.readLine();
accno++;
System.out.println(“\n Your Acc No:: “+accno);
}
catch(Exception e){}
}
void showinfo() throws myex
{
System.out.println(“\n Enter Acc No:: “);
accno=input.nextInt();
if(accno<0)
{
throw new myex(“\naccount no is wrong\n”);
}
System.out.println(“\n Your name is:: ” + name);
}
void wd() throws myex1,myex
{
System.out.println(“\n Enter Acc No:: “);
accno=input.nextInt();
if(accno<0)
{
throw new myex(“\naccount no is wrong\n”);
}
System.out.println(“\n Enter Money:: “);
int money=input.nextInt();
if(money%100!=0)
{
throw new myex1(“\nmoney sd be multiple of 100 \n”);
}
System.out.println(“\n Your name is:: ” + name);
}
public static void main (String[] args)
{
int ch;
Bank b =new Bank();
do
{
System.out.println(“\n 1.New Acc”);
System.out.println(“\n 2.Acc Info”);
System.out.println(“\n 3.Dep”);
System.out.println(“\n 4.Wd”);
System.out.println(“\n Enter Ur Choice”);
ch=input.nextInt();
switch(ch)
{
case 1:
b.newacc();
break;
case 2:
try
{
b.showinfo();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
break;
case 3:
try
{
b.wd();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
break;
}
}while(ch!=5);
}
}