+ All Categories
Home > Documents > Class 11 12 Practicals Answers

Class 11 12 Practicals Answers

Date post: 05-Apr-2018
Category:
Upload: subhash-varghese
View: 222 times
Download: 0 times
Share this document with a friend
27
1 | Page Java Practicals [email protected]  Create a GUI Form to do the following: 1. Create a GUI to perform addition, multiplication, subtraction an d division of two numbers u sing four d ifferent buttons for each operation and a single text box to display the result. private void btnAActionPerformed(java.awt.event.ActionEvent evt) { double n1,n2,res; String s1=t1.getText(); String s2=t2.getText(); n1=Double.parseDouble(s1); n2=Double.parseDouble(s2); res=n1+n2; t3.setText(res+""); } private void btnSActionPerformed(java.awt.event.ActionEvent evt) { double n1,n2,res; String s1=t1.getText(); String s2=t2.getText(); n1=Double.parseDouble(s1); n2=Double.parseDouble(s2); res=n1-n2; t3.setText(res+""); } private void btnMActionPerformed(java.awt.event.ActionEvent evt) { double n1,n2,res; String s1=t1.getText(); String s2=t2.getText(); n1=Double.parseDouble(s1); n2=Double.parseDouble(s2); res=n1*n2; t3.setText(res+""); } private void btnDActionPerformed(java.awt.event.ActionEvent evt) { double n1,n2,res; String s1=t1.getText(); String s2=t2.getText(); n1=Double.parseDouble(s1); n2=Double.parseDouble(s2); res=n1/n2; t3.setText(res+""); } private void btn0ActionPerformed(java.awt.event.ActionEvent evt) { DecimalFormat d=new DecimalFormat("#0"); double r=Double.parseDouble(t3.get Text()); t3.setText(d.format(r)); } private void btn2ActionPerformed(java.awt.event.ActionEvent evt) { DecimalFormat d=new DecimalFormat("#0.00"); double r=Double.parseDouble(t3.get Text()); t3.setText(d.format(r)); } 2. Create a GUI to perform addition, multiplication, subtraction an d division of two numbers using a single butto n and four different text boxes to store the respective results. private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) { double n1,n2; double add,sub,pro,div; n1=Double.parseDouble(t1.getText()); n2=Double.parseDouble(t2.getText()); add=n1+n2; sub=n1-n2;
Transcript

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 1/26

1 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

Create a GUI Form to do the following:

1. Create a GUI to perform addition, multiplication, subtraction and division of two numbers using four different buttonsfor each operation and a single text box to display the result.

private void btnAActionPerformed(java.awt.event.ActionEvent evt) {double n1,n2,res;

String s1=t1.getText();String s2=t2.getText();

n1=Double.parseDouble(s1);n2=Double.parseDouble(s2);

res=n1+n2;

t3.setText(res+""); }

private void btnSActionPerformed(java.awt.event.ActionEvent evt) {double n1,n2,res;String s1=t1.getText();String s2=t2.getText();

n1=Double.parseDouble(s1);n2=Double.parseDouble(s2);

res=n1-n2;

t3.setText(res+""); }

private void btnMActionPerformed(java.awt.event.ActionEvent evt) {double n1,n2,res;String s1=t1.getText();String s2=t2.getText();

n1=Double.parseDouble(s1);n2=Double.parseDouble(s2);

res=n1*n2;

t3.setText(res+""); }

private void btnDActionPerformed(java.awt.event.ActionEvent evt) {double n1,n2,res;String s1=t1.getText();String s2=t2.getText();

n1=Double.parseDouble(s1);n2=Double.parseDouble(s2);

res=n1/n2;

t3.setText(res+""); }

private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {DecimalFormat d=new DecimalFormat("#0");double r=Double.parseDouble(t3.getText());

t3.setText(d.format(r)); }

private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {DecimalFormat d=new DecimalFormat("#0.00");double r=Double.parseDouble(t3.getText());

t3.setText(d.format(r)); }

2. Create a GUI to perform addition, multiplication, subtraction and division of two numbers using a single button and four

different text boxes to store the respective results.

private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) {double n1,n2;double add,sub,pro,div;

n1=Double.parseDouble(t1.getText());n2=Double.parseDouble(t2.getText());

add=n1+n2;sub=n1-n2;pro=n1*n2;div=n1/n2;

tA.setText(add+"");

tS.setText(sub+"");tP.setText(pro+"");tD.setText(div+""); } 

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 2/26

2 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

3. Create a GUI form to find simple interest. Enter Principal amount, Rate of Interest and time in three different text boxesand calculate and display interest amount in a text box.

private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {double p,t,r;p=Double.parseDouble(tPrincipal.getText());r=Double.parseDouble(tRate.getText());

t=Double.parseDouble(tYear.getText());

double interest=(p*t*r)/100;tInt.setText(interest+"");tAmt.setText(p+interest+""); }

private voidbtnClearActionPerformed(java.awt.event.ActionEvent evt) {

tPrincipal.setText("");tRate.setText("");tYear.setText("");tInt.setText("");tAmt.setText(""); }

private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {

this.dispose(); } 

4. Find square and cube of a given number.

private void btnSqrActionPerformed(java.awt.event.ActionEvent evt) {double n1,sq;String s1=t1.getText();

n1=Double.parseDouble(s1);sq=n1*n1;

t3.setText(sq+""); }

private void btnCubeActionPerformed(java.awt.event.ActionEvent evt){

double n1,cu;String s1=t1.getText();

n1=Double.parseDouble(s1);cu=n1*n1*n1;

t3.setText(cu+""); }

5. Enter a number and display its square root.

private void btnSqrt1ActionPerformed(java.awt.event.ActionEvent evt) {DecimalFormat d=new DecimalFormat("#0.00");

double n1=Double.parseDouble(t1.getText());double res=Math.sqrt(n1);

t3.setText(d.format(res)+""); }

private void btnSqrt2ActionPerformed(java.awt.event.ActionEvent evt) {DecimalFormat d=new DecimalFormat("#.00");

double n1=Double.parseDouble(t2.getText());double res=Math.sqrt(n1);

t3.setText(d.format(res)+""); }

6. Enter two numbers and display the result of First Number raise to power Second Number.

private void btnAraisetoBActionPerformed(java.awt.event.ActionEvent evt){DecimalFormat d=new DecimalFormat("# 0.00");

double n1=Double.parseDouble(t1.getText());double n2=Double.parseDouble(t2.getText());double res=Math.pow(n1,n2);

t3.setText(d.format(res)+""); }

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 3/26

3 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

7. Enter radius of a circle and find its area and circumference.

private void btnAreaActionPerformed(java.awt.event.ActionEvent evt) {double r,a,c;r=Double.parseDouble(tRadius.getText());

a=3.14*r*r;

c=2*3.14*r;

tArea.setText(a+"");tCircum.setText(c+""); }

8. Enter your name, class, section, address, phone number, city, state and pin in different text fields and display all thesedifferent values in a textarea control.

private void btnsubmitActionPerformed(java.awt.event.ActionEvent evt) {ta1.append("Name: "+tName.getText()+" ");ta1.append("Class: "+ tClass.getText()+" "+tSection.getText()+ "\n");

ta1.append("\n");

ta1.append("Address: "+ tAddress.getText()+" Phone: "+tPhone.getText()+ "\n");ta1.append("\n");

ta1.append("City: "+ tCity.getText()+" State: "+tState.getText()+ "\n");ta1.append("Pin Code: "+tPin.getText()); }

9. Enter a name and display a message “Hello” and the name you entered in the textfield in a message box.

private void btnMessageActionPerformed(java.awt.event.ActionEvent evt) {String name=t1.getText();JOptionPane.showMessageDialog(this,"Hello "+ name+ " Welcome to Java");

}

10. Enter a name and create two buttons to convert that to “Uppercase” and “Lowercase”. Also create a button that willdisplay the “Length” of the name you entered in a message box.

private void btnUpperActionPerformed(java.awt.event.ActionEvent evt) {String name=t1.getText();

name=name.toUpperCase();

t1.setText(name); } 

private void btnLowerActionPerformed(java.awt.event.ActionEvent evt) {String name=t1.getText();name=name.toLowerCase();

t1.setText(name); }private void btnLengthActionPerformed(java.awt.event.ActionEvent evt) {

String name=t1.getText();int l=name.length();JOptionPane.showMessageDialog(this,"The Length is: "+l); } 

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 4/26

4 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

11. Enter a number and reverse the sign of that number i.e. if the number is positive change it to negative and it it is anegative than change it to positive.

private void btnRevSignActionPerformed(java.awt.event.ActionEvent evt) {double n=Double.parseDouble(t1.getText());n=n*-1;

t1.setText(n+""); } 

12. Enter two numbers and display which of them is the biggest and the smallest. Also check for their equality.

private void btnMaxActionPerformed(java.awt.event.ActionEvent evt) {int n1=Integer.parseInt(t1.getText());int n2=Integer.parseInt(t2.getText());

String r="";

if(n1>n2){r="First Number"; }

if(n2>n1){r="Second Number"; }

if(n1==n2){r="Both are equal"; }

t3.setText(r);}

private void btnMinActionPerformed(java.awt.event.ActionEvent evt) {int n1=Integer.parseInt(t1.getText());int n2=Integer.parseInt(t2.getText());

String r="";

if(n1<n2) r="First Number";

if(n2<n1) r="Second Number";

if(n1==n2) r="Both are equal";

t3.setText(r);}

13. Enter 5 numbers and display the maximum and minimum of those numbers. Create two buttons, one to find maximumand other to find minimum.

private void btnMaxActionPerformed(java.awt.event.ActionEvent evt) {int n1=Integer.parseInt(t1.getText());int n2=Integer.parseInt(t2.getText());int n3=Integer.parseInt(t3.getText());int n4=Integer.parseInt(t4.getText());int n5=Integer.parseInt(t5.getText());

int max=n1;String r="First";

if(n2>max){ max=n2;r="Second"; }if(n3>max){ max=n3;r="Third"; }if(n4>max){ max=n4;r="Fourth"; }if(n5>max){ max=n5;r="Fifth"; }

res.setText(r + " " + max);}

private void btnMinActionPerformed(java.awt.event.ActionEvent evt) {int n1=Integer.parseInt(t1.getText());int n2=Integer.parseInt(t2.getText());int n3=Integer.parseInt(t3.getText());int n4=Integer.parseInt(t4.getText());int n5=Integer.parseInt(t5.getText());

int min=n1;String r="First";

if(n2<min){ min=n2;r="Second"; }if(n3<min){ min=n3;r="Third"; }if(n4<min){ min=n4;r="Fourth"; }if(n5<min){ min=n5;r="Fifth"; }

res.setText(r + " " + min);

}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 5/26

5 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

14. Enter a number between 1-7 and display the corresponding dayname using „switch‟. 

private void btnDayNameActionPerformed(java.awt.event.ActionEvent evt) {int n=Integer.parseInt(t1.getText());String dayname="";

switch(n){case 1:dayname="Monday";break;case 2:dayname="Tuesday";break;case 3:dayname="Wednesday";break;case 4:dayname="Thursday";break;case 5:dayname="Friday";break;case 6:dayname="Saturday";break;case 7:dayname="Sunday";break;default: dayname="Invalid";

}

t2.setText("The Day is: "+dayname);}15. Enter a number between 1-12 and display the corresponding month name using if.

private void btnMonthNameActionPerformed(java.awt.event.ActionEvent evt) {int n=Integer.parseInt(t1.getText());String monthname="";

if(n==1)monthname="January";if(n==2)monthname="February";if(n==3)monthname="March";if(n==4)monthname="April";if(n==5)monthname="May";if(n==6)monthname="June";if(n==7)monthname="July";if(n==8)monthname="August";if(n==9)monthname="September";

if(n==10)monthname="October";if(n==11)monthname="November";if(n==12)monthname="December";if(n<1 || n>12) monthname="Invalid";

t2.setText("The Month is: "+monthname);}

16. Enter a number between 1-100 and display the grade based on the following criteria using if-else if

private void btnGradeActionPerformed(java.awt.event.ActionEvent evt) {int n=Integer.parseInt(t1.getText());String grade="";

if(n<1||n>100)grade="Invalid";

else if(n >= 90)grade="A++";

else if(n>=80)grade = "A+";

else if(n>=75)grade = "A";

else if(n>=60)grade = "B";

else if(n>=50)grade = "C";

else if(n>=40)grade = "D";

else if(n>=1 && n<40)grade = "Fail";

t2.setText("The Grade is: "+grade); }

Marks Percentage Grade> = 90 A + +

80 – 90 A +75 – 80 A

60 – 75 B50 – 60 C

40 – 50 D< 40 Fail

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 6/26

6 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

17. Enter a number and check (Create 5 different BUTTONS to do the following)a. if it is an even number or an odd number.b. if it is a prime number or not.c. if it is a positive, negative or zero.d. if it is divisible by 5 or not.e. if it is an Armstrong number or not.

private void btnIsPrimeActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());boolean prime=true;

for(int i=2;i<=n/2;i++){if(n%i==0){

prime=false;break;

}}

t2.setText("Prime: "+prime); }

private void btnEvenOddActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());

String evodd="";

if(n%2==0)evodd="An Even Number";

elseevodd="An Odd Number";

t2.setText(evodd); }

private void btnIsArmstrongActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());

if(n>999||n<100){JOptionPane.showMessageDialog(this,"Armstrong Numbers are 3 Digit Numbers ONLY");return;

}boolean armstrong=false;long newn=0;long temp=n;

long n1,n2,n3;

n1=n%10;n=n/10;

n2=n%10;n=n/10;

n3=n%10;n=n/10;

newn=(n1*n1*n1)+(n2*n2*n2)+(n3*n3*n3);

if(newn==temp) armstrong=true;

t2.setText("Armstrong: "+armstrong); }

private void btnPosNegActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());

if(n<0)t2.setText("It's a Negative Number");

else if(n>0)

t2.setText("It's a Positive Number");elset2.setText("The Number is ZERO");

}

private void btnDivBy5ActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());String divBy5="";

if(n%2==0)divBy5="Divisible by 5";

elsedivBy5="Not Divisible by 5";

t2.setText(divBy5);

}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 7/26

7 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

18. Enter a number in a texbox and on clicking a button that number will be appended to a textarea control and text box willbe blank. Another Button will show an Input Box and on clicking its OK button the number will be appended to the sametextarea control.

private void btnAppendActionPerformed(java.awt.event.ActionEvent evt) {double n=Double.parseDouble(t1.getText());ta.append(n+"\n");t1.setText("");

}

private void btnAppendInputBoxActionPerformed(java.awt.event.ActionEvent evt) {String s=JOptionPane.showInputDialog(this,"Enter a Number");double n=Double.parseDouble(s);ta.append(n+"\n");

}

19. Input two strings in two text boxes (Create 7 different BUTTONS to do the following)a. and concat them and display theconcated string in a third text box.

b. and print whether they are same:Ignoring case Case sensitiveusing equals(), equalsIgnoreCase()and compareTo() methods.

c. Print a character at position ‘n’ of first string

d. Reverse the first String usingStringBuilder Class.

e. Find the length of first string

private void btnConcatActionPerformed(java.awt.event.ActionEvent evt) {String str1=s1.getText();String str2=s2.getText();String str3="";

str3=str3.concat(str1+" "+str2);

s3.setText(str3); }

private void btnEqualsIgnoreCaseActionPerformed(java.awt.event.ActionEvent evt) {String str1=s1.getText();String str2=s2.getText();

String res="";

if(str1.equalsIgnoreCase(str2)) res="Both are equal";else res="Not Equal";

s3.setText(res); }

private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {String str1=s1.getText();String str2=s2.getText();

String res="";

if(str1.equals(str2))res="Both are equal";

elseres="Not Equal";

s3.setText(res); }

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 8/26

8 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

private void btnCompareToActionPerformed(java.awt.event.ActionEvent evt) {String str1=s1.getText();String str2=s2.getText();

String res="";

if(str1.compareTo(str2)==0)res="Both are equal";

elseres="Not Equal";

s3.setText(res); }

private void btnCharAtActionPerformed(java.awt.event.ActionEvent evt) {String str=s1.getText();int n=Integer.parseInt(JOptionPane.showInputDialog(this,"Enter position"));int l=str.length();

if(n>l-1){JOptionPane.showMessageDialog(this,"Value greater than Length of the String");return;

}char ch=str.charAt(n);

s3.setText(ch+""); }

private void btnLengthActionPerformed(java.awt.event.ActionEvent evt) {String str=s1.getText();int l=str.length();

s3.setText("The Length is: "+l); }

private void btnReverseActionPerformed(java.awt.event.ActionEvent evt) {String str1=s1.getText();StringBuilder sb1=new StringBuilder();

sb1.append(str1);str1=sb1.reverse()+"";

s3.setText(str1); }

20. Using „for‟ loop create a GUI Form to: (Create 14 different BUTTONS to do the following)a. Print 1

st10 numbers in a textarea control.

b. Print 1st

10 numbers in reverse in a textareacontrol.

c. Print 1st

10 even numbers in a textarea control.d. Print 1

st10 even numbers in reverse in a

textarea control.e. Print 1

st10 odd numbers in a textarea control

f. Print 1st

10 odd numbers in reverse in atextarea control.

g. Enter a number using Input box and print itstable upto x10 in a textarea control.

h. Enter a message using Input box and print it 10times in a textarea control.

i. Print characters from A to Z in a textareacontrol.

 j. Print all the ‘armstrong’ numbers in a textareacontrol.

k. Print 1st

10 numbers and their sum in a textareacontrol.

l. Print 1st

10 even numbers and their sum in atextarea control.

m. Print 1st

50 Prime numbers in a textarea control.n. Enter a number in a Input box and print its

factorial in a textarea control.

private void btnFirst10ActionPerformed(java.awt.event.ActionEvent evt) {int n;ta.setText("");for(n=1;n<=10;n++)

ta.append(n+"\n");}

private void btnFirst10RevActionPerformed(java.awt.event.ActionEvent evt) {int n;

ta.setText("");for(n=10;n>=1;n--)

ta.append(n+"\n");}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 9/26

9 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

private void btnFirst10EvenActionPerformed(java.awt.event.ActionEvent evt) {int n;ta.setText("");for(n=2;n<=20;n+=2)

ta.append(n+"\n"); }

private void btnFirst10EvenRevActionPerformed(java.awt.event.ActionEvent evt) {int n;

ta.setText("");for(n=20;n>=2;n-=2)ta.append(n+"\n"); }

private void btnFirst10OddActionPerformed(java.awt.event.ActionEvent evt) {int n;ta.setText("");for(n=1;n<=20;n+=2)

ta.append(n+"\n"); }

private void btnFirst10OddRevActionPerformed(java.awt.event.ActionEvent evt) {int n;ta.setText("");for(n=19;n>=1;n-=2)

ta.append(n+"\n"); }

private void btnTableX10ActionPerformed(java.awt.event.ActionEvent evt) {String s=JOptionPane.showInputDialog(this,"Table of which Number??");long n=Long.parseLong(s);

ta.setText("");for(int a=1 ; a<=10 ; a++)

ta.append(n + " x "+ a + " = "+ n*a + "\n");}

private void btnMsgX10ActionPerformed(java.awt.event.ActionEvent evt) {String s=JOptionPane.showInputDialog(this,"Type your Message");ta.setText("");for(int a=1 ; a<=10 ; a++)

ta.append(s + "\n");

}

private void btnAtoZActionPerformed(java.awt.event.ActionEvent evt) {ta.setText("");for(int a=65 ; a<=90 ; a++)

ta.append((char) a+ "\n");}

private void btnArmStrongActionPerformed(java.awt.event.ActionEvent evt) {ta.setText("");for(int a=100;a<=999;a++){

int n=a;int sumcube=0;while(n>0){

int dig=n%10;sumcube=sumcube+(dig*dig*dig);n=n/10;

}if(sumcube==a)ta.append(a+"\n");

}}

private void btnFirst10NosSumActionPerformed(java.awt.event.ActionEvent evt) {int sum=0;ta.setText("");

for(int a=1;a<=10;a++){ta.append(a+"\n");sum=sum+a;

}ta.append("Sum= "+sum); }

private void btnFirst10EvenNosSumActionPerformed(java.awt.event.ActionEvent evt) {int sum=0;ta.setText("");

for(int a=2;a<=20;a+=2){ta.append(a+"\n");sum=sum+a;

}ta.append("Sum= "+sum); }

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 10/26

10 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

private void btnFactActionPerformed(java.awt.event.ActionEvent evt) {String s=JOptionPane.showInputDialog(this,"Factorial of which Number??");

long n=Long.parseLong(s);

long fact=1;ta.setText("");for(int a=1 ; a<=n ; a++)

fact=fact*a;

ta.append("Factorial= "+fact);}

private void btnFirst50PrimesActionPerformed(java.awt.event.ActionEvent evt) {ta.setText("1\n");for(int a=2;a<=50;a++){

boolean p=true;

for(int k=2;k<=a/2;k++){if(a%k==0){

p=false;break;

}}//end k loop

if(p==true)ta.append(a+"\n");

}//end a loop

}

21. Using while Loop, enter a number in a textfield and : (Create 15 different BUTTONS to do the following)a. Display total digits it containb. Display sum of its individual

digits.c. Reverse that number.d. Display maximum digit.e. Display minimum digit.f. Display second maximum digit.

g. Display total number of evenand odd digits.

h. Display sum of its even digits.i. Display sum of its odd digits.

 j. Display sum of cube of itsindividual digits.

k. Display sum of square of itsindividual digits.

l. Increase each digit of thenumber by 1 and display it as anew number.

m. Display ‘n’ Fibonacci numbersin a textarea.

n. Display Prime numbers upto ‘n’. o. Display ‘n’ Prime Numbers. 

private void btnSumEvenActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());long sumeven=0;

while(n>0){long dig=n%10;

if(dig%2==0) sumeven=sumeven+dig;

n=n/10;}t2.setText("The Sum of Even digits is : "+ sumeven); }

private void btnReverseActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());long newn=0;long temp=n;

while(temp>0){long dig=temp%10;newn=(newn*10)+dig;temp=temp/10;

}

t2.setText("Reverse Number: "+newn);}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 11/26

11 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

private void btnCountDigitsActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());long count=0;long temp=n;

while(temp>0){count++;temp=temp/10;

}

t2.setText("Total Non-Zero Digits: "+count); }

private void btnSumDigitsActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());long sum=0;long temp=n;

while(temp>0){long dig=temp%10;sum=sum+dig;temp=temp/10;

}

t2.setText("Sum of Digits: "+sum); }

private void btnTotalEvenOddActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());long counteven=0;long countodd=0;long temp=n;

while(temp>0){long dig=temp%10;if(dig%2==0)counteven++; else countodd++;temp=temp/10;

}

t2.setText("Total Evens: "+counteven+" Total Odds: "+countodd); }

private void btn2MaxActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());long max=0,max2=0;

while(n>0){long dig=n%10;

if(dig>max) { max2=max ; max=dig ; } else if(dig>max2) max2=dig;

n=n/10;

}t2.setText("Maximum Digit: "+max+" Second Maximum Digit: "+max2); }

private void btnSumOfCubeDigitsActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());long sumcube=0;

while(n>0){long dig=n%10;

sumcube=sumcube+(dig*dig*dig);

n=n/10;}t2.setText("The Sum of Cube of digits is : "+ sumcube); }

private void btnPrimeActionPerformed(java.awt.event.ActionEvent evt) {

long n=Long.parseLong(t1.getText());boolean prime=true;

for(long i=1;i<=n;i++){prime=true;for(long k=2;k<=i/2;k++){

if(i%k==0) { prime=false; break; }}if(prime) ta1.append(i+"\n");

}}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 12/26

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 13/26

13 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

private void btnIcDigitBy1ActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());long newn=0;

while(n>0){long dig=n%10;

newn=newn*10 + (dig+1);

n=n/10;}

while(newn>0){long dig=newn%10;

n=n*10 + dig;newn=newn/10;

}

t2.setText("The New Number with each digit increased by 1 is : "+ n); }

private void btnNprimeActionPerformed(java.awt.event.ActionEvent evt) {long n=Long.parseLong(t1.getText());boolean prime=true;

ta1.setText("");long i;long count=0;for(i=1,count=1;count<=n;i++){

prime=true;for(long k=2;k<=i/2;k++){

if(i%k==0){ prime=false; break; }}

if(prime){ta1.append(count+" = " + i+"\n"); count++; }

}

}

22. Input a string and : (Create 3 different BUTTONS to do the following)a. Count total vowels in it. It should also display the count of all the individual vowels. Make use of a method namely

‘isVowel‟ to check if the character is a vowel or not.b. Reverse that string without using StringBuffer or StringBuilder class. Also check if the string is a ‘Palindrome’ or ‘Not a

Palindrome’. 

private void btnCountVowelActionPerformed(java.awt.event.ActionEvent evt) {String s;int c=0;int a=0;int e=0;int i=0; int o=0; int u=0;char ch;

s=t1.getText();

s=s.toUpperCase();

int l=s.length();

for(int k=0;k<l;k++){ch=s.charAt(k);

if(isVowel(ch)==true){

c++;a=(ch=='A' ? ++a:a);e=(ch=='E' ? ++e:e);i=(ch=='I' ?++i:i);

o=(ch=='O' ? ++o:o);u=(ch=='U' ? ++u:u);

}}

t2.setText(String.valueOf(c));t3.setText(String.valueOf(a));t4.setText(String.valueOf(e));t5.setText(String.valueOf(i));t6.setText(String.valueOf(o));t7.setText(String.valueOf(u));

}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 14/26

14 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

private void btnreverseActionPerformed(java.awt.event.ActionEvent evt) {String s=t1.getText();String s2="";

int l=s.length();

char ch;

for(int i=l-1;i>=0;--i){ch=s.charAt(i) ;s2=s2+ch;

}trev.setText(s2);

if(s.equalsIgnoreCase(s2)){ta.setText("Palindrome");

}else{

ta.setText("Not Palindrome");}

}

private boolean isVowel(char a){

if(a=='A'||a=='E'||a=='I'||a=='O'||a=='U')return true;

elsereturn false;

}

23. Enter a starting value and an end value in two text boxes and (Create 6 different BUTTONS to do the following)a. Print a series between those two numbers and their sumb. Print all the even numbers between them as well as the sum of those even numbersc. Print all the odd numbers between them as well as their sum.d. Print all the prime numbers between them as well as their sum.

e. Print a Fibonacci series between them and their sum.f. Interchange or swap values of both the textfield.

In all the above cases the result should be displayed in a textarea control.

private void btnSeriesActionPerformed(java.awt.event.ActionEvent evt) {long n1=Long.parseLong(t1.getText());long n2=Long.parseLong(t2.getText());

long sum=0;long temp;

if(n1>n2){temp=n1; n1=n2; n2=temp;

}

ta.setText("");for(long a=n1;a<=n2;a++){

ta.append(a+ " ");sum=sum+a;}

ta.append("\n Sum= "+ sum); }

private void btnEvenSumActionPerformed(java.awt.event.ActionEvent evt) {long n1=Long.parseLong(t1.getText());long n2=Long.parseLong(t2.getText());

long sum=0;long temp;

if(n1>n2){temp=n1; n1=n2; n2=temp;}

ta.setText("");for(long a=n1;a<=n2;a++){

if(a%2==0){ta.append(a+ " ");sum=sum+a;

}}

ta.append("\nEven Sum= "+ sum); }

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 15/26

15 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

private void btnOddSumActionPerformed(java.awt.event.ActionEvent evt) {long n1=Long.parseLong(t1.getText());long n2=Long.parseLong(t2.getText());

long sum=0, temp;

if(n1>n2){temp=n1; n1=n2; n2=temp; }

ta.setText("");for(long a=n1;a<=n2;a++){

if(a%2!=0){ta.append(a+ " ");sum=sum+a;

}}

ta.append("\nOdd Sum= "+ sum); }

private void btnPrimeSumActionPerformed(java.awt.event.ActionEvent evt) {long n1=Long.parseLong(t1.getText());long n2=Long.parseLong(t2.getText());

long sum=0, temp;

if(n1>n2){temp=n1; n1=n2; n2=temp; }

ta.setText("");boolean p=true;for(long a=n1;a<=n2;a++){

for(long k=2;k<=n1/2;k++){if(a%k==0){

p=false;break ;

}}// end loop2

if(p){ ta.append(a+ " "); sum=sum+a; }

p=true;}//end loop1

ta.append("\nPrime Sum= "+ sum); }

private void btnFibbSumActionPerformed(java.awt.event.ActionEvent evt) {long n1=Long.parseLong(t1.getText());long n2=Long.parseLong(t2.getText());

long sum=0,temp;

if(n1>n2){temp=n1; n1=n2; n2=temp; }

ta.setText("");long f1,f2,f3;f1=n1;f2=n1;ta.append(f1 + " "+ f2 +" ");f3=f1+f2;sum=sum+f3;

for(long a=f3;f3<=n2;){ta.append(f3+" ");f1=f2;f2=f3;f3=f1+f2;

sum=sum+f3;}

ta.append("\n Fibonacci Sum= "+ sum); }

private void btnSwapActionPerformed(java.awt.event.ActionEvent evt) {

long n1=Long.parseLong(t1.getText());long n2=Long.parseLong(t2.getText());

long sum=0, temp;

temp=n1; n1=n2; n2=temp;

t1.setText(n1+"");

t2.setText(n2+"");

ta.setText("Values of 2 textfields interchanged using a 3rd variable"); }

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 16/26

16 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

24. Enter 5 numbers and find maximum, minimum and 2nd

maximum of those numbers.

private void btnMaxMinMax2ActionPerformed(java.awt.event.ActionEvent evt) {int n1=Integer.parseInt(t1.getText());int n2=Integer.parseInt(t2.getText());int n3=Integer.parseInt(t3.getText());int n4=Integer.parseInt(t4.getText());int n5=Integer.parseInt(t5.getText());

int max1=n1;

int max2=0;int min=n1;

 // coding for max and second maxif(n2>max1){ max2=max1; max1=n2; } else if(n2>max2){ max2=n2; }if(n3>max1){ max2=max1; max1=n3; } else if(n3>max2){ max2=n3; }if(n4>max1){ max2=max1; max1=n4; } else if(n4>max2){ max2=n4; }if(n5>max1){ max2=max1; max1=n5; } else if(n5>max2){ max2=n5; }

 // coding for min

if(n2<min) min=n2;if(n3<min) min=n3;if(n4<min) min=n4;if(n5<min) min=n5;

tMax.setText(max1+"");tMin.setText(min+"");tMax2.setText(max2+""); }

25. Create a Panel control on the form and add three radio buttons namely „Male‟, „Female‟ and „Kid‟ in it. Perform action soas to make only one radio button selectable in that panel. Now, display which radio button has been selected in amessage box by:

a. Creating a buttonb. By creating a common code for the ‘actionPerformed’ event of those three radio buttons and name the method as

‘custType’ 

private void btnSelectionActionPerformed(java.awt.event.ActionEvent evt) {String msg="";

if(rbM.isSelected()==true){ msg="The customer is Male:Button Clicked"; }

if(rbF.isSelected()==true){ msg="The customer is Female:Button Clicked"; }

if(rbK.isSelected()==true){ msg="The customer is a Kid:Button Clicked"; }

JOptionPane.showMessageDialog(this,msg);}

private void custType(java.awt.event.ActionEvent evt) {String msg="";

if(rbM.isSelected()){ msg="The customer is Male:Common Code"; }

if(rbF.isSelected()){ msg="The customer is Female:Common Code"; }

if(rbK.isSelected()){ msg="The customer is a Kid:Common Code"; }

JOptionPane.showMessageDialog(this,msg); }

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 17/26

17 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

26. Create a combo box and add „Jaipur‟, „Kota‟, „Ajmer‟ and „Udaipur‟ in it. Now, display the selected value and its indexposition in a message box by:

a. Creating a buttonb. By creating a common code for the ‘actionPerformed’ event of that combo box and name the method as ‘CityName’  

private void btnSelectionActionPerformed(java.awt.event.ActionEvent evt) {String city=cmbCity.getSelectedItem()+"";int p=cmbCity.getSelectedIndex();

JOptionPane.showMessageDialog(this,"City: "+ city+ " Position: "+ p + " : Using Button");}

private void cityName(java.awt.event.ActionEvent evt) {String city=cmbCity.getSelectedItem()+"";

int p=cmbCity.getSelectedIndex();

JOptionPane.showMessageDialog(this,"City: "+ city+ " Position: "+ p + " : Using Common Code");}

27. Input a year and display if it is a leap year or not.28. Write a program which on clicking a button will keep on asking for a number in an Input box until user enters -999 and

then will display the sum and count of those numbers in a message box.

private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {int n;int sum=0;int count=0;

while (true){String s=JOptionPane.showInputDialog("Enter a Number : (-999 to STOP ) ");n=Integer.parseInt(s);

if(n==-999){break;

}else{

ta.append(s + "\n");sum=sum+n;++count;

}}JOptionPane.showMessageDialog(this, "Total Numbers :"+count + " Sum = "+ sum);

}

29. Write a program to print the following series:1 1 *12 22 **

123 333 ***1234 4444 ****

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 18/26

18 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

30. Enter a name using Input Box. Now add that name in a textarea control ONLY if users click YES button in a Confirm Box.On Adding that name in a textarea control a message „Success‟ will appear in a Message Box. The program will keep onprompting for a name using Input Box till user does not enter a Blank value in that box.

private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {String name="Start";

while(!(name.equals(""))){

name=JOptionPane.showInputDialog(this,"Enter a Name: (Leave BLANK to EXIT");if(name.equals(""))break;

int n=JOptionPane.showConfirmDialog(this, "Are you Sure to Add?");

if(n==0){ta.append(name+"\n");JOptionPane.showMessageDialog(this, "Name Added Successfully");

}else{

JOptionPane.showMessageDialog(this, "Name NOT Added");}

}

}

31. Enter a string in a textbox anda. On clicking a button that string will

be added into a combo box.b. On clicking a button, the selected

value will be removed from thecombo box after confirming it fromthe user using Confirm Box.

private void tAdd2ComboActionPerformed(java.awt.event.ActionEvent evt) {String city=t1.getText();cmbCity.addItem(city+"");t1.setText("");

}

private void btnRemoveFromComboActionPerformed(java.awt.event.ActionEvent evt) {int n=cmbCity.getSelectedIndex();int yn=JOptionPane.showConfirmDialog(this,"Are you sure to REMOVE it??");

if(yn==0){cmbCity.removeItemAt(n);}else{

JOptionPane.showMessageDialog(this,"Operation Cancelled");}

t1.setText("");}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 19/26

19 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

32. Create a calculator using four radio buttons that will perform all the four basic mathematical operations. Write acommon code for all the four radio button‟s „actionPerformed‟ event. 

private void opSelected(java.awt.event.ActionEvent evt) {double f=0;double s=0;double res=0;

DecimalFormat d=new DecimalFormat("#.00");try{

f=Double.parseDouble(t1.getText());s=Double.parseDouble(t2.getText());

}catch(Exception e){JOptionPane.showMessageDialog(this,"Please Enter a Valid Number");t1.requestFocus();return;

}

if(op1.isSelected()==true) res=f+s;if(op2.isSelected()==true) res=f-s;if(op3.isSelected()==true) res=f*s;

try{if(op4.isSelected()==true) res=f/s;

}catch(Exception e) {JOptionPane.showMessageDialog(this,"Please Enter a non-zero value for second number");t2.requestFocus();return;

}

t3.setText(""+d.format(res)); }

33. Create a calculator using a „List Box‟. Four mathematical operations will be displayed in a list box and on clicking anoption in a list box, the result will be displayed in a text field. Make use of its „getSelectedValue()‟ method. 

private void lstOperationsValueChanged(javax.swing.event.ListSelectionEvent evt) {String op=lstOperations.getSelectedValue()+"";

DecimalFormat d=new DecimalFormat("#0.00");

double n1=Double.parseDouble(t1.getText());double n2=Double.parseDouble(t2.getText());double n3=0;

if(op.equalsIgnoreCase("Add")) n3=n1+n2;if(op.equalsIgnoreCase("Subtract")) n3=n1-n2;if(op.equalsIgnoreCase("Multiply")) n3=n1*n2;if(op.equalsIgnoreCase("Divide")) n3=n1/n2;

t3.setText(d.format(n3)+"");

}34. Create a calculator using a „Combo Box‟. Four mathematical operations will be displayed in a combo box and on

clicking an option in a combo box, the result will be displayed in a text field. Make use of its „getSelectedItem()‟ method. 

private void cmbOperationsActionPerformed(java.awt.event.ActionEvent evt) {String op=cmbOperations.getSelectedItem()+"";

DecimalFormat d=new DecimalFormat("#0.00");

double n1=Double.parseDouble(t1.getText());double n2=Double.parseDouble(t2.getText());double n3=0;

if(op.equalsIgnoreCase("Add")) n3=n1+n2;

if(op.equalsIgnoreCase("Subtract")) n3=n1-n2;if(op.equalsIgnoreCase("Multiply")) n3=n1*n2;if(op.equalsIgnoreCase("Divide")) n3=n1/n2;

t3.setText(d.format(n3)+"");}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 20/26

20 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

35. Design the following Java Form to calculate net payment to be made by a customer.

There are four mode of payments : Cash,Credit Card, Debit Card and Cheque in a combo box.

The modes Credit Cards, Debit Cards and Cheque is not applicable for kids. And so se lecting these modes should disable ‘Kid’customer type.

  Ensure that ONLY numeric values are entered in ‘tPr’ and ‘tQty’ text fields. 

No editing should be allowed in gross, discount amount, additional discount and net text fields.

  When the form starts, ‘Male’ radio button should be selected as the customer type. 

NOTE: The following discount is given for the gross amount greater than 1000 rs. Any bill less than 1000 Rs will not get anydiscount.

CUSTOMERTYPE

PAYMENT MODE DISCOUNT %

MALE CASH 2

CREDIT CARD 5

DEBIT CARD 10

CHEQUE NIL

FEMALE CASH 3

CREDIT CARD 7DEBIT CARD 12

CHEQUE NIL

KID CASH 5

CREDIT CARD NOT APPL

DEBIT CARD NOT APPL

CHEQUE NOT APPL

The customer gets additional discount of 2% if he/she is a member of the Mall.

  To make ‘Male’ radio button selected when the form starts, type the following code just below the ‘initComponents()’ code asshown below:

initComponents();rbM.setSelected(true);

Now to ensure that ONLY numeric values are entered in ‘Price’ and ‘Quantity’ text fields do the following: o  To write a common „KeyTyped‟ event code for these two text fields.o Select these two text boxes:

  From the shortcut menu, select ‘Properties’.   In that dialog box, click on the ‘Events’ tab.  Now click on the ellipsis (…) button in front of ‘keyTyped’ option.   In the ‘Handler’ dialog box, click on ‘Add’ button.   Type a name for the ‘Handler’ say ‘numbers’ and click OK button.   Now click on the drop down box in front of ‘KeyTyped’ method and select the handler name you have just

created i.e. ‘numbers’ in our case.   Now open the ‘source’ window and there you will see the ‘numbers’ method and then type the following piece

of code:

private void numbers(java.awt.event.KeyEvent evt) {char c=evt.getKeyChar();

if( ! ( (c >= '0' && c <= '9') || ( c==evt.VK_DELETE ) || ( c==evt.VK_BACK_SPACE ) ) ){ JOptionPane.showMessageDialog(this,"Only Numbers are Allowed");evt.consume();

} // end if }// end method „numbers‟ 

tProduct

tPr tQty

cmbPayment

chkMember

rbM rbF rbK

tGross

tNet

tDisctAdlDisc

btnCalc

panType

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 21/26

21 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

  Now to disable ‘Kid’ radio button when payment mode is ‘Credit Card’, ‘Debit Card’ and ‘Cheque’, write the following code forthat combo box’s Action Performed event: 

private void cmbPaymentActionPerformed(java.awt.event.ActionEvent evt) {int n=cmbPayment.getSelectedIndex();

 // to disable Credit Card, Debit Card and Cheque if customer is KIDif(n>0) rbK.setEnabled(false);else rbK.setEnabled(true);

}

Now to write the code behind ‘Calculate‟ button:

private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) {double price=Double.parseDouble(tPr.getText());int qty=Integer.parseInt(tQty.getText());double gross=price*qty;double disc=0;double addldisc=0;

 // to get the selected string from the combo boxString modename=cmbPayment.getSelectedItem()+"";

 //or you can even get the index position of the selected mode

int modeposition=cmbPayment.getSelectedIndex();

 // if customer is Maleif(rbM.isSelected()){ 

if(gross>1000){ // here modename is used for comparisonif(modename.equalsIgnoreCase("cash")){ disc=gross*2/100 ; }if(modename.equalsIgnoreCase("credit card")) { disc=gross*5/100; }if(modename.equalsIgnoreCase("debit card")) { disc=gross*10/100; }

}// end gross}//end male

 //if customer is Femaleif(rbF.isSelected()){ 

if(gross>1000){ // here modeposition is used for comparisonif(modeposition==0){ disc=gross*3/100; }if(modeposition==1){ disc=gross*7/100; }if(modeposition==2){ disc=gross*12/100;}

}//end gross}//end female

 //if customer is a Kidif(rbK.isSelected()){ 

if(gross>1000){ // here modeposition is used for comparisonif(modeposition==0) { disc=gross*5/100; }

}//end gross}//end kid

if(chkMember.isSelected()){ addldisc=gross*2/100; }

double totaldisc=disc+addldisc;double net=gross-totaldisc;

tGross.setText(gross+"");tDisc.setText(disc+"");tAddlDisc.setText(addldisc+"");tNet.setText(net+"");

}// end button calc 

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 22/26

22 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

36.  Create a form as shown below: 

  It contains 2 panels namely ‘panPer’ and ‘panTemp’.  When the form loads both these panels are not visible.  On clicking ‘Permanent’ radio button, ‘panPer’ get visible and other one invisible.   On clicking ‘Temporary’ radio button, ‘panTemp’ get visible and other one invisible. 

The criterion for calculation is as given below :For Temporary Employee Salary is Rs. 250 per day.

OverTime is Rs. 50 per overtime hour

For Permanent Employee HRA is 10% of Basic SalaryCCA is Rs. 500

OverTime allowance is Rs. 75 per overtime hour.

Total Amount is the sum total of Salary Amount (TxtSalary) and OverTimeAmount (TxtOverTime). 

private void opPerActionPerformed(java.awt.event.ActionEvent evt) {

panPer.setVisible(true);panTemp.setVisible(false);

}

private void opTempActionPerformed(java.awt.event.ActionEvent evt) {

panPer.setVisible(false);panTemp.setVisible(true);

}

private void txtBasicFocusLost(java.awt.event.FocusEvent evt) {int basic,hra;

try{basic=Integer.parseInt(txtBasic.getText());

}catch(Exception e){

System.out.println("Invalid Basic Amount");txtBasic.requestFocus();

return;}

txtBasic

txtOver

txtHr

txtCCA

panPer(a panel)

txtGross txtOvertime

txtNet

panTemp(a panel)

txtGross txtOvertime

txtNet

txtWork

txtOverTmp

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 23/26

23 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

hra=basic*10/100;

txtHr.setText(""+hra);}

private void txtOverFocusLost(java.awt.event.FocusEvent evt) {int overtime;

try{

overtime=Integer.parseInt(txtOver.getText());}catch(Exception e){

System.out.println("Invalid Overtime Hours");txtOver.requestFocus();return;

}}

private void cmdCalculateActionPerformed(java.awt.event.ActionEvent evt) {int gross,net;int overtimeamt;

if(panPer.isVisible()==true){int basic=Integer.parseInt(txtBasic.getText());

int overtime=Integer.parseInt(txtOver.getText());int hra=Integer.parseInt(txtHr.getText());

int cca=Integer.parseInt(txtCCA.getText());

overtimeamt=overtime*75;

gross=basic+hra+cca;net=gross+overtimeamt;

txtGross.setText(""+gross);txtOvertime.setText(""+overtimeamt);txtNet.setText(""+net);

}

if(panTemp.isVisible()==true){int work=Integer.parseInt(txtWork.getText());int overtime=Integer.parseInt(txtOverTmp.getText());

gross=(work*250);overtimeamt=overtime*50;

net=gross+overtimeamt;

txtGross.setText(""+gross);

txtOvertime.setText(""+overtimeamt);txtNet.setText(""+net);

}}

private void txtWorkFocusLost(java.awt.event.FocusEvent evt) {int workdays;

try{workdays=Integer.parseInt(txtWork.getText());

}catch(Exception e){

System.out.println("Invalid Number....");txtWork.requestFocus();return;

}}

private void txtOverTmpFocusLost(java.awt.event.FocusEvent evt) {

int overtime;try{

overtime=Integer.parseInt(txtOverTmp.getText());

}catch(Exception e){System.out.println("Invalid Overtime Hours");txtOverTmp.requestFocus();

return;}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 24/26

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 25/26

25 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  

38.39. Create a form to show working of all the three dialog boxes of Java namely: 

a. Message Dialog b. Input Dialog c. Confirm Dialog 

First include the following statement at the start of the form: import javax.swing.* ;

The reason is these dialog boxes are part of JOptionPane class which is included in „swing‟ package. 

private void btnMsg2ActionPerformed(java.awt.event.ActionEvent evt) {JOptionPane.showMessageDialog(this,"Welcome To Java Programming");

}

private void btnInputActionPerformed(java.awt.event.ActionEvent evt) {String s=JOptionPane.showInputDialog(this,"Enter Your Name");

JOptionPane.showMessageDialog(this,"Hello " + s + " Welcome To Java Programming");}

private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {int n=JOptionPane.showConfirmDialog(this,"Select your option");

if(n==0)JOptionPane.showMessageDialog(this,"You have selected YES");if(n==1)JOptionPane.showMessageDialog(this,"You have selected NO");

if(n==2)JOptionPane.showMessageDialog(this,"You have selected CANCEL");}

7/31/2019 Class 11 12 Practicals Answers

http://slidepdf.com/reader/full/class-11-12-practicals-answers 26/26

26 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m  


Recommended