- Back to Home »
- Review Final Exam (Year 3) »
- Java Programming Review Answer In Final Exam 22-23, 12, 2012
Posted by : Unknown
Thursday, December 20, 2012
កូដនៅសរសេរនៅក្នុង EmployeeForm.java
គឺ ៖
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.List;
public class EmployeeForm extends Frame
implements
ActionListener {
Vector
<Employee> vs=new Vector<Employee>();
TextField
txtCode=new TextField();
TextField
txtName =new TextField();
TextArea
txtView=new TextArea();
Choice
cSex=new Choice();
CheckboxGroup
g=new CheckboxGroup();
Checkbox
cm=new Checkbox("Male",g,true);
Checkbox
cf=new Checkbox("Female",g,false);
TextField
txtSalary=new TextField();
Button
badd=new Button("ADD");
Button
bView=new Button("VIEW");
Button
bDelete=new Button("Delete");
Button
bClose=new Button("CLOSE");
List
lst=new List();
public
EmployeeForm() {
this.setTitle("Employee
Form ");
this.setVisible(true);
this.setSize(500,300);
Panel p=new Panel();
p.setLayout(new
GridLayout(4,2,7,7));
p.add(new
Label("Code:"));p.add(txtCode);
p.add(new
Label("Name:"));p.add(txtName);
p.add(new Label("Sex"));
Panel p1=new Panel();
p1.add(cm);p1.add(cf);
p.add(p1);
p.add(new
Label("Salary"));p.add(txtSalary);
this.add("North",p);
p=new Panel();
p.add(badd);
p.add(bView);
p.add(bDelete);
p.add(bClose);
this.add("Center",p);
this.add("South",lst);
this.addWindowListener(new WindowAdapter(){
public void
windowClosing(WindowEvent e){
System.exit(0);}});
badd.addActionListener(this);
bView.addActionListener(this);
bDelete.addActionListener(this);
bClose.addActionListener(this);
}
public
void actionPerformed(ActionEvent e){
if(e.getSource()==badd){
for (int i = 0;
i<vs.size(); i++){
if(vs.elementAt(i).code.equalsIgnoreCase(txtCode.getText())){
return
;
}
}
Employee sobj=new
Employee();
sobj.setCode(txtCode.getText());
sobj.setName(txtName.getText());
if(g.getSelectedCheckbox()==cm)
sobj.setSex("Male");
else
sobj.setSex("Female");
sobj.setSalary(Integer.valueOf(txtSalary.getText()));
vs.add(sobj);
}else if (e.getSource()==bView){
g.setSelectedCheckbox(cm);
lst.clear();
for (int i = 0;
i<vs.size(); i++)
lst.add(vs.elementAt(i).toString());
}else if (e.getSource()==bDelete){
vs.removeElementAt(lst.getSelectedIndex());
lst.remove(lst.getSelectedIndex());
}else System.exit(0);
}
public
static void main (String[] args) {
new
EmployeeForm();
}
}
ការសរសេរកូដនៅក្នុង
Employee.java
គឺ ៖
import
java.util.*;
public
class Employee implements Comparable {
String code;
String name; String sex; int salary;
void setCode(String c){code=c; }
void setName(String n){name=n; }
void setSex(String s){sex=s; }
void setSalary(int s){salary=s; }
public String toString(){
return
code + " | " + name + " | " + sex + " | " +
salary;
}
public int compareTo(Object obj){
Employee
sobj=(Employee)obj;
return
this.name.compareToIgnoreCase(sobj.name);
}
}