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 [Link] package.
10. Develop a program for Character Streams using [Link] package.
1
DEFAULT CONSTRUCTOR AND PARAMETERIZED CONSTRUCTOR
AIM
To write program JAVA program that implements the concept of Default Constructor and
Parameterized Constructor.
PACKAGES | CLASSES |METHOD USED
Packages
java, io
Classes
DataInputStream, Integer
Methods
parseInt(), readLine (), toUpperCase(),equals(), charAt(), substring()
ALGORITHM
Step 1: Import the essential package for implementing.
Step 2: Initialize all the variables needed to execute the process.
Step 3: Read the String from the user for Manipulation using Pre built class and methods.
Step 4: Default Constuctor is used to find the length of the String.
Step 5: Single Parameterized Constructor is initialized to convert the String to lowercase.
Step 6: Constructor with two Parameters is invoked String comparison.
Step 7: Three Parameterized Constructor is called to extract the specific sequence of
Characters from the given String.
Step 8: Display all the Manipulated String.
PROGRAM
[Link]
import [Link].*;
class Strex
{
Strex(String s1)
{
[Link]("\n Upper case:"+ [Link]());
}
Strex(String s1,String s2)
{
[Link]("String1 Equals String? :"+ [Link](s2));
}
Strex(String s3,int n)
{
[Link]("Character at the particular index is:"+ [Link](n));
2
}
Strex(String s4,int n1,int n2)
{
[Link]("Substring of the given string is:"+ [Link](n1,n2));
}
Strex()throws IOException
{
DataInputStream abc=new DataInputStream([Link]);
[Link]("Enter the string to find length");
String s=[Link]();
[Link]("Length of the string:"+ [Link]());
}
public static void main(String a[])throws IOException
{
Strex s=new Strex();
DataInputStream abc=new DataInputStream([Link]);
[Link]("\nEnter the string to convert to uppercase:");
String name=[Link]();
Strex str1=new Strex(name);
[Link]();
[Link]("Enter two strings to compare:");
[Link]("String1:");
String name2=[Link]();
[Link]("String2:");
String name3=[Link]();
[Link]();
Strex str2=new Strex(name2,name3);
[Link]();
[Link]("Enter the string for charAt operation:");
String name4=[Link]();
[Link]("Enter the index value:");
int n=[Link]([Link]());
[Link]();
Strex str3=new Strex(name4,n);
[Link]();
[Link]("Enter the string to perform substring:");
String name5=[Link]();
[Link]("Enter the startingposition:");
int n1=[Link]([Link]());
[Link]("Enter the Endingposition:");
int n2=[Link]([Link]());
[Link]();
Strex str4=new Strex(name5,n1,n2);
}
}
3
INPUT / OUTPUT
4
INHERITANCE AND SUPER KEYWORD
AIM
To write program JAVA program that implements the concept of Inheritance and super
keyword.
PACKAGES | CLASSES |METHOD USED
Packages
java, io
Classes
DataInputStream
Methods
readLine ()
ALGORITHM
Step 1: Import the essential package for implementing.
Step 2: Initialize all the variables needed to execute the process.
Step 3: Get the details of the Strudent such as name, regno and marks.
Step 4: Inherit the Variables and Methods from the Base Class.
Step 5: Calculate the Result with marks in the Derived Class.
Step 6: Display the Result using super keyword.
PROGRAM
import [Link].*;
class student
{
student()
{
[Link]("\n**************************************************************");
[Link]("\n********* [Link] college of Arts and Science *********");
}
void disp()
{
[Link]("\n********* Department of MCA **********");
[Link]("\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()
{
[Link]();
[Link]("\n********** Regno:"+reno+" "+ "Name:"+ne+" "+"
*****************");
if((mk1>=50)&&(mk2>=50)&&(mk3>=50))
[Link]("\n********* Reslult:Pass ********************");
else
[Link]("\n********* Reslult:Fail **********************");
[Link]("\n********* Percentage:"+per+" **********************");
[Link]("\n**************************************************************");
}
}
class inheritance
{
public static void main(String ar[]) throws IOException
{
DataInputStream da=new DataInputStream([Link]);
String regno,name;
int m1,m2,m3;
[Link]("Enter the Regno:");
regno=[Link]();
[Link]("Enter your name:");
name=[Link]();
[Link]("Enter the mark1:");
m1=[Link]([Link]());
[Link]("Enter the mark2:");
m2=[Link]([Link]());
[Link]("Enter the mark3:");
m3=[Link]([Link]());
mca mc=new mca(regno,name,m1,m2,m3);
[Link]();
}
}
INPUT / OUTPUT
6
OVERRIDING
AIM
7
To write program JAVA program that implements the concept of Method
Overriding.
PACKAGES | CLASSES |METHOD - USED
Packages
java, io
Classes
DataInputStream
Methods
readLine ()
ALGORITHM
Step 1: Import the essential package for implementing.
Step 2: Initialize all the variables needed to execute the process.
Step 3: Declare the same method both in super and sub class.
Step 4: Perform the needed operation in the Overriding method.
Step 5: Call the Overrided method.
Step 6: Display the Result.
PROGRAM
[Link]
import [Link].*;
class prod
{
void disp()
{
[Link]("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()
{
[Link]("\n\t\t------------Welcome to purchase world------------");
[Link]();
[Link]("\n\n\nProduct num\tProduct name\tprice\n");
[Link]("_____________\t____________\t_____\n\n");
for(int i=0;i<=9;i++)
{
[Link](pno[i]+"\t\t");
8
[Link](pro[i]+"\t\t");
[Link](pri[i]+"\n");
}
}
}
class Product
{
public static void main(String a[])throws IOException
{
product1 p=new product1();
[Link]();
DataInputStream ab=new DataInputStream([Link]);
[Link]("\nEnter the product number available in the list:");
int pno=[Link]([Link]());
[Link]("Enter the quantity:");
int qty=[Link]([Link]());
for(int i=0;i<9;i++)
{
if(pno==[Link][i])
{
[Link]("\n\nProduct is:\t"+[Link][i]+"\nPrice is:\t" +
[Link][i]*qty);
}
}
[Link]("\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 | CLASSES |METHOD USED
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
[Link]
import [Link].*;
import [Link];
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([Link]);
[Link]("\tPayroll requirement");
[Link]("\t~~~~~~~~~~~~~~~~~");
[Link]("*********************************");
[Link]("Enter Empolyee_ID:");
int id=[Link]([Link]());
[Link]("Enter Empolyee Name:");
String name=[Link]();
[Link]("Enter Designation:");
String des=[Link]();
[Link]("Enter Basic pay:");
int bp=[Link]([Link]());
[Link]("Enter House Rent Allowance:");
int hra=[Link]([Link]());
[Link]("Enter dearance Allowance:");
int da=[Link]([Link]());
[Link]("Enter Travelling Allowance:");
int ta=[Link]([Link]());
[Link]("******************************");
[Link]("\tPAYROLL SYSTEM");
[Link]("\t***************************");
[Link]("Employee ID:"+id);
[Link]("Employee Name:"+name);
[Link]("Designation is:"+des);
gpay g=new gpay();
int pf=[Link](des);
int gspy=[Link](bp,hra,da,ta);
netpy n=new netpy();
int np=[Link](gspy,pf);
[Link](np);
}
}
12
[Link]
package netpay;
public class netpy
{
public int npay(int gspy,int pf)
{
int np;
np=gspy-pf;
[Link]("Net pay:"+np);
return np;
}
public void tax(int np)
{
int ta=0;
if(np<=204000)
[Link]("Tax is:No Tax");
else if((np>=200001)&&(200001<=300000))
{
ta=np*10/100;
[Link]("Tax Amount:"+ta);
}
else if((np>=300001)&&(300001<=400000))
{
ta=np*20/100;
[Link]("Tax Amount:"+ta);
}
else if(np>=400001)
{
ta=np*30/100;
[Link]("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 | CLASSES |METHOD USED
Packages
java, io
Classes
DataInputStream, Integer, Float
Methods
parseInt(), readLine (), parseFloat()
ALGORITHM
Step 1: Import the essential packages.
Step 2: Declare the variables and fix some minimum amount for balance by default.
Step 3: Get the choice from the user for Bank Transaction.
Step 4: As per the user choice debit or credit the amount from the account.
Step 5: Call the user defined exception for displaying the low balance while withdrawing.
PROGRAM
[Link]
import [Link].*;
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([Link]);
int ch=1;
String s;
do
{
[Link]("\[Link]\[Link]\[Link] enquiry\[Link]");
[Link]("Enter your chioce:");
15
ch=[Link]([Link]());
switch(ch)
{
case 1:
{
[Link]("Enter the amount to withdraw:");
wd=[Link]([Link]());
if(bal>wd)
{
bal=bal-wd;
[Link]("Available balance is:"+bal);
}
else
{
NoBalanceException n=new NoBalanceException
("Sorry!!!Insufficiant balance");
throw n;
}
break;
}
case 2:
{
[Link]("Enter the amount to deposit:");
dp=[Link]([Link]());
bal=bal+dp;
[Link]("New balance is:"+bal);
break;
}
case 3:
{
[Link]("Available balance in your account is:"+bal);
break;
}
case 4:
{
break;
}
}
[Link]("Do you want to do another transaction(y/n):");
s=[Link]();
}while(s=="y");
[Link]("THANK YOU!!!!!!!!!");
}
}
INPUT / OUTPUT
16
AWT AND GRAPHICS
17
AIM
To write program JAVA program that embeds the Applet and Graphics.
PACKAGES | CLASSES |METHOD USED
Packages
java, applet, awt
Classes
Applet, Graphics, Font
Methods
paint(), setColor(), fillOval(), drawstring(), drawLine(), drawOval(), setFont()
ALGORITHM
Step 1: Import the essential packages.
Step 2: Define a class and extend it to Applet class.
Step 3: Using the methods available draw the wall clock inside paint method..
Step 4: Include the HTML tag <applet></applet> at the end to execute in Applet Window.
Step 5: Compile the Program as usual with javac tool.
Step 6: Execute it with appletviewer tool.
PROGRAM
[Link]
import [Link].*;
import [Link].*;
public class wallclock extends Applet
{
public void paint(Graphics g)
{
[Link]([Link]); [Link](400,300,400,400);
[Link]([Link]); [Link]("12",575,340);
[Link]([Link]); [Link]("1",650,360);
[Link]([Link]); [Link]("2",720,430);
[Link]([Link]); [Link]("3",750,500);
[Link]([Link]); [Link]("4",755,580);
[Link]([Link]); [Link]("5",700,650);
[Link]([Link]); [Link]("6",600,675);
[Link]([Link]); [Link]("7",500,665);
[Link]([Link]); [Link]("8",451,600);
[Link]([Link]); [Link]("9",425,520);
[Link]([Link]); [Link]("10",425,440);
[Link]([Link]); [Link]("11",483,360);
[Link]([Link]); [Link](590,500,10,10);
18
[Link](590,500,660,600);
[Link](590,500,740,500);
[Link]([Link]); [Link](355,250,500,500);
[Link]([Link]); [Link](465,305,20,20);
[Link]([Link]); [Link](490,290,20,20);
[Link]([Link]); [Link](520,280,20,20);
[Link]([Link]); [Link](550,270,20,20);
[Link]([Link]); [Link](580,265,20,20);
[Link]([Link]); [Link](610,265,20,20);
[Link]([Link]); [Link](640,270,20,20);
[Link]([Link]); [Link](670,280,20,20);
[Link]([Link]); [Link](700,295,20,20);
[Link]([Link]); [Link](730,315,20,20);
[Link]([Link]); [Link](755,335,20,20);
[Link]([Link]); [Link](775,365,20,20);
[Link]([Link]); [Link](790,395,20,20);
[Link]([Link]); [Link](805,425,20,20);
[Link]([Link]); [Link](815,455,20,20);
[Link]([Link]); [Link](818,490,20,20);
[Link]([Link]); [Link](818,520,20,20);
[Link]([Link]); [Link](815,550,20,20);
[Link]([Link]); [Link](808,580,20,20);
[Link]([Link]); [Link](790,610,20,20);
[Link]([Link]); [Link](771,640,20,20);
[Link]([Link]); [Link](741,670,20,20);
[Link]([Link]); [Link](700,695,20,20);
[Link]([Link]); [Link](660,715,20,20);
[Link]([Link]); [Link](620,720,20,20);
[Link]([Link]); [Link](580,720,20,20);
[Link]([Link]); [Link](540,717,20,20);
[Link]([Link]); [Link](500,700,20,20);
[Link]([Link]); [Link] val(465,680,20,20);
[Link]([Link]); [Link](430,655,20,20);
[Link]([Link]); [Link](403,615,20,20);
[Link]([Link]); [Link](385,585,20,20);
[Link]([Link]); [Link](370,555,20,20);
[Link]([Link]); [Link](365,515,20,20);
[Link]([Link]); [Link](362,475,20,20);
[Link]([Link]); [Link](369,435,20,20);
[Link]([Link]); [Link](379,400,20,20);
[Link]([Link]); [Link](405,360,20,20);
[Link]([Link]); [Link](428,330,20,20);
[Link]([Link]);
Font f=new Font("Arial",[Link],30);
[Link](f);
[Link]("SONY",560,420);
}
19
}
/*
<applet code="[Link]" width=300 height=500></applet>
*/
INPUT / OUTPUT
AWT WITH EVENT HANDLING
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
Step 1: Import the essential packages.
Step 2: Define a class and extend it to Applet class and implement Listener.
Step 3: Include the Choice Control and add items to get the choice of the user.
Step 4: Using the methods available draw the face.
Step 5: As per the user choice with repaint method generate the event change smiley.
Step 5: Include the HTML tag <applet></applet> at the end to execute in Applet Window.
Step 6: Compile the Program as usual with javac tool.
Step 7: Execute it with appletviewer tool.
PROGRAM
import [Link].*;
import [Link].*;
import [Link].*;
public class smiley extends Applet implements ItemListener
{
Font f;
Choice ch;
String opt;
int x,y,z,h;
public void init()
{
ch = new Choice();
[Link]("smile");
[Link]("cry");
[Link]("normal");
add(ch);
[Link](this);
}
public void itemStateChanged(ItemEvent ie)
{
opt = [Link]();
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([Link]);
f=new Font("Imprint MT Shadow",[Link],30);
[Link](f);
[Link]([Link]);
[Link](250,250,150,100);
[Link]([Link]);
[Link](300,280,10,15);
[Link](340,280,10,15);
[Link](320,300,1,10);
[Link](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 | CLASSES |METHOD USED
Packages
java, io, util
Classes
DataInputStream, String, ArrayList, Integer
Methods
parseInt(), readLine(), add(), remove(), contains(), equals()
ALGORITHM
Step 1: Import the essential packages.
Step 2: Declare the ArrayList Classes of List Inteface.
Step 3: Get the choice from the user to access the item in ArrayList.
Step 4: As per the Choice, add an Item, add at specified index, remove and search an Item.
Step 5: Display the result.
PROGRAM
import [Link].*;
import [Link].*;
class collectionprg
{
public static void main(String ar[]) throws IOException
{
DataInputStream dis = new DataInputStream([Link]);
int ch;
String item,opt;
ArrayList al = new ArrayList();
do
{
[Link]("[Link] an Item");
[Link]("[Link] in index");
[Link]("[Link] an Item");
[Link]("[Link] an Item");
[Link]("What is ur choice");
ch = [Link]([Link]());
25
switch(ch)
{
case 1:
[Link]("Enter the item to add");
item = [Link]();
[Link](item);
break;
case 2:
[Link]("Enter the item to add");
item = [Link]();
[Link]("Enter the index to add");
int ind = [Link]([Link]());
[Link](ind,item);
break;
case 3:
[Link]("Enter the item to remove");
item = [Link]();
[Link](item);
break;
case 4:
[Link]("Enter the item to search");
item = [Link]();
if ([Link](item))
{
[Link]("Available");
}
else
{
[Link]("Not Available");
}
break;
}
[Link]("Items are" + al);
[Link]("Want to continue(Y|N)");
opt = [Link]();
}while([Link]("y"));
}
}
INPUT / OUTPUT
26
27
28
BYTE STREAMS
AIM
To write a JAVA program to implement the concept of Byte Streams.
PACKAGES | CLASSES |METHOD USED
Packages
java, io
Classes
DataInputStream, String, FileInputStream, FileOutputStream
Methods
readLine(), read(), write(), close()
ALGORITHM
Step 1: Import the essential packages.
Step 2: Get the file name from the user to copy the content.
Step 3: Get the destination file to place the copied content.
Step 4: Read the content from the source and copy it to destination till end of file is reached.
Step 5: Display the content in the Destination file.
PROGRAM
import [Link].*;
public class Bytepgm
{
public static void main(String arg[]) throws IOException
{
DataInputStream da=new DataInputStream([Link]);
[Link]("Enter the Source File:");
String src=[Link]();
[Link]("Enter the destination file:");
String dest=[Link]();
FileInputStream fin=new FileInputStream(src);
FileOutputStream fout=new FileOutputStream(dest);
int c;
while((c=[Link]())!=-1)
{
[Link](c);
}
[Link]();
[Link]();
}
}
29
INPUT / OUTPUT
30
CHARACTER STREAMS
AIM
To write a JAVA program to implement the concept of Character Streams.
PACKAGES | CLASSES |METHOD USED
Packages
java, io
Classes
DataInputStream, String, FileReader, FileWriter
Methods
readLine(), read(), write(), close()
ALGORITHM
Step 1: Import the essential packages.
Step 2: Get the file name from the user to copy the content character by character.
Step 3: Get the destination file to place the copied content.
Step 4: Read the content from the source and copy it to destination till end of file is reached.
Step 5: Display the content in the Destination file.
PROGRAM
import [Link].*;
class characterpgm
{
public static void main(String arg[]) throws IOException
{
DataInputStream da=new DataInputStream([Link]);
[Link]("Enter the Source File:");
String src=[Link]();
[Link]("Enter the destination file:");
String dest=[Link]();
FileReader fr=new FileReader(src);
FileWriter fw=new FileWriter(dest);
int c;
while((c=[Link]())!=-1)
{
[Link](c);
}
[Link]();
[Link]();
31
}
}
INPUT / OUTPUT
32