advanced java record
Embed Size (px)
TRANSCRIPT

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 1/28
THE RECORD BOOK OF ADVANCED JAVA PROGRAMMING
LABORATORY
BAKTAVATCHALAM.G(08MW03)
Dissertation submitted in partial fulfillment of the requirements for the degree of
MASTER OF ENGINEERING
Branch: SOFTWARE ENGINEERING
of Anna University
November 2008
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
PSG COLLEGE OF TECHNOLOGY
(Autonomous Institution)
COIMBATORE – 641 004

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 2/28
Ex.No.1
Aim:
To find Maturity for given Rate, Year and Capital
Code:
import java.io.*;
public class Maturity {
public static void main(String[] ss) throws Exception
{
double m,p,r,n;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
m=p=r=n=0;
//m=Double.parseDouble(in.readLine());
System.out.println("Enter P:");
p=Double.parseDouble(in.readLine());
System.out.println("Enter R:");
r=Double.parseDouble(in.readLine());
System.out.println("Enter N:");
n=Double.parseDouble(in.readLine());
m=p*(Math.pow((1+r/100),n));
System.out.println("M="+m);
}
}
Output:
Enter P:2000
Enter R:15
Enter N:3
M=3041.7499999999995
Result:
Thus the above program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 3/28
Ex.No.2
Aim:
To find the Students who had given constraints.
Code:
import java.io.*;
public class Student
{
public static void main(String[] aa) throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int rno=0;
String name="",dob="";
double H=0,W=0;
System.out.println("Enter RollNo:");
rno=Integer.parseInt(in.readLine());
System.out.println("Enter Name:");
name=in.readLine();
System.out.println("Enter DOB[DD-MM-YYYY]:");
dob=in.readLine();
System.out.println("Enter Height:");
H=Double.parseDouble(in.readLine());
System.out.println("Enter Width:");
W=Double.parseDouble(in.readLine());
Students ss=new Students(rno,name,dob,H,W);
ss.disp();
}
}

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 4/28

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 5/28
Output:
Enter RollNo:03
Enter Name:SuperStar
Enter DOB[DD-MM-YYYY]:22-jul-1985
Enter Height:175
Enter Width:65
---------------------
RollNo:3
Name:SuperStar
DOB:22-jul-1985
Height:175.0
Width:65.0
Result:
Thus the above program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 6/28
Ex.No.3
Aim:
To evaluate Student marks to find the eligibility of Technical Pass.
Code:
import java.io.*;
public class MarkEval {
public static void main(String[] str) throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter M1:");
int m1=Integer.parseInt(in.readLine());
System.out.println("Enter M2:");
int m2=Integer.parseInt(in.readLine());
if(m1>=15 && m2>=15)
{
if(m1>=39 &&m2>=39)
System.out.println("Pass!");
else
System.out.println("Fail[Mark<15]!");
}
else
System.out.println("Fail[Mark<15]!");
}
}

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 7/28
Output:
Enter M1:
39
Enter M2:
49
Pass!
Result:
Thus the above program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 8/28
Ex.No.4
Aim:
To find the total wages to be paid according to given criteria
Code:
import java.io.*;
public class Wages {
public static void main(String[] str) throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Workers Total Hours:");
int m1=Integer.parseInt(in.readLine()),w=0;
for(int i=1;i<=m1;i++)
{
if(i<=30)
w+=30;
else if(i>30 && i<=55)
w+=(45);
else
w+=60;
}
System.out.println("Wages: Rs."+w);
}
}
Output:
Enter Workers Total Hours:245
Wages: Rs.13425
Result:
Thus the above program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 9/28
Ex.No.5
Aim:
To find commission of the given product code
Code:
import java.io.*;
public class Commission {
public static void main(String[] ss) throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Product Code:");
int c=Integer.parseInt(in.readLine());
switch(c)
{
case 1: System.out.println("Commission:"+0.5); break;
case 2: System.out.println("Commission:"+1); break;
case 3: System.out.println("Commission:"+2); break;
case 4: System.out.println("Commission:"+3); break;
case 5: System.out.println("Commission:"+3.5); break;
}
}
}
Output:
Enter Product Code:2
Commission:1
Result:
Thus the above program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 10/28
Ex.No.6
Aim:
To find the total salary of a man in which the salary twice per each day
Code:
import java.io.*;
public class PayTwice {
public static void main(String[] ss) throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Mans Total Working Days:");
int c=Integer.parseInt(in.readLine()),p=0;
for(int i=1;i<=c;i++)
p+=(i*25);
System.out.println("Mans Total Pay:"+p);
}
}
Output:
Enter Mans Total Working Days:30
Mans Total Pay:11625
Result:
Thus the above program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 11/28
Ex.No.7
Aim:
To find the given matrix is a Identity Matrix or not
Code:
import java.io.*;
public class IdentityMatrix {
public static void main(String[] ss) throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Square Matrix Size:");
int n=Integer.parseInt(in.readLine());
boolean f=true;
int[][] m=new int[n][n];
System.out.println("Enter Matrix Values One By One:");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
m[i][j]=Integer.parseInt(in.readLine());
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(i==j && m[i][j]==1)
f=true;
else if(i!=j && m[i][j]==0)
f=true;
else
{
f=false;
break;

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 12/28
}
}
if(f)
System.out.println("It's a Identity Matrix!");
else
System.out.println("It's Not a Identity Matrix!");
}
}
Output:
Enter Square Matrix Size: 3
Enter Matrix Values One By One:
1 0 0
0 1 0
0 0 1
It's a Identity Matrix!
Result:
Thus the above program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 13/28
Ex.No.8
Aim:
To implement STD call rate with given input parameters
Code:
public class MakingCalls {
public static void main(String[] ss)
{
STDCall s1,s2,s3;
s1=new STDCall("235707", "235480", 15, 8, 16, "04343", "04346");
s2=new STDCall("244707", "236780", 65, 23, 6, "04373", "04546");
s3=new STDCall("266707", "244580", 35, 5, 7, "04783", "04676");
s1.disp(); s2.disp(); s3.disp();
}
}
////////////////////////////////////
class Call
{
String sTno="",dTno=""; int d=0,tts=0,tte=0,nou=0;
public Call(String src,String dest,int d1,int t1,int t2)
{
sTno=src; dTno=dest; d=d1; tts=t1; tte=t2;
switch(d)
{
case 2: nou=1; break;
case 3: nou=1; break;
case 4: nou=2; break;
case 5: nou=2; break;
case 16: nou=6; break;
default: nou=6;
}

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 14/28
}
}
//////////////////////////////////////
class STDCall extends Call
{
String sC="",dC=""; int pR=0; Concession con;
public STDCall(String src,String dest,int d1,int t1,int t2,String sc,String dc)
{
super(src,dest,d1,t1,t2); sC=sc; dC=dc;
pR=d1; con=new Concession(d1);
}
public void disp()
{
System.out.println("\nSrc:"+sC+"-"+sTno+"\nDest:"+dC+"-"+dTno+"\nPulseRate:"+pR+"\nConcession Rate:"+con.getCon(pR)+"\nNo of Units:"+nou+"\n");
}
}
////////////////////////////////////////////
class Concession
{
int dur=0;
public Concession(int d)
{ dur=d; }
public double getCon(double p)
{
double pr=p;
if(dur>=8 && dur<=18) pr*=1;
else if(dur>=18 && dur<=20) pr*=1/2;
else if(dur>=20 && dur<=23) pr*=1/3;
else if(dur>=23 && dur<=5) pr*=1/4;
else if(dur>=5 && dur<=6) pr*=1/3;

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 15/28

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 16/28
Ex.No.9
Aim:
To find Average of N numbers with User Defined Exception.
Code:
import java.io.*;
public class AvgOfNos {
public static void main(String[] ss)
{
try {
int n=0,no[],s=0;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter How many No's:");
n=Integer.parseInt(in.readLine());
no=new int[n];
System.out.println("Enter the nos one by one:");
for(int i=0;i<n;i++)
{
no[i]=Integer.parseInt(in.readLine());
if(no[i]<=0) throw new ZeroE("You are entered 0/-ve !!!");
else s+=no[i];
}
System.out.println("Avg:"+(s/n));
} catch(ZeroE e) { System.out.println("ZError:"+e.toString()); }
catch(Exception e1) { System.out.println("StdError:"+e1.toString()); }
}
}
////////////////////////////
class ZeroE extends Exception {
public ZeroE(String msg) { super(msg); }
}

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 17/28
Output:
Enter How many No's:5
Enter the nos one by one:
-1
ZError:ZeroE: You are entered 0/-ve !!!
Enter How many No's: 5
Enter the nos one by one: 5 6 7 34 23
Avg:15
Result:
Thus the above program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 18/28

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 19/28
public Emp(String e,String n, Date d1,Date d2)
{
ecode=e; name=n; dob=d1; doa=d2;
}
public void verifyD() throws DateE
{
if(dob.before(doa)) throw new DateE(ecode+"-DOB>DOA");
else System.out.println(ecode+"-Date Format is Valid!");
}
public void verifyC() throws CodeE {
if( ecode.charAt(0)>='0' && ecode.charAt(0)<='9' &&
ecode.charAt(1)>='0' && ecode.charAt(1)<='9' &&
ecode.charAt(3)=='M' || ecode.charAt(3)<='A' ||
ecode.charAt(3)=='H' || ecode.charAt(3)<='E' ||
ecode.charAt(3)=='T' &&
ecode.charAt(5)>='0' && ecode.charAt(5)<='9' &&
ecode.charAt(6)>='0' && ecode.charAt(6)<='9' &&
ecode.charAt(7)>='0' && ecode.charAt(7)<='9'
) System.out.println(ecode+"-Emp Code Format is Valid!");
else throw new CodeE(ecode+"-Invalid Emp Code[nn-#-nnn](#-M,A,H,E,T)!");
} }
Output:
08-A-001-Emp Code Format is Valid!
CodeE: 08-X-002-Invalid Emp Code[nn-#-nnn](#-M,A,H,E,T)!
Result:
Thus the above program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 20/28
Ex.No.11
Aim:
To implement a basic calculator using Applet
Code:
import java.awt.*;import java.awt.event.*;import javax.swing.*;
//<applet code=Calc height=500 width=500></applet> public class Calc extends JApplet implements ActionListener {
JTextField t1,t2,t3,t4;JButton b1,b2;double n1,n2,ans;String op;
public void init()
{ Container cp=getContentPane();cp.setLayout(new FlowLayout());t1=new JTextField(15);t1.setText("0");t2=new JTextField(3);t2.setText("+");t3=new JTextField(15);t3.setText("0");t4=new JTextField(15);t1.setText("0");b1=new JButton("=");b2=new JButton("C");n1=n2=ans=0;op="";cp.add(t1);//,BorderLayout.NORTH);cp.add(t2);//,BorderLayout.NORTH);cp.add(t3);//,BorderLayout.NORTH);cp.add(t4);//,BorderLayout.NORTH);cp.add(b1);//,BorderLayout.SOUTH);cp.add(b2);//,BorderLayout.SOUTH);cp.add((new JLabel("*GMB Super Calculator*")));//,BorderLayout.CENTER);b1.addActionListener(this);b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{ if(ae.getActionCommand().equalsIgnoreCase("=")){
n1=Double.parseDouble(t1.getText());n2=Double.parseDouble(t3.getText());op=t2.getText();switch((char)op.charAt(0)){
case '+': ans=n1+n2; break;

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 21/28
case '-': ans=n1-n2; break;case '*': ans=n1*n2; break;case '/': ans=n1/n2; break;
}t4.setText(ans+"");
}else if(ae.getActionCommand().equalsIgnoreCase("C")){
t1.setText("0");t2.setText("0");t3.setText("+");t4.setText("0");
} }
}
Output:
Result:
Thus the above applet program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 22/28
Ex.No.12
Aim:
To calculate Average of Variable size of numbers using Swing
Code:
import java.awt.*;import java.awt.event.*;import javax.swing.*;
public class Avg extends JFrame implements ActionListener {
JTextField t1,t2;JButton b1,b2;double n2;String n1[];
public Avg(){
Container cp=getContentPane();cp.setLayout(new FlowLayout());t1=new JTextField(15);t1.setText("0,0");t2=new JTextField(15);t2.setText("0");b1=new JButton("=");b2=new JButton("C");cp.add(t1);//,BorderLayout.NORTH);cp.add(t2);//,BorderLayout.NORTH);cp.add(b1);//,BorderLayout.SOUTH);cp.add(b2);//,BorderLayout.SOUTH);cp.add((new JLabel("*GMB Super Adder*")));b1.addActionListener(this);b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equalsIgnoreCase("=")){
n1=(t1.getText().split(",")); //n2=Double.parseDouble(t3.getText());n2=0;for(int i=0;i<n1.length;i++)
n2+=(Double.parseDouble(n1[i]));t2.setText(""+(n2/n1.length));
}else if(ae.getActionCommand().equalsIgnoreCase("C")){
t1.setText("0");t2.setText("0");
} }

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 23/28
public static void main(String[] ss){ new Avg().setVisible(true);
} }
Output:
Result:
Thus the above swing program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 24/28
Ex.No.13
Aim:
To implement DTD for an XML Document
Code:
DTD:
<!ELEMENT mydoc (gmb)+>
<!ELEMENT gmb (vp,ls)>
<!ELEMENT vp (#PCDATA)>
<!ELEMENT ls (#PCDATA)>
XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="a.xsl"?>
<!DOCTYPE mydoc SYSTEM "a.dtd">
<mydoc>
<gmb>
<vp>12345</vp>
<ls>abcdefg</ls>
</gmb>
<gmb>
<vp>m</vp>
<ls>t</ls>
</gmb>
<gmb>
<vp>b</vp>
<ls>a</ls>
</gmb>
</mydoc>

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 25/28
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="mydoc">
<html> <body fgcolor="pink">
<font size="15" face="Tahoma">
<table border="2">
<xsl:for-each select="gmb">
<tr><td style='color:#ff0f0f'>1.</td>
<td style='color:pink'><xsl:value-of select="vp" ></xsl:value-of><br/></td>
</tr> <tr>
<td style='color:#ff00ff'>2.</td>
<td style='color:red'><xsl:value-of select="ls" ></xsl:value-of><br/></td>
</tr> </xsl:for-each>
</table> </font> </body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
1. 12345
2. abcdefg
1. m
2. t
1. b
2. a
Result:
Thus the above XML program is executed successfully.

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 26/28
Ex.No.13
Aim:
To implement DTD for an XML Document with Style Sheet Definition
Code:
XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="b.xsl"?>
<!DOCTYPE mydoc SYSTEM "b.dtd">
<mydoc>
<gmb>
<vp>
<age>12</age>
<dab>34</dab>
</vp>
<ls>vprakash</ls>
</gmb>
<gmb>
<vp>
<age>12</age>
<dab>34</dab>
</vp>
<ls>gmbaktha</ls>
</gmb>
<gmb>
<vp>
<age>12</age>
<dab>34</dab>
</vp>
<ls>SuperStar</ls>
</gmb>

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 27/28
</mydoc>
DTD:
<!ELEMENT mydoc (gmb)+>
<!ELEMENT gmb (vp,ls)>
<!ELEMENT vp (age,dab)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT dab (#PCDATA)>
<!ELEMENT ls (#PCDATA)>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="mydoc">
<html>
<body fgcolor="pink">
<font size="15" face="Tahoma">
<table border="2">
<xsl:for-each select="gmb">
<xsl:for-each select="vp">
<tr><td style='color:aqua'>1.</td>
<td style='color:pink'><xsl:value-of select="age" ></xsl:value-of><br/></td>
<td style='color:blue'><xsl:value-of select="dab" ></xsl:value-of><br/></td>
</tr>
</xsl:for-each>
<tr>
<td style='color:green'>2.</td>
<td style='color:red'><xsl:value-of select="ls" ></xsl:value-of><br/></td>
</tr>
</xsl:for-each>

8/14/2019 Advanced Java Record
http://slidepdf.com/reader/full/advanced-java-record 28/28
</table>
</font>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
1. 12 34
2. vprakash
1. 12 34
2. gmbaktha
1. 12 34
2. SuperStar
Result:
Thus the above XML program is executed successfully.