Calling A Created Class In A Test Class In Java -
i have created public class: employee. create drive class test new class. however, cannot figure out how call created class within driver class: employeetest. have placed each class (employee , employeetest) in same directory; however, still receiving error message: "cannot find class employee."
can me on right track?
here code class employee:
package employee; /** * * @author ljferris */ public class employee { private string first; // instance variable first name private string last; // instance variable last name public double salary; // instance variable monthly salary // constructor initializes first parameter firstname , intializes last parameter lastname public employee(string firstname, string lastname){ this.first = firstname; this.last = lastname; } // constructor initializes salary parameter monthlysalary public employee(double monthlysalary){ this.salary = monthlysalary; } // method set first , last name public void setname(string firstname, string lastname){ this.first = firstname; this.last = lastname; } // method set monthly salary public void setsalary (double monthlysalary){ this.salary = monthlysalary; if (salary > 0.0) this.salary = monthlysalary; } // method retrive first , last name public string getname(){ return first + last; } // method retrive monthly salary public double getsalary (){ return salary; } } // end class employee here code employeetest:
package employeetest; /** * * @author ljferris */ import java.util.scanner; public class employeetest { public static void main(string[] args) { employee employee1 = new employee("leviticus ferris", 1200.00); }
as per code employee , employeetest in different packages. need add import employee in employeetest class. can create new employee instance employeetest.
package employeetest; import employee; import java.util.scanner; public class employeetest { public static void main(string[] args) { employee employee1 = new employee("leviticus ferris", 1200.00); } update below comment:
add 1 more constructor firstname , salary parameters.
public employee(string firstname, double salary){ this.first = firstname; this.salary = salary; } if want 3 values intialized. add 1 more constructor taking fields.
public employee(string firstname, string lastname, double salary){ this.first = firstname; this.last = lastname; this.salary = salary; }
Comments
Post a Comment