日期:2014-05-20 浏览次数:20797 次
class Employee{
/* Declare three instance variables here. */
private String firstName;
private String lastName;
private double salary;
/* Add a constructor that declares a parameter for each instance variable. Assign
each parameter value to the appropriate instance variable. Write code that
validates the value of salary to ensure that it is not negative. */
public Employee(String nameOfFirst, String nameOfLast, double monthlySalary){
firstName = nameOfFirst;
lastName = nameOfLast;
salary = monthlySalary;
}
/* Declare set and get methods for the first name instance variable. */
public void setFirstName(String nameOfFirst){
firstName = nameOfFirst;
}
public String getFirstName(){
return firstName;
}
/* Declare set and get methods for the last name instance variable. */
public void setLastName(String nameOfLast){
lastName = nameOfLast;
}
public String getLastName(){
return lastName;
}
/* Declare set and get methods for the monthly salary instance variable. Write code
that validates the salary to ensure that it is not negative. */
public void setSalary(double monthlySalary){
if(monthlySalary > 0.00)
salary = monthlySalary;
}
public double getSalary(){
return salary;
}
}/* End class declaration of Employee class. */
class EmployeeTest{
/* Begin main method declaration. */
public static void main(String[] args){
/* Create two Employee objects and assign them to Employee variables. */
Employee employee1 = new Employee("Bob","Jones",2875.00);
Employee employee2 = new Employee("Susan","Baker",3150.75);
/* Output the first name, last name and salary for each Employee. */
System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n",
employee1.getFirstName(),employee1.getLastName(),employee1.getSalary()*12);
System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n\n",
employee2.getFirstName(),employee2.getLastName(),employee2.getSalary()*12);
/* Give each Employee a 10% raise. */
System.out.printf("Increasing employee salaries by 10%\n");
/* Output the first name, last name and salary of each Employee again. */
System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n",
employee1.getFirstName(),employee1.getLastName(),employee1.getSalary()*13.2);
System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n\n",
employee2.getFirstName(),employee2.getLastName(),employee2.getSalary()*13.2);
}/* End main method declaration */
}/* End class declaration of EmployeeTest class. */