0

This is my assignment and write following code for this, its compile but not run. Please check and guide me to solve this problem. I used jdk1.4 for this.

Question:
You are required to develop a GUI based “Age Calculator” using applets

It should take input from the students for Name, Student ID and Date of birth.
On pressing “Calculate” button, the program should compare his date of birth with the current date and calculate his age.
It should show his name, roll no, age; and any of the followings
In case of age 18 to 28, it should display message “Congratulation! You are eligible”.
If his age is below 18 then it should display message “Under age”.
If his age is more than 30 then it should display message “Over age”.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;


/*<html>
<applet code="AgeCal.class" width=275 height=200">
</applet>
</html>*/


public class AgeCal extends Applet implements ActionListener
{
Button Calculate;
Label label1, label2, label3, label4, label5;
TextField sname, sid, sage;


public void init()
{
setLayout(new FlowLayout());


label1 = new Label("        Age Calculation     ");
label2 = new Label("Name    :");
sname = new TextField("name", 20);
label3 = new Label("Student ID:");
sid = new TextField("id", 20);
label4 = new Label("DOB");
sage = new TextField("dd-mm-yyy", 20);
Calculate = new Button("        Calculate       ");
label5 = new Label("                    ");


add(label1);
add(sname);
add(label2);
add(sid);
add(label3);
add(sid);
add(label4);
add(sage);
add(Calculate);
add(label5);


Calculate.addActionListener(this);
sage.addActionListener(this);
}
public void pain(Graphics g)
{


}


public void actionPerformed(ActionEvent evt)
{
Date pd = new Date();
DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
DateFormat y = new SimpleDateFormat("yyyy");


try
{
Date dob = df.parse(sage.getText());
Calendar calendarA = Calendar.getInstance();
Calendar calendarB = Calendar.getInstance();



int multiplier;
if(dob.getTime() - pd.getTime() > 0)
{
multiplier = -1;
calendarA.setTime(dob);
calendarB.setTime(pd);
}
else
{
multiplier = 1;
calendarA.setTime(pd);
calendarB.setTime(dob);
}
int years = calendarA.get(Calendar.YEAR) - calendarB.get(Calendar.YEAR);
int months = calendarA.get(Calendar.MONTH) - calendarB.get(Calendar.MONTH);
int days = calendarA.get(Calendar.DAY_OF_MONTH) - calendarB.get(Calendar.DAY_OF_MONTH);
if(years > 0 && (months < 0 || (months == 0 && days < 0)))
{
years -= 1;
}


if(years < 18)
label5.setText("Under Age   ");
if(years >= 18)
label5.setText("Congratulation! you are eligible    ");
if(years > 30)
label5.setText("Over Age    ");


}
catch(Exception ex)
{
}


repaint();
}
}

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Acidburn
0

Can you use code tags please. Its the rules!

What does it do when it runs? Does it start to execute? I've noticed you've commented out some code at the top.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.