Component Type Purpose txtName JTextField Input for name txtYears JTextField Inp
ID: 2246784 • Letter: C
Question
Component
Type
Purpose
txtName
JTextField
Input for name
txtYears
JTextField
Input for years
txtSalary
JTextField
Input for salary
btnCalc
JButton
Click to calculate bonus
txtBonusPercent
JTextField
Displays bonus percentage
txtBonusAmount
JTextField
Displays bonus amount
Component
Type
Purpose
txtName
JTextField
Input for name
txtYears
JTextField
Input for years
txtSalary
JTextField
Input for salary
btnCalc
JButton
Click to calculate bonus
txtBonusPercent
JTextField
Displays bonus percentage
txtBonusAmount
JTextField
Displays bonus amount
You DO NOT have to code the GUI.
The action listener for btnCalc is set up as follows.
btnCalc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calcBonus(); //write the code for this method
}
});istener for btnCalc is set up as follows.
btnCalc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calcBonus(); //write the code for this method
}
});
Component
Type
Purpose
txtName
JTextField
Input for name
txtYears
JTextField
Input for years
txtSalary
JTextField
Input for salary
btnCalc
JButton
Click to calculate bonus
txtBonusPercent
JTextField
Displays bonus percentage
txtBonusAmount
JTextField
Displays bonus amount
Explanation / Answer
public class Employee {
// Member Variables
private String Name;
private int Years;
private double Salary;
public Employee() { // Default Constructor
Name = "NA";
Years = 0;
Salary = 0.00;
}
/**
* Parameterized Constructor
* @param name
* @param years
* @param salary
*/
public Employee(String name, int years, double salary) {
Name = name;
Years = years;
Salary = salary;
}
/**
* Method to return Name
* @return String
*/
public String getName() {
return Name;
}
/**
* Method to return the number of years
* @return int
*/
public int getYears() {
return Years;
}
/**
* Method to return the salary amount
* @return double
*/
public double getSalary() {
return Salary;
}
/**
* Method to set name
* @param name
*/
public void setName(String name) {
Name = name;
}
/**
* Method to set years
* @param years
*/
public void setYears(int years) {
Years = years;
}
/**
* Method to set salary amount
* @param salary
*/
public void setSalary(double salary) {
Salary = salary;
}
/**
* Method to calculate and return the bonus percentage
* @return
*/
public int getBonusRate()
{
int bonus = 0;
if(getYears() < 5)
bonus = 3;
else if(getYears() >= 5 && getYears() <= 14)
bonus = 7;
else if(getYears() >= 15)
bonus = 12;
return bonus;
}
/**
* Method to calculate and return the bonus amount based on bonus rate
* @return
*/
public double getBonusAmount()
{
int rate = getBonusRate();
double salary = getSalary();
return (rate * salary) / 100;
}
}
btnCalc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Input validation
if(txtName.getText().equals("") || txtYears.getText().equals("") ||
txtSalary.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please input all the fields.");
}
else
{
try{
String name = txtName.getText();
int years = Integer.parseInt(txtYears.getText());
double salary = Double.parseDouble(txtSalary.getText());
Employee emp = new Employee(name, years, salary);
int bonusRate = emp.getBonusRate();
double bonusAmount = emp.getBonusAmount();
txtBonusPercent.setText(bonusRate+"");
txtBonusAmount.setText(bonusAmount+"");
}
catch(NumberFormatException ex) // catching any illegal input such as String input in salary field
{
JOptionPane.showMessageDialog(null, "Invalid year/salary input!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
Employee Classpublic class Employee {
// Member Variables
private String Name;
private int Years;
private double Salary;
public Employee() { // Default Constructor
Name = "NA";
Years = 0;
Salary = 0.00;
}
/**
* Parameterized Constructor
* @param name
* @param years
* @param salary
*/
public Employee(String name, int years, double salary) {
Name = name;
Years = years;
Salary = salary;
}
/**
* Method to return Name
* @return String
*/
public String getName() {
return Name;
}
/**
* Method to return the number of years
* @return int
*/
public int getYears() {
return Years;
}
/**
* Method to return the salary amount
* @return double
*/
public double getSalary() {
return Salary;
}
/**
* Method to set name
* @param name
*/
public void setName(String name) {
Name = name;
}
/**
* Method to set years
* @param years
*/
public void setYears(int years) {
Years = years;
}
/**
* Method to set salary amount
* @param salary
*/
public void setSalary(double salary) {
Salary = salary;
}
/**
* Method to calculate and return the bonus percentage
* @return
*/
public int getBonusRate()
{
int bonus = 0;
if(getYears() < 5)
bonus = 3;
else if(getYears() >= 5 && getYears() <= 14)
bonus = 7;
else if(getYears() >= 15)
bonus = 12;
return bonus;
}
/**
* Method to calculate and return the bonus amount based on bonus rate
* @return
*/
public double getBonusAmount()
{
int rate = getBonusRate();
double salary = getSalary();
return (rate * salary) / 100;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.