You are on page 1of 26

1|Page

Java Practicals

subhashv1224@gmail.com

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 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.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+"");

2|Page
3.

Java Practicals

subhashv1224@gmail.com

Create a GUI form to find simple interest. Enter Principal amount, Rate of Interest and time in three different text boxes and 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 void btnClearActionPerformed(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)+""); }

3|Page
7.

Java Practicals

subhashv1224@gmail.com

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 these different 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 will display 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); }

4|Page

Java Practicals

subhashv1224@gmail.com

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 a negative 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) if(n2<n1) if(n1==n2) r="First Number"; r="Second Number"; 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 maximum and 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); }

5|Page

Java Practicals

subhashv1224@gmail.com

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 Marks Percentage > = 90 80 90 75 80 60 75 50 60 40 50 < 40 Grade A++ A+ A B C D Fail

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); }

6|Page

Java Practicals

subhashv1224@gmail.com

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"; else evodd="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"); else t2.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"; else divBy5="Not Divisible by 5"; t2.setText(divBy5); }

7|Page

Java Practicals

subhashv1224@gmail.com

18. Enter a number in a texbox and on clicking a button that number will be appended to a textarea control and text box will be blank. Another Button will show an Input Box and on clicking its OK button the number will be appended to the same textarea 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 the concated string in a third text box. b. and print whether they are same :Ignoring case Case sensitive using equals(), equalsIgnoreCase() and compareTo() methods. c. Print a character at position n of first string d. Reverse the first String using StringBuilder 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)) else s3.setText(res); } res="Both are equal"; res="Not Equal";

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"; else res="Not Equal"; s3.setText(res); }

8|Page

Java Practicals

subhashv1224@gmail.com

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"; else res="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) st a. Print 1 10 numbers in a textarea control. st b. Print 1 10 numbers in reverse in a textarea control. st c. Print 1 10 even numbers in a textarea control. st d. Print 1 10 even numbers in reverse in a textarea control. st e. Print 1 10 odd numbers in a textarea control st f. Print 1 10 odd numbers in reverse in a textarea control. g. Enter a number using Input box and print its table upto x10 in a textarea control. h. Enter a message using Input box and print it 10 times in a textarea control. i. Print characters from A to Z in a textarea control. j. Print all the armstrong numbers in a textarea control. st k. Print 1 10 numbers and their sum in a textarea control. st l. Print 1 10 even numbers and their sum in a textarea control. st m. Print 1 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"); }

9|Page

Java Practicals

subhashv1224@gmail.com

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);

10 | P a g e

Java Practicals

subhashv1224@gmail.com

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 contain b. 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 even and odd digits. h. Display sum of its even digits. i. Display sum of its odd digits. j. Display sum of cube of its individual digits. k. Display sum of square of its individual digits. l. Increase each digit of the number by 1 and display it as a new number. m. Display n Fibonacci numbers in 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); }

11 | P a g e

Java Practicals

subhashv1224@gmail.com

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"); } }

12 | P a g e

Java Practicals

subhashv1224@gmail.com

private void btnFibbSeriesActionPerformed(java.awt.event.ActionEvent evt) { long n=Long.parseLong(t1.getText()); long n1=1; long n2=1; ta1.append("1 = " + n1+"\n"+"2 = "+n2+"\n"); long i=3; while(i<=n){ long n3=n1+n2; ta1.append(i+" = "+ n3+"\n"); n1=n2; n2=n3; i++; } } private void btnMaxDigitActionPerformed(java.awt.event.ActionEvent evt) { long n=Long.parseLong(t1.getText()); long max=0; while(n>0){ long dig=n%10; if(dig>max) max=dig; n=n/10; } t2.setText("The Maximum digit is : "+ max);

private void btnReverseNumberActionPerformed(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; n=n/10; } t2.setText("The Reverse Number is : "+ newn); }

private void btnMinDigitActionPerformed(java.awt.event.ActionEvent evt) { long n=Long.parseLong(t1.getText()); long min=0; long dig=n%10; min=dig; n=n/10; while(n>0){ dig=n%10; if(dig<min) min=dig; n=n/10; } t2.setText("The Minimum digit is : "+ min);

private void btnSumOddActionPerformed(java.awt.event.ActionEvent evt) { long n=Long.parseLong(t1.getText()); long sumodd=0; while(n>0){ long dig=n%10; if(dig%2!=0) sumodd=sumodd+dig; n=n/10; } t2.setText("The Sum of Odd digits is : "+ sumodd);

private void btnSumSquareDigitsActionPerformed(java.awt.event.ActionEvent evt) { long n=Long.parseLong(t1.getText()); long sumsqr=0; while(n>0){ long dig=n%10; sumsqr=sumsqr+(dig*dig); n=n/10; } t2.setText("The Sum of Square of digits is : "+ sumsqr);

13 | P a g e

Java Practicals

subhashv1224@gmail.com

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)); }

14 | P a g e

Java Practicals

subhashv1224@gmail.com

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; else return 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 sum b. Print all the even numbers between them as well as the sum of those even numbers c. 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); }

15 | P a g e

Java Practicals

subhashv1224@gmail.com

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"); }

16 | P a g e

Java Practicals

subhashv1224@gmail.com

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

nd

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 max if(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 so as to make only one radio button selectable in that panel. Now, display which radio button has been selected in a message box by: a. Creating a button b. 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){ if(rbF.isSelected()==true){ if(rbK.isSelected()==true){ } private void custType(java.awt.event.ActionEvent evt) { String msg=""; if(rbM.isSelected()){ if(rbF.isSelected()){ if(rbK.isSelected()){ msg="The customer is Male:Common Code"; msg="The customer is Female:Common Code"; msg="The customer is a Kid:Common Code"; } } } } msg="The customer is Male:Button Clicked"; msg="The customer is Female:Button Clicked"; msg="The customer is a Kid:Button Clicked"; } } }

JOptionPane.showMessageDialog(this,msg);

JOptionPane.showMessageDialog(this,msg);

17 | P a g e

Java Practicals

subhashv1224@gmail.com

26. Create a combo box and add Jaipur, Kota, Ajmer and Udaipur in it. Now, display the selected value and its index position in a message box by: a. Creating a button b. 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

* ** *** ****

18 | P a g e

Java Practicals

subhashv1224@gmail.com

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 on prompting 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 and a. 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 the combo box after confirming it from the 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(""); }

19 | P a g e

Java Practicals

subhashv1224@gmail.com

32. Create a calculator using four radio buttons that will perform all the four basic mathematical operations. Write a common code for all the four radio buttons 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 an option 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)+""); }

20 | P a g e

Java Practicals

subhashv1224@gmail.com

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 selecting 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.
tProduct tPr cmbPayment panType chkMember btnCalc rbM rbF rbK tGross tAdlDisc tDisc tNet tQty

NOTE: The following discount is given for the gross amount greater than 1000 rs. Any bill less than 1000 Rs will not get any discount. CUSTOMER PAYMENT MODE DISCOUNT % TYPE MALE CASH 2 CREDIT CARD 5 DEBIT CARD 10 CHEQUE NIL FEMALE CASH 3 CREDIT CARD 7 DEBIT 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 as shown 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

21 | P a g e

Java Practicals

subhashv1224@gmail.com

Now to disable Kid radio button when payment mode is Credit Card, Debit Card and Cheque, write the following code for that combo boxs 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 KID if(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 box String modename=cmbPayment.getSelectedItem()+""; //or you can even get the index position of the selected mode int modeposition=cmbPayment.getSelectedIndex(); // if customer is Male if(rbM.isSelected()){ if(gross>1000){ // here modename is used for comparison if(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 Female if(rbF.isSelected()){ if(gross>1000){ // here modeposition is used for comparison if(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 Kid if(rbK.isSelected()){ if(gross>1000){ // here modeposition is used for comparison if(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

22 | P a g e

Java Practicals

subhashv1224@gmail.com

36. Create a form as shown below:

txtBasic txtOver panPer (a panel)

txtHr txtCCA

txtGross

txtOvertime

txtNet

txtWork

txtOverTmp

panTemp (a panel)

txtGross

txtOvertime

txtNet

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 Salary CCA is Rs. 500 OverTime allowance is Rs. 75 per overtime hour. Total Amount is the sum total of Salary Amount (TxtSalary) and OverTime Amount (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; }

23 | P a g e
hra=basic*10/100; txtHr.setText(""+hra); }

Java Practicals

subhashv1224@gmail.com

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; }

24 | P a g e

Java Practicals

subhashv1224@gmail.com

37. Input marks of 5 subjects and print its total, average and grade. Grade is based on the following rules: Average Grade >=90 S >=80 and <90 A >=70 and <80 B >=60 and <70 C >=50 and <60 D >=40 and <50 E <40 F o Write code to ensure that ONLY numeric values are entered in 5 subjects fields. o Write code so as to ensure that no blank value is given in any of the 5 subjects fields. o Write code to ensure that ONLY value between 0-100 is entered in any of the 5 subjects fields. o Make Total, %age and Grade fields un-editable. private void tEngKeyTyped(java.awt.event.KeyEvent evt) { char ch=evt.getKeyChar(); if(!((ch>='0'&&ch<='9') || (ch==evt.VK_BACK_SPACE))){ JOptionPane.showMessageDialog(this,"Only Numbers and BACKSPACE Are Allowed"); } } private void tPhyKeyTyped(java.awt.event.KeyEvent evt) { char ch=evt.getKeyChar(); if(!((ch>='0'&&ch<='9') || (ch==evt.VK_BACK_SPACE))){ JOptionPane.showMessageDialog(this,"Only Numbers and BACKSPACE Are Allowed"); } } private void tCheKeyTyped(java.awt.event.KeyEvent evt) { char ch=evt.getKeyChar(); if(!((ch>='0'&&ch<='9') || (ch==evt.VK_BACK_SPACE))){ JOptionPane.showMessageDialog(this,"Only Numbers and BACKSPACE Are Allowed"); } } private void tMatKeyTyped(java.awt.event.KeyEvent evt) { char ch=evt.getKeyChar(); if(!((ch>='0'&&ch<='9') || (ch==evt.VK_BACK_SPACE))){ JOptionPane.showMessageDialog(this,"Only Numbers and BACKSPACE Are Allowed"); } } private void tInfKeyTyped(java.awt.event.KeyEvent evt) { char ch=evt.getKeyChar(); if(!((ch>='0'&&ch<='9') || (ch==evt.VK_BACK_SPACE))){ JOptionPane.showMessageDialog(this,"Only Numbers and BACKSPACE Are Allowed"); } } private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) { DecimalFormat d=new DecimalFormat("#0.00"); int eng=0,phy=0,che=0,mat=0,inf=0; double tot,avg; String gr=""; try{ eng=Integer.parseInt(tEng.getText()); phy=Integer.parseInt(tPhy.getText()); che=Integer.parseInt(tChe.getText()); mat=Integer.parseInt(tMat.getText()); inf=Integer.parseInt(tInf.getText()); if(eng<0 || eng>100){JOptionPane.showMessageDialog(this,"Invalid English Marks");tEng.requestFocus();return;} if(phy<0 || phy>100){JOptionPane.showMessageDialog(this,"Invalid Physics Marks");tPhy.requestFocus();return;} if(che<0 || che>100){JOptionPane.showMessageDialog(this,"Invalid Chemistry Marks");tChe.requestFocus();return;} if(mat<0 || mat>100){JOptionPane.showMessageDialog(this,"Invalid Maths Marks");tMat.requestFocus();return;} if(inf<0 || inf>100){JOptionPane.showMessageDialog(this,"Invalid Info Marks");tInf.requestFocus();return;} }catch(Exception e){ JOptionPane.showMessageDialog(this,"One of the Subject Field is BLANK"); tEng.requestFocus(); String calcGrade(double n){ return; String g=""; } tot=eng+che+phy+mat+inf; avg=tot/500.0*100.0; gr=calcGrade(avg); tSum.setText(tot+""); tPercent.setText(d.format(avg)+"%"); tGrade.setText(gr+""); } if(n>=90) g="S"; else if (n>=80) g="A"; else if (n>=70) g="B"; else if (n>=60) g="C"; else if (n>=50) g="D"; else if (n>=40) g="E"; else if (n<40) g="FAIL"; return g; }

evt.consume();

evt.consume();

evt.consume();

evt.consume();

evt.consume();

25 | P a g e

Java Practicals

subhashv1224@gmail.com

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"); }

26 | P a g e

Java Practicals

subhashv1224@gmail.com

You might also like