LAB EXERCISES
1. Design a GUI application in which the user enters a number in the text field and on
clicking the button the sum of the digits of the number should be displayed in a label.
Hint : Suppose user enters 123 the output should be 6(1+2+3).
A) Design;
2. Design a GUI application to accept a String from the user in a text field and print using
JOptionPane whether it is a palindrome or not. Hint ABBA is a palindrome.
A) Design;
3. Design a GUI application to accept the cost price and selling price form the user in two
text fields then calculate the profit or loss incurred.
A) Design;
Coding for jButton1 (“Calculate the profit/loss” button);
double sp,cp,r;
sp=Double.parseDouble(jTextField1.getText());
cp=Double.parseDouble(jTextField2.getText());
r=sp-cp;
jTextField3.setText(Double.toString(r));
Coding for jButton2 (“Refresh” button);
jTextField1.setText(" ");
jTextField2.setText(" ");
jTextField3.setText(" ");
Coding for jButton3 (“Close” button);
System.exit(0);
Output;
4. Design a GUI application to accept a character in a text field and print in a label if that
character is a vowel: a, e, i, o, or u. The application should be case sensitive.
A) Design view;
Coding for jButton1 (“Check if it is a vowel”);
String in;
in=jTextField1.getText();
switch (in){
case "a":
case "e":
case "i":
case "o":
case "u":
case "A":
case "E":
case "I":
case "O":
case "U":
jLabel4.setText(in+" is a vowel.");
break;
default:
jLabel4.setText(in+" is not a vowel. It is a consonent.");}
}
Coding for jButton2 (“Refresh”);
jTextField1.setText(" ");
jLabel4.setText(" ");
Coding for jButton3 (“Close”);
System.exit(0);
Output;
5. Design a GUI application that repeatedly accepts numbers in a JOptionPane and once
the typed number is 0 the maximum and minimum of all numbers typed are displayed.
A)
6. Design a GUI application in java to convert temperature from Celsius to Fahrenheit or
vice versa using radio buttons and two text fields.
A) Design view;
Coding for jButton1 (‘Convert’);
double t,rt;
t=Double.parseDouble(jTextField1.getText());
rt=0;
if (jRadioButton1.isSelected()==true){
rt=(t*9/5)+32;}
if (jRadioButton2.isSelected()==true){
rt=(t-32)*5/9;}
jTextField2.setText(Double.toString(+rt));
Coding for jButton2 (‘Refresh’);
jTextField1.setText(" ");
jTextField2.setText(" ");
jRadioButton1.setSelected(false);
jRadioButton2.setSelected(false);
Coding for jButton3 (‘Close’);
System.exit(0);
Output;
7. Design a GUI application in java to convert kilograms into grams, litres into milliliters,
rupees into paisa using combobox and text fields.
A)
Coding for jButton1 (‘Convert now’);
double in,r;
in=Double.parseDouble(jTextField1.getText());
r=0;
if (jComboBox1.getSelectedItem().equals("Kilograms to grams")){
r=in*1000;}
if (jComboBox1.getSelectedItem().equals("Litres to mililitres")){
r=in*1000;}
if (jComboBox1.getSelectedItem().equals("Rupees to paisa (INR)")){
r=in*100;}
jTextField2.setText(Double.toString(+r));
Coding for jButton2 (‘Refresh’);
jTextField1.setText(" ");
jTextField2.setText(" ");
jComboBox1.setSelectedIndex(0);
Coding for jButton3 (‘Close’);
System.exit(0);
Output;
8. A book publishing house decided to go in for computerization. The database will be
maintained at the back end but you have to design the front end for the company. You
have to accept book code, Title, Author and Quantity sold from the user. The Price will be
generated depending upon the book code. Net price should be calculated on the basis of
the discount given.
Bookseller - 25%
School - 20%
Customer - 5%
A)
Coding for jButton1 (‘Calculate the net price button’);
int code,qty;
code=Integer.parseInt(jTextField1.getText());
qty=Integer.parseInt(jTextField4.getText());
float price,net,bsdis,schdis,cusdis;
price=0;
bsdis=25/100*price;
schdis=20/100*price;
cusdis=5/100*price;
Coding for jButton2 (‘Refresh’);
jTextField1.setText(" ");
jTextField2.setText(" ");
jTextField3.setText(" ");
jTextField4.setText(" ");
jTextField5.setText(" ");
Coding for jButton3 (‘Close’);
System.exit(0);
Output;
9. A networking company decided to computerize its employee salary . Develop an
application to store employee's personal data which will be saved in the back end. The
front end should accept Name, Father's Name, Mother's Name, Address, Gender, Basic
Salary, Medical and Conveyance. Calculate gross and net salary.
A)