Java Practical Slip Solutions
Slip no 1
Write a java program to read the characters from a file, if a character
is alphabet then reverse its case, if not then display its category on the
Screen. (Whether it is Digit or
Space)
import java.io.*;
class Slip1
{
public static void main(String args[])throws Exception
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter file Name");
String f1=br.readLine();
FileReaderfr=new FileReader(f1);
intch;
while((ch=fr.read())!=-1)
{
char c=(char)ch;
if(Character.isUpperCase(c))
{
c=Character.toLowerCase(c);
System.out.print(c);
}
else if(Character.isLowerCase(c))
{
c=Character.toUpperCase(c);
System.out.print(c);
}
else if(Character.isDigit(c))
{
System.out.print(c+"Digit");
}
else if(Character.isSpaceChar(c))
System.out.print(c+"Space");
else
System.out.print(c);
}
fr.close();
}
}
Output:
Enter file Name
testslip1.txt
mYSpacenAMESpaceISSpaceaPARNA
rOLLSpaceNO: Space2Digit0Digit1Digit1Digit
Slip no 2
Write a java program to accept n names of cites from user and display
them in descending order.
class Slip2
{
public static void main(String args[])
{
int n=args.length;
inti,j;
String str[]=new String[n];
for(i=0;i<n;i++)
{
str[i]=args[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((str[i].compareTo(str[j]))>0)
{
String tmp=str[j];
str[j]=str[i];
str[i]=tmp;
}
}
}
System.out.println("City Name After Sorting");
for(i=0;i<n;i++)
{
System.out.println(str[i]);
}
}
}
Output:
Pune Bhosari Mumbai Chakan Nashik Thane Ahemadnagar
City Name After Sorting
Ahemadnagar
Bhosari
Chakan
Mumbai
Nashik
Pune
Thane
Slip no 5
Define a class Student with attributes rollno and name. Define default
and parameterized constructor. Override the toString() method. Keep the count
of Objects created.
Create objects using parameterized constructor and Display the object
count after each object is created
importjava.util.*;
class Slip5
{
staticintcnt;
int roll;
String nm;
Slip5()
{
}
Slip5(intrno,String name)
{
roll=rno;
nm=name;
cnt++;
System.out.println("Objects created="+cnt);
}
public String toString()
{
return "rno="+roll+" name of student="+nm;
}
public static void main(String a[])
{
intn,i,rno;
String name;
System.out.println("Enter no of students");
Scanner s=new Scanner(System.in);
n=s.nextInt();
Slip5 ob[]=new Slip5[n];
for(i=0;i<n;i++)
{
System.out.println("Enter roll no");
rno=s.nextInt();
System.out.println("Enter name");
name=s.next();
ob[i]=new Slip5(rno,name);
}
System.out.println("Students are: ");
for(i=0;i<n;i++)
{
System.out.println(ob[i]);
}
}
}
Output:
Enter no of students
3
Enter roll no
11
Enter name
Geetanjali Gautam
Objects created=1
Enter roll no
12
Enter name
Aparna Joshi
Objects created=2
Enter roll no
13
Enter name
Rohini Kulkarni
Objects created=3
Students are:
rno=11 name of student=Geetanjali
rno=12 name of student=Aparna Joshi
rno=13 name of student=Rohini Kulkarni
Slip no 9
Write a java program to display the contents of a file in reverse order.
import java.io.*;
class Slip9
{
public static void main(String a[]) throws
Exception
{
BufferedReaderbr = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter file name");
String fname=br.readLine();
FileInputStream fin = new FileInputStream(fname);
BufferedInputStreambis = new
BufferedInputStream(fin);
int s = bis.available();
System.out.println(s);
for(inti=s-1;i>=0;i--)
{ bis.mark(i);
bis.skip(i);
System.out.print((char)bis.read());
bis.reset();
}
fin.close();
}
}
Output
Enter file name
slip7.java
563
}
}
;)(7pilS wen
{
)][sgragnirtS(niamdiovcitatscilbup
}
;)eurt(elbisiVtes
;))(tuoyaLwolF wen(tuoyaLtes
;)004,004(eziStes
;)EULB.roloC(dnuorgkcaBtes
;)l(dda
;)DER.roloC(dnuorgeroFtes.l
;))42,DLOB.tnoF,"aigroeG"(tnoF
wen(tnoFtes.l
;)"avajolleH"(lebaL wen=l
{
)(7pilS
;llebaL
{
emarFsdnetxe 7pilS ssalc
;*.twa.avajtropmi
/*
.emarFeht no eulB ?rolocdnuorgkcab ,
,aigroeG -tnoFsgnitteshtiw ?avaJolleH? yalps
nemngissA
.ijanaTinihoRtuaR:emaN */
Slip no 11
Write a java program to accept Employee name from the user and check
whether it is valid or not.
If it is not valid then throw user defined Exception “Name is Invalid”
otherwise display it.(Name should contain only characters)
import java.lang.Exception;
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}
class Slipno11
{
public static void main(String args[])
{
String myName;
System.out.println(validateFirstName("Aparna"));
System.out.println(validateMiddleName("123"));
System.out.println(validateLastName("Lohakare"));
}
public static boolean validateFirstName(String firstname)
{
return firstname.matches("[A-Z][a-z]*");
}
public static boolean validateMiddleName(String middlename)
{
return middlename.matches("[A-Z][a-z]*");
}
public static boolean validateLastName(String lastname)
{
return lastname.matches("[A-Z][a-z]*");
}
}
Output:
C:\Aparna>java Slipno11
true
false
true
Slip no 15
Define an abstract class Shape with abstract method area() and
volume().Write a java program to calculate area and volume of Cone and
Cylinder.
abstract class Shape
{
float pi=3.14F;
abstract float areaofcone(float radius,float side);
abstract float volumeofcone(float radius,float height);
abstract float areaofcylinder(float radius,float height);
abstract float volumeofcylinder(float radius,float height);
}
class AreaVolume extends Shape
{
float areaofcone(float r,float s)
{
return pi*r*s;
}
float volumeofcone(float r,float h)
{
return (1/3)*pi*r*r*h;
}
float areaofcylinder(float r,float h)
{
return 2*pi*r*h;
}
float volumeofcylinder(float r,float h)
{
return pi*r*r*h;
}
}
class Slip15
{
public static void main(String args[])
{
Shape obj=new AreaVolume();
float x=obj.areaofcone(1.2F,3.2F);
System.out.println("Area of Cone="+x);
float y=obj.volumeofcone(1.2F,3.2F);
System.out.println("Volume of Cone="+y);
float z=obj.areaofcylinder(1.2F,3.2F);
System.out.println("area of cylinder="+z);
float p=obj.volumeofcylinder(1.2F,3.2F);
System.out.println("volume of cylinder="+p);
}
}
Output:
Area of Cone=12.057601
Volume of Cone=0.0
area of cylinder=24.115202
volume of cylinder=14.469123
Write a java program that displays the number of characters,lines
&words from a file.
import java.io.*;
class Slip18
{
public static int words=0;
public static int lines=0;
public static int chars=0;
public static void wc(Reader r)throws IOException
{
StreamTokenizer tok=new StreamTokenizer(r);
tok.resetSyntax();
tok.wordChars(33,255);
tok.whitespaceChars(0,' ');
tok.eolIsSignificant(true);
while(tok.nextToken()!=tok.TT_EOF)
{
switch(tok.ttype)
{
case StreamTokenizer.TT_EOL:
lines++;
chars++;
break;
case StreamTokenizer.TT_WORD:
words++;
default:
chars+=tok.sval.length();
break;
}
}
}
public static void main(String args[])
{
if(args.length==0)
{
try
{
wc(new InputStreamReader(System.in));
System.out.println(lines+" "+words+" "+chars);
}catch(IOException e){ }
}
else
{
int twords=0,tchars=0,tlines=0;
for(int i=0;i<args.length;i++)
{
try
{
words=chars=lines=0;
wc(new FileReader(args[i]));
twords+=words;
tchars+=chars;
tlines+=lines;
System.out.println(args[i]+": "+lines+" "+words+"
"+chars);
}catch(IOException e){
System.out.println(args[i]+": error.");
}
}
System.out.println("total: "+tlines+" "+twords+"
"+tchars);
}
}
}
//Testslip18.txt
My name is Aparna Kulkarni
Roll no is 1234
collage name is Siddhant collage of management studies
Output:
Testslip18.txt: 3 16 76
total: 3 16 76
Slip no 22
Define an
Interface Shape with abstract method area().Write a java program to calculate
an area of circle and sphere.(Use final keyword).
interface Shape
{
final float pi=3.11416f;
public float areaOfCircle(float radius);
public float areaOfSphere(float
radius);
}
class AreaOfCirSphere implements Shape
{
public float areaOfCircle(float r)
{
return pi*r*r;
}
public float areaOfSphere(float r)
{
return (4/3)*pi*r*r;
}
}
class Slip22{
public static void main(String args[])
{
Shape area;
AreaOfCirSphere ob=new AreaOfCirSphere();
area=ob;
float areaOfCir=area.areaOfCircle(5.3F);
System.out.println("Area of Circle="+areaOfCir);
float areaOfSphere=area.areaOfSphere(3.2F);
System.out.println("Area of Sphere="+areaOfSphere);
}
}
Output:
Area of Circle=87.47676
Area of Sphere=31.888998
Slip no 23
Write a java
program to accept to details of n Players from user(Player
code,name,runs,innings-played and number of times not out).The program should
contain following menus:
-Display
average runs of a single player.
-Display
average runs of all players.(Use array of object,Method overloading and static
keyword)
import java.util.*;
import java.io.*;
class player
{
String name;
int trun,tno,ip,pcode;
float avg;
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void getdata()
{
try
{
System.out.println("\nEnter Player Code: ");
pcode=Integer.parseInt(br.readLine());
System.out.println("Enter Player name: ");
name=br.readLine();
System.out.println("Enter Total Runs: ");
trun=Integer.parseInt(br.readLine());
System.out.println("Enter Times Not Out: ");
tno=Integer.parseInt(br.readLine());
System.out.println("Enter Innings Played: ");
ip=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
}
void putdata()
{
System.out.println(pcode + "\t" + name + "\t" + trun +
"\t" + tno + "\t" + ip + "\t" + avg);
}
void getavg()
{
avg=trun/(ip-tno);
}
static void getavg(player p[],int n)
{
for(int i=0;i<n;i++)
{
p[i].avg=p[i].trun/(p[i].ip-p[i].tno);
p[i].putdata();
}
}
}
public class Slip23
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])
{
int n=0,ch;
player p[]=null;
try
{
do
{
System.out.println("\n\n1.Enter details of players. ");
System.out.println("2.Display average runs of a single player ");
System.out.println("3.Display average runs of all player ");
System.out.println("4.Exit ");
System.out.println("Enter your choice: ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1 :
System.out.print("Enter No. of Players: ");
n=Integer.parseInt(br.readLine());
p=new player[n];
for(int i=0;i<n;i++)
{
p[i]=new player();
p[i].getdata();
}
break;
case 2:
System.out.println("Enter Player Code For Avg Calculation :");
int m=Integer.parseInt(br.readLine());
boolean flag=false;
for(int i=0;i<n;i++)
{
if(p[i].pcode==m)
{
flag=true;
p[i].getavg();
p[i].putdata();
}
}
if(!flag)
System.out.println("Player not found");
break;
case 3:
System.out.println("Average of All the Players");
player.getavg(p,n);
break;
case 4:
break;
default:
System.out.println("Wrong Input (Try again)");
}
}while(ch!=4);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
1.Enter details of players.
2.Display average runs of a single player
3.Display average runs of all player
4.Exit
Enter your choice:
1
Enter No. of Players: 2
Enter Player Code:
101
Enter Player name:
Aparna
Enter Total Runs:
122
Enter Times Not Out:
2
Enter Innings Played:
1
Enter Player Code:
102
Enter Player name:
Geeta
Enter Total Runs:
100
Enter Times Not Out:
3
Enter Innings Played:
1
1.Enter details of players.
2.Display average runs of a single player
3.Display average runs of all player
4.Exit
Enter your choice:
2
Enter Player Code For Avg Calculation :
102
102 Geeta
100 3
1 -50.0
1.Enter details of players.
2.Display average runs of a single player
3.Display average runs of all player
4.Exit
Enter your choice:
3
Average of All the Players
101 Aparna 122
2 1
-122.0
102 Geeta
100 3
1 -50.0
1.Enter details of players.
2.Display average runs of a single player
3.Display average runs of all player
4.Exit
Enter your choice:
4
Slip no 25
Define an
Employee class with suitable attributes having getSalary() method, which
returns salary withdrawn by a particular employee. Write a class Manager which
extends a class Employee, override the getSalary() method,which will return
salary of manager by adding travelling allowance,house rent allowance etx.
import java.util.*;
import java.io.*;
class Emp
{
int e_no;
float bsal,gsal,tax ,pf,nsal,ded,da;
String e_name;
Emp(int n ,String en,float bs)
{
e_no=n;
e_name=en;
bsal=bs;
}
float getSalary()
{
da=bsal*0.10f;
gsal=bsal+da;
tax=bsal*0.05f;
pf=bsal*0.04f;
ded=tax+pf;
nsal=gsal-ded;
return(nsal);
}
void display()
{
System.out.println("EmpNo:" +e_no+"Name: "+e_name
+"Net Salary:" +nsal);
}
}
class Manager extends Emp
{
float hra,ta,mns;
Manager(int n,String en,float bs,float hr,float t)
{
super(n,en,bs);
hra=hr;
ta=t;
}
float getSalary()
{
float r=super.getSalary();
mns=r+hra+ta;
return(mns);
}
}
class Slip25
{
public static void main(String args[])
{
Manager ma=new Manager(01,"Aparna Lohakare",40000.0f,9200.0f,8420.0f);
float shb=ma.getSalary();
ma.display();
System.out.println("Salary of manager is\t" +shb);
}
}
Output:
EmpNo:1Name: Aparna LohakareNet Salary:40400.0
Salary of manager is 58020.0
Slip no 26
Write a java
Program to accept ‘n’ no’s through the command line and store all the prime
no’s and perfect no’s
into the
different arrays and display both the arrays.
import java.util.*;
import java.io.*;
class Slip26
{
public static void main(String args[])
{
int n=args.length;
int pri[]=new int[n];
int per[]=new int[n];
int k=0,i,j;
int sum=0;
//prime no
for(i=0;i<n;i++)
{
for(j=2;j<Integer.parseInt(args[i]);j++)
{
if(Integer.parseInt(args[i])%j==0)
break;
}
if(Integer.parseInt(args[i])==j)
{
pri[k++]=Integer.parseInt(args[i]);
}
}
System.out.println("Prime Array=");
for(j=0;j<k;j++)
{
System.out.print(" "+pri[j]);
}
//Perfect no
k=0;
for(i=0;i<n;i++)
{
sum=0;
for(j=1;j<Integer.parseInt(args[i]);j++)
{
if(Integer.parseInt(args[i])%j==0)
{
sum=sum+j;
}
}
if(sum==Integer.parseInt(args[i]))
{
per[k++]=sum;
}
}
System.out.println("\nPerfect Array=");
for(i=0;i<k;i++)
{
System.out.println(" "+per[i]);
}
}
}
Output:
C:\Aparna>javac Slip26.java
C:\Aparna>java Slip26 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20
Prime Array=
2 3 5 7 11 13 17 19
Perfect Array=
6
Slip no 28
Write a java
program to read n Students names from user,store them into the ArrayList
collection.The program should not allow duplicate names.Display the names in
Ascending order.
import java.util.*;
import java.io.*;
public class Slip28
{
public static void main(String args[])throws
Exception
{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
ArrayList a = new
ArrayList();
System.out.println("\nEnter number of Employee : ");
int n =
Integer.parseInt(br.readLine());
System.out.println("\nEnter name : ");
for(int i = 1; i <= n;
i++)
{
a.add(br.readLine());
}
TreeSet tr = new TreeSet(a);
System.out.println("Sorted Name
are : "+tr);
}
}
Output:
Enter number of Employee :
7
Enter name :
renuka
pratiksha
madhuri
aparna
geeta
renuka
rohini
Sorted Name are : [aparna, geeta, madhuri, pratiksha, renuka, rohini]
Slip No 29
Write a java
program to accept n employee names from user,store them into the linkedlist
class and Display them by using.
-Iterator
Interface
-ListIterator
Interface
import java.util.*;
import java.io.*;
public class Slip29
{
public static void main(String args[])throws
Exception
{
int n;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
LinkedList li = new
LinkedList ();
System.out.println("\nEnter number of Employee : ");
n =
Integer.parseInt(br.readLine());
System.out.println("\nEnter name : ");
for(int i = 1; i <= n;
i++)
{
li.add(br.readLine());
}
System.out.println("\nLink List Content : ");
Iterator it = li.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println("\nReverse
order : ");
ListIterator lt =
li.listIterator();
while(lt.hasNext())
{
lt.next();
}
while(lt.hasPrevious())
{
System.out.println(lt.previous());
}
}
}
Output:
Enter number of Employee
2
Enter name :
abc
pqr
Link List Content :
abc
pqr
Reverse order :
pqr
abc
Slip No 19
Write a java
program to accept a number from the user, if number is zero then throw user
defined Exception “Number is 0” otherwise calculate the sum of first and last
digit of a given number (Use static keyword).
import java.io.*;
class NumberZero extends Exception
{
NumberZero()
{}
}
class Number
{
static int no;
Number() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no");
no=Integer.parseInt(br.readLine());
try
{ if(no==0)
{
throw new NumberZero();
}
cal();
}//end of try
catch(NumberZero e)
{
System.out.println("no is zero");
}
}
void cal()
{ int l=0,r=0;
l =
no%10;
//System.out.println("no = "+no);
if(no>9)
{
while(no>0)
{
r = no%10;
no=no/10;
}
System.out.println("Addition of first and last digit = "+(l+r));
}
else
System.out.println("Addition of first and last digit = "+l);
}
}
class Slip19
{
public static void main(String a[])
throws IOException
{ Number n= new
Number();
}
}
Output
Enter no
1
Addition of first and last digit = 1
Enter no
45
Addition of first and last digit = 9
Enter no
456
Addition of first and last digit = 10
Slip no 4
Design a
screen in Java to handle the Mouse Events such as MOUSE_MOVED and MOUSE_CLICK
and display the position of the Mouse_Click in a TextField.
importjava.awt.*;
importjava.awt.event.*;
class Slip4 extends Frame
{
TextFieldstatusBar;
public static void main(String
[]args)
{
new Slip4().show();
}
Slip4()
{
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent
e)
{
statusBar.setText("Clicked at
(" + e.getX() + "," + e.getY() + ")");
repaint();
}
public void mouseEntered(MouseEvent
e)
{
statusBar.setText("Entered at
(" + e.getX() + "," + e.getY() + ")");
repaint();
}
}
);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent
e)
{
System.exit(0);
}
});
setLayout(new FlowLayout());
setSize(275,300);
setTitle("Mouse Click
Position");
statusBar = new TextField(20);
add(statusBar);
setVisible(true);
}
}
Slip no 6
.Create a calculator with functionality in an Applet.
importjava.awt.*;
importjava.applet.*;
importjava.awt.event.*;
//<applet code="Cal"
width=500 height=500></applet>
public class Cal extends Applet
implements ActionListener
{
inta,b,c;
TextField t1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
String s,s1,s2,s3,s4;
public void init()
{
setLayout(null);
t1=new TextField(10);
t1.setBounds(80,200,260,40);
add(t1);
b1=new Button("0");
b2=new Button("1");
b3=new Button("2");
b4=new Button("3");
b5=new Button("4");
b6=new Button("5");
b7=new Button("6");
b8=new Button("7");
b9=new Button("8");
b10=new Button("9");
b11=new Button("+");
b12=new Button("-");
b13=new Button("*");
b14=new Button("/");
b15=new Button("=");
b16=new Button("CLR");
b1.setBounds(90,260,40,30);
add(b1);
b2.setBounds(140,260,40,30);
add(b2);
b3.setBounds(190,260,40,30);
add(b3);
b4.setBounds(240,260,40,30);
add(b4);
b5.setBounds(290,260,40,30);
add(b5);
b6.setBounds(90,300,40,30);
add(b6);
b7.setBounds(140,300,40,30);
add(b7);
b8.setBounds(190,300,40,30);
add(b8);
b9.setBounds(240,300,40,30);
add(b9);
b10.setBounds(290,300,40,30);
add(b10);
b11.setBounds(90,340,40,30);
add(b11);
b12.setBounds(140,340,40,30);
add(b12);
b13.setBounds(190,340,40,30);
add(b13);
b14.setBounds(240,340,40,30);
add(b14);
b15.setBounds(290,340,40,30);
add(b15);
b16.setBounds(90,380,70,20);
add(b16);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
repaint();
s=ae.getActionCommand();
if(s.equals("0")||s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")||s.equals("5")||s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9"))
{
s1=t1.getText()+s;
t1.setText(s1);
}
if(s.equals("+"))
{
s2=t1.getText();
t1.setText("");
s3="+";
}
if(s.equals("-"))
{
s2=t1.getText();
t1.setText("");
s3="-";
}
if(s.equals("*"))
{
s2=t1.getText();
t1.setText("");
s3="*";
}
if(s.equals("/"))
{
s2=t1.getText();
t1.setText("");
s3="/";
}
if(s.equals("="))
{
s4=t1.getText();
a=Integer.parseInt(s2);
b=Integer.parseInt(s4);
if(s3.equals("+"))
c=a+b;
if(s3.equals("-"))
c=a-b;
if(s3.equals("*"))
c=a*b;
if(s3.equals("/"))
c=a/b;
t1.setText(String.valueOf(c));
}
if(s.equals("CLR"))
{
t1.setText("");
}
}
public void paint(Graphics g)
{
setBackground(Color.green);
g.drawRect(80,200,260,200);
showStatus("ASHUSOFTECH");
g.drawString("CALCULATER",200,50);
}
}
Slip no 7
Write a java
program to display “Hello Java” with settings Font- Georgia, Foreground color-
Red, background color – Blue on the Frame.
importjava.awt.*;
class Slip7 extends Frame
{
Label l1;
Slip7()
{
l1=new Label("Hello java");
l1.setFont(new Font("Georgia",Font.BOLD,24));
l1.setForeground(Color.RED);
add(l1);
setBackground(Color.BLUE);
setSize(300,300);
setLayout(new FlowLayout());
setVisible(true);
}
public static void main(String a[])
{
new Slip7();
}
}
Slip no 8
Write a java
program to design a following GUI (Use Swing).
importjava.awt.*;
importjavax.swing.*;
class Slip8 extends JFrame
{
JLabel l1,l2,l3,l4,l5,l6;
JTextField t1,t2,t3;
JTextArea t;
JPanel p,p1,p2,p3;
ButtonGroupbg;
JRadioButtonm,f;
JCheckBox c1,c2,c3;
JButton b1,b2;
Slip8()
{
p =new JPanel();
p1=new JPanel();
l1=new JLabel("First Name ");
l2=new JLabel("last Name ");
l3=new JLabel("Address ");
l4=new JLabel("mobile No ");
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
t=new JTextArea(2,10);
p.add(l1); p.add(t1);
p.add(l2); p.add(t2);
p.add(l3); p.add(t);
p.add(l4); p.add(t3);
p.setLayout(new GridLayout(4,2));
add(p);
l5=new JLabel("Gender ");
m = new JRadioButton("male");
f = new JRadioButton("female");
bg = new ButtonGroup();
bg.add(m);
bg.add(f);
p1.add(l5);
p1.add(m);
p1.add(f);
p1.setLayout(new GridLayout(1,3));
p2=new JPanel();
l6=new JLabel("Your Interests ");
c1=new JCheckBox("Computer");
c2=new JCheckBox("Sports");
c3=new JCheckBox("Music");
p2.add(l6);
p2.add(c1);
p2.add(c2);
p2.add(c3);
p2.setLayout(new GridLayout(1,4));
p3=new JPanel();
b1=new JButton("submit");
b2=new JButton("clear");
p3.add(b1);
p3.add(b2);
add(p);
add(p1);
add(p2);
add(p3);
setSize(300,400);
setLayout(new
FlowLayout(FlowLayout.LEFT));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String a[])
{
new Slip8();
}
}
Slip no 10
Write an
applet application in Java for designing Temple.
importjava.awt.*;
importjava.applet.*;
public class Slip10 extends Applet
{
public void paint(Graphics g)
{
g.drawRect(400,100,30,30);
g.drawLine(400,100,400,180 );
g.drawLine(400,180,480,250);
g.drawLine(400,180,320,250);
g.drawRect(320,250,160,160);
g.drawRect(390,350,20,60);
}
}
//Slip10.html
<html>
<head><title></title>
</head>
<body>
<Applet
code="Slip10.class"
width=150
height=500>
</Applet>
</body>
</html>
Slip no 14
Write a java
program which will create a frame if we try to close it, it should change it’s
color and it remains visible on the screen(Use swing).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Slip14 extends JFrame
{
JPanel
p = new JPanel();
Slip14()
{ setVisible(true);
setSize(400,400);
setTitle("Swing Background");
add(p);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ p.setBackground(Color.RED);
JOptionPane.showMessageDialog(null,"Close window","Login",JOptionPane.INFORMATION_MESSAGE);
}
});
}
public
static void main(String args[])
{
new Slip14();
Slip no 30
Write an
applet in Java for smile face
import java.awt.*;
import java.applet.*;
public class AFace extends Applet
{
public void paint(Graphics g)
{
Font f=new Font("Helvetice", Font.BOLD,20);
g.setFont(f);
g.drawString("Keep Smiling!!!", 50, 30);
g.drawOval(60,60,210,200);
g.drawOval(80,115,70,30);
g.drawOval(180,115,70,30);
g.fillOval(90,120,50,20);
g.fillOval(190,120,50,20);
g.drawLine(165,125,165,175);
g.drawArc(110,130,110,95,0,-180);
g.drawOval(50,120,10,50);
g.drawOval(270,120,10,50);
}
}
//Slip30.html
<html>
<head><title></title>
</head>
<body>
<Applet
code="AFace.class"
width=150
height=500>
</Applet>
</body>
</html>
Slip no 21
Write a Java
program to design a screen using Swing that will create four TextFields. First
for the text, second for what to find and third for replace. Display result in
the fourth TextField. Display the count of total no. of replacements
made. The button clear to clear the TextFields
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Slip21 extends JFrame
implements ActionListener
{
JLabel ltext, lfind, lreplace, loccurance;
JTextArea text;
JTextField findText, replaceText, occurrenceText;
JButton find, replace, clear;
JPanel pan1,pan2;
int occurrences=0,i=0;
Slip21()
{
ltext= new JLabel("Enter Text : ");
lfind = new JLabel("Text to Find : ");
lreplace = new JLabel("Text to Replace : ");
loccurance = new JLabel("No.of Occurrences : ");
text = new JTextArea(1,20);
findText =
new JTextField(20);
replaceText = new JTextField(20);
occurrenceText = new JTextField(20);
pan1 = new JPanel();
pan1.setLayout(new GridLayout(4,2));
pan1.add(ltext);
pan1.add(text);
pan1.add(lfind);
pan1.add(findText);
pan1.add(lreplace);
pan1.add(replaceText);
pan1.add(loccurance);
pan1.add(occurrenceText);
find = new JButton("Find");
replace = new JButton("Replace");
clear= new JButton("Clear");
find.addActionListener(this);
replace.addActionListener(this);
clear.addActionListener(this);
pan2 = new JPanel();
pan2.setLayout(new FlowLayout());
pan2.add(find);
pan2.add(replace);
pan2.add(clear);
add(pan1,"Center");
add(pan2,"South");
setTitle("Find And Replace");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public
void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == find)
{
String s = text.getText();
String f = findText.getText();
i = s.indexOf(f);
if(i != -1)
{ occurrences++;
occurrenceText.setText(Integer.toString(occurrences));
text.select(i,i+f.length());
text.requestFocus();
}
}
if(ae.getSource() == replace)
{
if(text.getSelectedText().length()!=0)
{
String
r = replaceText.getText();
text.replaceSelection(r);
}
}
if(ae.getSource() == clear)
{
text.setText("");
findText.setText("");
replaceText.setText("");
occurrenceText.setText("");
}
}
public static void main(String[]
args)
{
new Slip21();
}
}
Slip no 17
Write a Java
program to design a screen using Awt that will take a user name and password.
If the user name and password are not same, raise an Exception with appropriate
message. User can have 3 login chances only. Use clear button to clear the
TextFields.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class InvalidPasswordException
extends Exception
{}
class Slip17 extends JFrame
implements ActionListener
{
JLabel name, pass;
JTextField
nameText;
JPasswordField
passText;
JButton login,
end;
static int cnt=0;
Slip17()
{
name = new JLabel("Name : ");
pass = new JLabel("Password : ");
nameText = new JTextField(20);
passText = new JPasswordField(20);
login = new JButton("Login");
end = new JButton("End");
login.addActionListener(this);
end.addActionListener(this);
setLayout(new GridLayout(3,2));
add(name);
add(nameText);
add(pass);
add(passText);
add(login);
add(end);
setTitle("Login Check");
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void
actionPerformed(ActionEvent e)
{
if(e.getSource()==end)
{
System.exit(0);
}
if(e.getSource()==login)
{
try
{
String user = nameText.getText();
String pass = new String(passText.getPassword());
if(user.compareTo(pass)==0)
{ JOptionPane.showMessageDialog(null,"Login Successful","Login",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e1)
{
cnt++;
JOptionPane.showMessageDialog(null,"Login
Failed","Login",JOptionPane.ERROR_MESSAGE);
nameText.setText("");
passText.setText("");
nameText.requestFocus();
if(cnt == 3)
{
JOptionPane.showMessageDialog(null,"3 Attempts
Over","Login",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
}
public static void main(String
args[])
{
new Slip17();
}
}
Comments
Post a Comment