0% found this document useful (0 votes)
14 views

Ch4 Solutions

The document describes 4 lab exercises to create Java GUI applications: 1. A program to reverse a string input by the user. 2. A program to generate random whole numbers between two numbers set by the user. 3. A program to calculate the sum, average, maximum or minimum of 3 numbers input by the user. 4. A program to convert a date from separate inputs for date, month, and year to a single formatted output of dd/mm/yy, with validation checks.

Uploaded by

Baron
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Ch4 Solutions

The document describes 4 lab exercises to create Java GUI applications: 1. A program to reverse a string input by the user. 2. A program to generate random whole numbers between two numbers set by the user. 3. A program to calculate the sum, average, maximum or minimum of 3 numbers input by the user. 4. A program to convert a date from separate inputs for date, month, and year to a single formatted output of dd/mm/yy, with validation checks.

Uploaded by

Baron
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB EXERCISES

1. ​Create a GUI application to accept a string and display it in reverse order using the
substring() method.

A) Design;

Coding;

jButton1 ((‘Reverse’ button)


String x,y;
y="";
x=jTextField1.getText();
int z=x.length();
for(int i=z-1;i>=0;i--)
{
y=y+x.substring(i,i+1);
}
jTextField2.setText(y);

jButton2 (‘Refresh’ button)


jTextField1.setText(" ");
jTextField2.setText(" ");

jButton3 (‘Close’ button)


System.exit(0);

Output;

2. Create a GUI application to create random whole numbers between 2 float numbers
input by the user.
A) Design;
Coding;

jButton2 (‘Refresh’ button)


jTextField1.setText(“”);
jTextField2.setText(“”);
jTextField3.setText(“”);

jButton3 (‘Close’ button)


System.exit(0);

jButton1 (‘Generate random whole numbers’ button)


-
3. Create a GUI application to accept 3 numbers in separate text fields and display
their sum, average, maximum or minimum after rounding the results on the click of
appropriate buttons (There are four separate buttons - one for sum, one for
average, one for maximum and one for minimum). The result should be displayed in
the fourth text field.
A)

Coding;

jButton1 (‘Sum’ button)


double fn=Double.parseDouble(jTextField1.getText());
double sn=Double.parseDouble(jTextField2.getText());
double tn=Double.parseDouble(jTextField2.getText());
double sum=fn+sn+tn;
jTextField4.setText(Double.toString(+sum));

jButton2 (‘Average’ button)


double fn=Double.parseDouble(jTextField1.getText());
double sn=Double.parseDouble(jTextField2.getText());
double tn=Double.parseDouble(jTextField2.getText());
double avg=fn+sn+tn/3;
jTextField4.setText(Double.toString(+avg));

jButton3 (‘Maximum’ button)


double fn=Double.parseDouble(jTextField1.getText());
double sn=Double.parseDouble(jTextField2.getText());
double tn=Double.parseDouble(jTextField2.getText());
if (fn>sn && fn>tn){
jTextField4.setText(""+fn);}
else if (sn>fn && sn>tn){
jTextField4.setText(""+sn);}
else if (tn>fn && tn>sn){
jTextField4.setText(""+tn);}
else
jTextField4.setText("The numbers are not distinct.");

jButton4 (‘Minimum’ button)


double fn=Double.parseDouble(jTextField1.getText());
double sn=Double.parseDouble(jTextField2.getText());
double tn=Double.parseDouble(jTextField2.getText());
if (fn<sn && fn<tn){
jTextField4.setText(""+fn);}
else if (sn<fn && sn<tn){
jTextField4.setText(""+sn);}
else if (tn<fn && tn<sn){
jTextField4.setText(""+tn);}
else
jTextField4.setText("The numbers are not distinct.");

jButton5 (‘Refresh’ button)


jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jButton6 (‘Close’ button)
System.exit(0);

Output;

4. Create a GUI application to accept the date (as 1), month (as a number like 3 for
March) and year (as 2010) in separate text fields and display the date in the format:
dd/mm/yy. Take care of the following points while creating the application:
● Verify the date input before displaying it in the suggested format and display
error messages wherever applicable.
● The date is accepted as 1 (for the first of any month) but should be displayed as
01 in the final format.
● The year is accepted as ‘2010’ but displayed as 10 in the final format.
A) Design;

Coding;

jButton2 (‘Refresh’ button)


jTextField1.setText(" ");
jTextField2.setText(" ");
jTextField3.setText(" ");
jTextField4.setText(" ");

jButton3 (‘Close’ button)


System.exit(0);

jButton2 (‘Convert to the format’ button)


int date=Integer.parseInt(jTextField1.getText());
int month=Integer.parseInt(jTextField2.getText());
int year=Integer.parseInt(jTextField3.getText());
if(date>31 || date<1){
JOptionPane.showMessageDialog(null,"Invalid date.");}

if (month>12 || month<1){
JOptionPane.showMessageDialog(null,"Invalid month.");}

You might also like