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 | 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
Strex.java
import java.io.*;
class Strex
{
Strex(String s1)
{
System.out.println("\n Upper case:"+ s1.toUpperCase());
}
Strex(String s1,String s2)
{
System.out.println("String1 Equals String? :"+ s1.equals(s2));
}
Strex(String s3,int n)
{
System.out.println("Character at the particular index is:"+ s3.charAt(n));
2
}
Strex(String s4,int n1,int n2)
{
System.out.println("Substring of the given string is:"+ s4.substring(n1,n2));
}
Strex()throws IOException
{
DataInputStream abc=new DataInputStream(System.in);
System.out.println("Enter the string to find length");
String s=abc.readLine();
System.out.println("Length of the string:"+ s.length());
}
public static void main(String a[])throws IOException
{
Strex s=new Strex();
DataInputStream abc=new DataInputStream(System.in);
System.out.println("\nEnter the string to convert to uppercase:");
String name=abc.readLine();
Strex str1=new Strex(name);
System.out.println();
System.out.println("Enter two strings to compare:");
System.out.println("String1:");
String name2=abc.readLine();
System.out.println("String2:");
String name3=abc.readLine();
System.out.println();
Strex str2=new Strex(name2,name3);
System.out.println();
System.out.println("Enter the string for charAt operation:");
String name4=abc.readLine();
System.out.println("Enter the index value:");
int n=Integer.parseInt(abc.readLine());
System.out.println();
Strex str3=new Strex(name4,n);
System.out.println();
System.out.println("Enter the string to perform substring:");
String name5=abc.readLine();
System.out.println("Enter the startingposition:");
int n1=Integer.parseInt(abc.readLine());
System.out.println("Enter the Endingposition:");
int n2=Integer.parseInt(abc.readLine());
System.out.println();
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 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;
System.out.println("Enter the Regno:");
regno=da.readLine();
System.out.println("Enter your name:");
name=da.readLine();
System.out.println("Enter the mark1:");
m1=Integer.parseInt(da.readLine());
System.out.println("Enter the mark2:");
m2=Integer.parseInt(da.readLine());
System.out.println("Enter the mark3:");
m3=Integer.parseInt(da.readLine());
mca mc=new mca(regno,name,m1,m2,m3);
mc.disp();
}
}
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
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 | 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
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 Empolyee Name:");
String name=dai.readLine();
System.out.println("Enter Designation:");
String des=dai.readLine();
System.out.println("Enter Basic pay:");
int bp=Integer.parseInt(dai.readLine());
System.out.println("Enter House Rent Allowance:");
int hra=Integer.parseInt(dai.readLine());
System.out.println("Enter dearance Allowance:");
int da=Integer.parseInt(dai.readLine());
System.out.println("Enter Travelling Allowance:");
int ta=Integer.parseInt(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);
gpay g=new gpay();
int pf=g.pfcalc(des);
int gspy=g.grossp(bp,hra,da,ta);
netpy n=new netpy();
int np=n.npay(gspy,pf);
n.tax(np);
}
}
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 | 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
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 | 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
wallclock.java
import java.applet.*;
import java.awt.*;
public class wallclock extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.pink); g.fillOval(400,300,400,400);
g.setColor(Color.black); g.drawString("12",575,340);
g.setColor(Color.black); g.drawString("1",650,360);
g.setColor(Color.black); g.drawString("2",720,430);
g.setColor(Color.black); g.drawString("3",750,500);
g.setColor(Color.black); g.drawString("4",755,580);
g.setColor(Color.black); g.drawString("5",700,650);
g.setColor(Color.black); g.drawString("6",600,675);
g.setColor(Color.black); g.drawString("7",500,665);
g.setColor(Color.black); g.drawString("8",451,600);
g.setColor(Color.black); g.drawString("9",425,520);
g.setColor(Color.black); g.drawString("10",425,440);
g.setColor(Color.black); g.drawString("11",483,360);
g.setColor(Color.black); g.fillOval(590,500,10,10);
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
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 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;
public void init()
{
ch = new Choice();
ch.add("smile");
ch.add("cry");
ch.add("normal");
add(ch);
ch.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
opt = ch.getSelectedItem();
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 | 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 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;
ArrayList al = new ArrayList();
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
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 java.io.*;
public class Bytepgm
{
public static void main(String arg[]) throws IOException
{
DataInputStream da=new DataInputStream(System.in);
System.out.println("Enter the Source File:");
String src=da.readLine();
System.out.println("Enter the destination file:");
String dest=da.readLine();
FileInputStream fin=new FileInputStream(src);
FileOutputStream fout=new FileOutputStream(dest);
int c;
while((c=fin.read())!=-1)
{
fout.write(c);
}
fin.close();
fout.close();
}
}
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 java.io.*;
class characterpgm
{
public static void main(String arg[]) throws IOException
{
DataInputStream da=new DataInputStream(System.in);
System.out.println("Enter the Source File:");
String src=da.readLine();
System.out.println("Enter the destination file:");
String dest=da.readLine();
FileReader fr=new FileReader(src);
FileWriter fw=new FileWriter(dest);
int c;
while((c=fr.read())!=-1)
{
fw.write(c);
}
fr.close();
fw.close();
31
}
}
INPUT / OUTPUT
32