You are on page 1of 8

Experiment No.

1
Using JLable, JTextfield and JButton
Selection Control Structure(if-else-if Construct)

Problem Definition:Consider the following JFrame.

1. When the user the calculate percentage button, calculate the percentage marks of
the subjects if the student passes in all the subjects otherwise display percentage
marks as follows:
Passing marks=35 maximum marks=100
2. When the user clicks calculate grade and result button, calculate grade and result
as follows:

Astrid Ana Gomes

PERCENTAGE
Greater than or equal
75
Greater than or equal
65
Greater than or equal
55
Greater than or equal
45
Greater than or equal
35
Less than 35

GRADE
to

to

to

to

to

E
F

Result:
If a student fails in one subject, his result is compartment.
If a student fails in more than one subject, his result is fail otherwise it is
pass.
3. When the user clicks on the clear button, clear all textfields and disable the
calculate grade and result button and change the editable status of textfields
Total Marks, Percentage Marks, Grade and Result to false.
4. When the user clicks on the exit button, terminate the program.
Program code:public class EXP1 extends javax.swing.JFrame {
int count;
double pm;
public EXP1() {
initComponents(); }
private void btncalculateActionPerformed(java.awt.event.ActionEvent evt) {
double eng,phy,chem,bio,maths,tm=0.00;
eng=Double.parseDouble(txteng.getText());
phy=Double.parseDouble(txtphy.getText());
maths=Double.parseDouble(txtmaths.getText());
Astrid Ana Gomes

bio=Double.parseDouble(txtbio.getText());
chem=Double.parseDouble(txtchem.getText());
count=0;
if(eng<35)
count++;
if(phy<35)
count++;
if(bio<35)
count++;
if(chem<35)
count++;
if(maths<35)
count++;

if(count==0)
{tm=eng+phy+chem+bio+maths;
pm=tm/500.00*100.00;
txttm.setText(" "+tm);
txtpm.setText(" "+pm);}
else
{txttm.setText("-");
txtpm.setText("-");}

btngrade.setEnabled(true);}

Astrid Ana Gomes

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


char grade=' ';
String result=null;

if(pm>=75)
grade='A';
else if(pm>=65)
grade='B';
else if(pm>=55)
grade='C';
else if(pm>=45)
grade='D';
else if(pm>=35)
grade='E';
else grade='F';
txtgrade.setText(" "+grade);
if (count==0)
txtresult.setText("pass");
else if(count==1)
txtresult.setText("compartment");
else if(count>1)
txtresult.setText("fail");}

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


txtadmno.setText(null);
txtname.setText(null);
Astrid Ana Gomes

txtclass.setText(null);
txtsec.setText(null);
txteng.setText(null);
txtbio.setText(null);
txtmaths.setText(null);
txtchem.setText(null);
txtphy.setText(null);
txttm.setText(null);
txtpm.setText(null);
txttm.setEditable(false);
txtpm.setEditable(false);
txtgrade.setEditable(false);
txtresult.setEditable(false);}

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


System.exit(0);}

public static void main(String args[]) {


try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break; } }
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(EXP1.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
Astrid Ana Gomes

} catch (InstantiationException ex) {


java.util.logging.Logger.getLogger(EXP1.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EXP1.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EXP1.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EXP1().setVisible(true); } }); }

private javax.swing.JButton btncalculate;


private javax.swing.JButton btnclear;
private javax.swing.JButton btnexit;
private javax.swing.JButton btngrade;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel2;
Astrid Ana Gomes

private javax.swing.JLabel jLabel3;


private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField txtadmno;
private javax.swing.JTextField txtbio;
private javax.swing.JTextField txtchem;
private javax.swing.JTextField txtclass;
private javax.swing.JTextField txteng;
private javax.swing.JTextField txtgrade;
private javax.swing.JTextField txtmaths;
private javax.swing.JTextField txtname;
private javax.swing.JTextField txtphy;
private javax.swing.JTextField txtpm;
private javax.swing.JTextField txtresult;
private javax.swing.JTextField txtsec;
private javax.swing.JTextField txttm;}

Astrid Ana Gomes

Input/Output:-

Astrid Ana Gomes

You might also like