12PCAMP301 Core Practical Vi: Java Programming Semester - Iii
12PCAMP301 Core Practical Vi: Java Programming Semester - Iii
1. Develop a program that should use default constructor and parameterized constructor.
2. Develop a program to implement the concept of Inheritance and super keyword.
3. Develop a program to implement the concept of Overriding.
4. Develop a program to implement the concept of packages and Interfaces.
5. Develop a program to implement the concept of user defined exception.
6. Develop a program to implement the concept of AWT and graphics.
7. Develop a program to implement the concept of AWT with Event Handling.
8. Develop a program to implement the concept of Collection frameworks using
a. List Interface
b. Set Interface
c. Map Interface
9. Develop a program for Byte Streams using java.io package.
10. Develop a program for Character Streams using java.io package.
1
DEFAULT CONSTRUCTOR AND PARAMETERIZED CONSTRUCTOR
AIM
To write program JAVA program that implements the concept of Default Constructor and
Parameterized Constructor.
Packages
java, io
Classes
DataInputStream, Integer
Methods
parseInt(), readLine (), toUpperCase(),equals(), charAt(), substring()
ALGORITHM
PROGRAM
Strex.java
import java.io.*;
class Strex
{
Strex(String s1)
{
System.out.println("\n Upper case:"+ s1.toUpperCase());
}
4
INHERITANCE AND SUPER KEYWORD
AIM
To write program JAVA program that implements the concept of Inheritance and super
keyword.
Packages
java, io
Classes
DataInputStream
Methods
readLine ()
ALGORITHM
PROGRAM
import java.io.*;
class student
{
student()
{
System.out.println("\n**************************************************************");
System.out.println("\n********* K.S.Rangasamy college of Arts and Science *********");
}
void disp()
{
System.out.println("\n********* Department of MCA **********");
System.out.println("\n***************************************************************");
}
}
class mca extends student
{
String reno,ne;
int mk1,mk2,mk3,tot;
float per;
mca(String regno,String name,int m1,int m2,int m3)
{
super();
reno=regno;ne=name;
5
mk1=m1;mk2=m2;mk3=m3;
tot=m1+m2+m3;
per=tot/3;
}
void disp()
{
super.disp();
System.out.println("\n********** Regno:"+reno+" "+ "Name:"+ne+" "+"
*****************");
if((mk1>=50)&&(mk2>=50)&&(mk3>=50))
System.out.println("\n********* Reslult:Pass ********************");
else
System.out.println("\n********* Reslult:Fail **********************");
System.out.println("\n********* Percentage:"+per+" **********************");
System.out.println("\n**************************************************************");
}
}
class inheritance
{
public static void main(String ar[]) throws IOException
{
DataInputStream da=new DataInputStream(System.in);
String regno,name;
int m1,m2,m3;
INPUT / OUTPUT
6
OVERRIDING
AIM
7
To write program JAVA program that implements the concept of Method
Overriding.
Packages
java, io
Classes
DataInputStream
Methods
readLine ()
ALGORITHM
PROGRAM
Product.java
import java.io.*;
class prod
{
void disp()
{
System.out.println("Super market");
}
}
class product1 extends prod
{
int pri[]={25,50,30,10,200,5,2,3,100,10};
int pno[]={1,2,3,4,5,6,7,8,9,10};
String pro[] = {"Hamam","cinthol","Lux","colgate", "Boost","pen","pencil", "Eraser",
"Ponds","Box"};
void disp()
{
System.out.println("\n\t\t------------Welcome to purchase world------------");
System.out.println();
System.out.print("\n\n\nProduct num\tProduct name\tprice\n");
System.out.print("_____________\t____________\t_____\n\n");
for(int i=0;i<=9;i++)
{
System.out.print(pno[i]+"\t\t");
8
System.out.print(pro[i]+"\t\t");
System.out.print(pri[i]+"\n");
}
}
}
class Product
{
public static void main(String a[])throws IOException
{
product1 p=new product1();
p.disp();
DataInputStream ab=new DataInputStream(System.in);
System.out.println("\nEnter the product number available in the list:");
int pno=Integer.parseInt(ab.readLine());
System.out.println("Enter the quantity:");
int qty=Integer.parseInt(ab.readLine());
for(int i=0;i<9;i++)
{
if(pno==p.pno[i])
{
System.out.println("\n\nProduct is:\t"+p.pro[i]+"\nPrice is:\t" +
p.pri[i]*qty);
}
}
System.out.println("\n\n-----------Welcome again---------");
}
}
INPUT / OUTPUT
9
10
PACKAGES AND INTERFACES
AIM
To write program JAVA program that implements the concept of Packages and Interfaces.
Packages
java, io , netpay(user defined package)
Classes
DataInputStream
Methods
readLine ()
ALGORITHM
Step 1: Get the details of an employee such as name, designation, salary and others for gross
pay.
Step 2: Declare a new defined package.
Step 3: In the user defined package, perform the calculation for tax.
Step 4: Import the user defined package, and call the method for calculation in
Step 5: Display the result (tax details).
PROGRAM
payroll.java
import java.io.*;
import netpay.netpy;
interface grosspay
{
public int pfcalc(String des);
public int grossp(int bp,int hra,int da,int ta);
}
class gpay implements grosspay
{
int pf=0;
public int pfcalc(String des)
{
if(des=="professor")
pf=1200;
else if(des=="assistentprofessor")
pf=780;
else if(des=="lecture")
pf=500;
return pf;
}
public int grossp(int bp,int hra,int da,int ta)
{
11
int gspy;
gspy=bp+hra+da+ta;
return gspy;
}
}
class payroll
{
public static void main(String ar[]) throws IOException
{
DataInputStream dai=new DataInputStream(System.in);
System.out.println("\tPayroll requirement");
System.out.println("\t~~~~~~~~~~~~~~~~~");
System.out.println("*********************************");
System.out.println("Enter Empolyee_ID:");
int id=Integer.parseInt(dai.readLine());
System.out.println("Enter Designation:");
String des=dai.readLine();
System.out.println("******************************");
System.out.println("\tPAYROLL SYSTEM");
System.out.println("\t***************************");
System.out.println("Employee ID:"+id);
System.out.println("Employee Name:"+name);
System.out.println("Designation is:"+des);
12
netpay.java
package netpay;
public class netpy
{
public int npay(int gspy,int pf)
{
int np;
np=gspy-pf;
System.out.println("Net pay:"+np);
return np;
}
public void tax(int np)
{
int ta=0;
if(np<=204000)
System.out.println("Tax is:No Tax");
else if((np>=200001)&&(200001<=300000))
{
ta=np*10/100;
System.out.println("Tax Amount:"+ta);
}
else if((np>=300001)&&(300001<=400000))
{
ta=np*20/100;
System.out.println("Tax Amount:"+ta);
}
else if(np>=400001)
{
ta=np*30/100;
System.out.println("Tax Amount:"+ta);
}
}
}
INPUT / OUTPUT
13
14
USER DEFINED EXCEPTION
AIM
To write program JAVA program that implements the concept of User Defined Exception.
Packages
java, io
Classes
DataInputStream, Integer, Float
Methods
parseInt(), readLine (), parseFloat()
ALGORITHM
PROGRAM
bank.java
import java.io.*;
class NoBalanceException extends Exception
{
NoBalanceException(String str)
{
super(str);
}
}
class bank
{
public static void main(String a[])throws IOException,NoBalanceException
{
float bal=20000;
float wd,dp;
DataInputStream ab=new DataInputStream(System.in);
int ch=1;
String s;
do
{
System.out.println("\n1.withdraw\n2.Deposit\n3.Balance enquiry\n4.exit");
System.out.println("Enter your chioce:");
15
ch=Integer.parseInt(ab.readLine());
switch(ch)
{
case 1:
{
System.out.println("Enter the amount to withdraw:");
wd=Float.parseFloat(ab.readLine());
if(bal>wd)
{
bal=bal-wd;
System.out.println("Available balance is:"+bal);
}
else
{
NoBalanceException n=new NoBalanceException
("Sorry!!!Insufficiant balance");
throw n;
}
break;
}
case 2:
{
System.out.println("Enter the amount to deposit:");
dp=Float.parseFloat(ab.readLine());
bal=bal+dp;
System.out.println("New balance is:"+bal);
break;
}
case 3:
{
System.out.println("Available balance in your account is:"+bal);
break;
}
case 4:
{
break;
}
}
System.out.println("Do you want to do another transaction(y/n):");
s=ab.readLine();
}while(s=="y");
System.out.println("THANK YOU!!!!!!!!!");
}
}
INPUT / OUTPUT
16
AWT AND GRAPHICS
17
AIM
To write program JAVA program that embeds the Applet and Graphics.
Packages
java, applet, awt
Classes
Applet, Graphics, Font
Methods
paint(), setColor(), fillOval(), drawstring(), drawLine(), drawOval(), setFont()
ALGORITHM
PROGRAM
wallclock.java
import java.applet.*;
import java.awt.*;
18
g.drawLine(590,500,660,600);
g.drawLine(590,500,740,500);
g.setColor(Color.blue); g.drawOval(355,250,500,500);
g.setColor(Color.cyan); g.fillOval(465,305,20,20);
g.setColor(Color.cyan); g.fillOval(490,290,20,20);
g.setColor(Color.cyan); g.fillOval(520,280,20,20);
g.setColor(Color.cyan); g.fillOval(550,270,20,20);
g.setColor(Color.cyan); g.fillOval(580,265,20,20);
g.setColor(Color.red); g.fillOval(610,265,20,20);
g.setColor(Color.red); g.fillOval(640,270,20,20);
g.setColor(Color.red); g.fillOval(670,280,20,20);
g.setColor(Color.red); g.fillOval(700,295,20,20);
g.setColor(Color.red); g.fillOval(730,315,20,20);
g.setColor(Color.black); g.fillOval(755,335,20,20);
g.setColor(Color.black); g.fillOval(775,365,20,20);
g.setColor(Color.black); g.fillOval(790,395,20,20);
g.setColor(Color.black); g.fillOval(805,425,20,20);
g.setColor(Color.black); g.fillOval(815,455,20,20);
g.setColor(Color.cyan); g.fillOval(818,490,20,20);
g.setColor(Color.cyan); g.fillOval(818,520,20,20);
g.setColor(Color.cyan); g.fillOval(815,550,20,20);
g.setColor(Color.cyan); g.fillOval(808,580,20,20);
g.setColor(Color.cyan); g.fillOval(790,610,20,20);
g.setColor(Color.red); g.fillOval(771,640,20,20);
g.setColor(Color.red); g.fillOval(741,670,20,20);
g.setColor(Color.red); g.fillOval(700,695,20,20);
g.setColor(Color.red); g.fillOval(660,715,20,20);
g.setColor(Color.red); g.fillOval(620,720,20,20);
g.setColor(Color.black); g.fillOval(580,720,20,20);
g.setColor(Color.black); g.fillOval(540,717,20,20);
g.setColor(Color.black); g.fillOval(500,700,20,20);
g.setColor(Color.black); g.fillO val(465,680,20,20);
g.setColor(Color.black); g.fillOval(430,655,20,20);
g.setColor(Color.cyan); g.fillOval(403,615,20,20);
g.setColor(Color.cyan); g.fillOval(385,585,20,20);
g.setColor(Color.cyan); g.fillOval(370,555,20,20);
g.setColor(Color.cyan); g.fillOval(365,515,20,20);
g.setColor(Color.cyan); g.fillOval(362,475,20,20);
g.setColor(Color.red); g.fillOval(369,435,20,20);
g.setColor(Color.red); g.fillOval(379,400,20,20);
g.setColor(Color.red); g.fillOval(405,360,20,20);
g.setColor(Color.red); g.fillOval(428,330,20,20);
g.setColor(Color.black);
Font f=new Font("Arial",Font.BOLD,30);
g.setFont(f);
g.drawString("SONY",560,420);
}
19
}
/*
<applet code="wallclock.class" width=300 height=500></applet>
*/
INPUT / OUTPUT
AIM
To write program JAVA program that embeds the AWT with Event Handling.
20
PACKAGES | CLASSES |METHOD USED
Packages
java, applet, awt
Classes
Applet, Font, Choice, String, Color
Interface
ItemListener, ItemEvent,
Methods
add(), addItemListener(), itemStateChanged(), getSelectedItem(), repaint(),
setBackground(), setFont(), setColor()
ALGORITHM
PROGRAM
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class smiley extends Applet implements ItemListener
{
Font f;
Choice ch;
String opt;
int x,y,z,h;
ch.addItemListener(this);
}
21
switch (opt)
{
case "smile":
x = 305; y = 320;z=-180;h=20;
repaint();
break;
case "cry":
x = 305; y = 320;z=180;h=20;
repaint();
break;
case "normal":
x = 305; y = 320;z=-180; h =1;
repaint();
break;
}
}
public void paint(Graphics g)
{
setBackground(Color.pink);
f=new Font("Imprint MT Shadow",Font.ITALIC,30);
g.setFont(f);
g.setColor(Color.yellow);
g.fillOval(250,250,150,100);
g.setColor(Color.black);
g.fillOval(300,280,10,15);
g.fillOval(340,280,10,15);
g.drawOval(320,300,1,10);
g.fillArc(x,y,40,h,0,z);
}
}
/*
<applet code=smiley width=600 height=600>
</applet>
*/
INPUT / OUTPUT
22
23
24
COLLECTION FRAMEWORKS
AIM
To write program JAVA program that uses the List Interface of Collections Framework.
Packages
java, io, util
Classes
DataInputStream, String, ArrayList, Integer
Methods
parseInt(), readLine(), add(), remove(), contains(), equals()
ALGORITHM
PROGRAM
import java.util.*;
import java.io.*;
class collectionprg
{
public static void main(String ar[]) throws IOException
{
DataInputStream dis = new DataInputStream(System.in);
int ch;
String item,opt;
do
{
System.out.println("1.Add an Item");
System.out.println("2.Add in index");
System.out.println("3.Remove an Item");
System.out.println("4.Search an Item");
System.out.println("What is ur choice");
ch = Integer.parseInt(dis.readLine());
25
switch(ch)
{
case 1:
System.out.println("Enter the item to add");
item = dis.readLine();
al.add(item);
break;
case 2:
System.out.println("Enter the item to add");
item = dis.readLine();
System.out.println("Enter the index to add");
int ind = Integer.parseInt(dis.readLine());
al.add(ind,item);
break;
case 3:
System.out.println("Enter the item to remove");
item = dis.readLine();
al.remove(item);
break;
case 4:
System.out.println("Enter the item to search");
item = dis.readLine();
if (al.contains(item))
{
System.out.println("Available");
}
else
{
System.out.println("Not Available");
}
break;
}
System.out.println("Items are" + al);
System.out.println("Want to continue(Y|N)");
opt = dis.readLine();
}while(opt.equals("y"));
}
}
INPUT / OUTPUT
26
27
28
BYTE STREAMS
AIM
Packages
java, io
Classes
DataInputStream, String, FileInputStream, FileOutputStream
Methods
readLine(), read(), write(), close()
ALGORITHM
PROGRAM
import java.io.*;
public class Bytepgm
{
public static void main(String arg[]) throws IOException
{
DataInputStream da=new DataInputStream(System.in);
29
INPUT / OUTPUT
30
CHARACTER STREAMS
AIM
Packages
java, io
Classes
DataInputStream, String, FileReader, FileWriter
Methods
readLine(), read(), write(), close()
ALGORITHM
PROGRAM
import java.io.*;
class characterpgm
{
public static void main(String arg[]) throws IOException
{
DataInputStream da=new DataInputStream(System.in);
int c;
while((c=fr.read())!=-1)
{
fw.write(c);
}
fr.close();
fw.close();
31
}
}
INPUT / OUTPUT
32