| Author |
toString() method overloading
|
sandip pol
Greenhorn
Joined: Apr 29, 2005
Posts: 3
|
|
dear friends i have created 3 java files 1) Employee 2) Nameof_emp 3) Emploeetest which has main method my problem is how should i overload tostring method of Nameof_emp to get full name of person Here is Employee class package My_emp_project; public class Employee { private Nameof_emp name; private double in_time; private double out_time; private double work_h; public Employee(String s1, String s2, String s3, double y, double z) { this.name = new Nameof_emp(s1, s2, s3); in_time = y; out_time = z; } public Employee(String s1, String s2, double y, double z) { this.name = new Nameof_emp(s1, s2); in_time = y; out_time = z; } public Employee(String s1, double y, double z) { this.name = new Nameof_emp(s1); in_time = y; out_time = z; } protected Nameof_emp setnameof_emp(String s1,String s2,String s3) { return new Nameof_emp(s1,s2,s3); } protected Nameof_emp setnameof_emp(String s1,String s2) { return new Nameof_emp(s1,s2); } protected Nameof_emp setnameof_emp(String s1) { return new Nameof_emp(s1); } protected void set_in_time(double in) { this.in_time = in; } protected void set_out_time(double out) { this.out_time = out; } protected double set_work_hour() { work_h = (out_time) - (in_time); return work_h; } protected String get_work_hour() { return Double.toString(work_h); } public String toString() { return "my Dear\t " + this.name + "\n your intime:\t" + this.in_time + "\n outtime\t" + this.out_time + "\n work hour:\t" + Double.toString(this.set_work_hour()); } } now Employeename class package My_emp_project; public class Nameof_emp { private String first_n; private String middle_n; private String Last_n; public Nameof_emp(String x, String y, String z) { first_n = x; middle_n = y; Last_n = z; } public Nameof_emp(String x, String y) { first_n = x; Last_n = y; } public Nameof_emp(String x) { first_n = x; } public String toString() { return first_n; } } Employeetest class package My_emp_project; public class Employeetest { public static void main(String[] args) { Employee g2 = new Employee("jayesh","Khanvilkar", 9.45, 13.45); Employee g1 = new Employee("vivek","padh", 9.23, 13.45); System.out.println(); System.out.println(g1); System.out.println(g2); } } o/p is my Dear vivek your intime:9.23 outtime13.45 work hour:4.219999999999999 my Dear jayesh your intime:9.45 outtime13.45 work hour:4.0
|
 |
Henrik Engert
Ranch Hand
Joined: Apr 26, 2005
Posts: 68
|
|
You have Then use name.toString() instead of this.toString(); Maybe that will help.
|
SCJP 5.0<br />SCWCD
|
 |
Henrik Engert
Ranch Hand
Joined: Apr 26, 2005
Posts: 68
|
|
Instead of using toString() you can create Getters in your Nameof_emp class. Then in your Employee class you will access the first, middle and last name by using: name.getFirst_n() and so on. I hope that helps.
|
 |
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
|
|
Following on Henrik's idea, I'd also create some helper methods for the Nameof_emp class like getFullName() [all three concatenated together with spaces as necessary) and getFirstAndLastName() [should be obvious]. This will make it easier to enforce uniformity of how names are built. For example, if you code getFullName() like this:you'll get "null" showing up when a portion of the name is not specified. When fixing that, make sure you don't return two spaced between a first and last name when the middle name is null. Finally, it will make debugging and writing logging code easier if you provide a toString() method for the Nameof_emp class. Just call one of the other methods -- probably getFullName(). Normal code shouldn't rely on toString() since none of the various forms for a three-part name can really be viewed as "correct." In my code I tend to use toString() only when displaying an object for developers, thus my choice of the full name to ease debugging. Now for some stylistic advice. I recommend reading and following the Java naming conventions. Classes use InitialUpperCamelCase, methods and non-final variables use initialLowerCamelCase, and final variables (constants) use ALL_CAPS_WITH_UNDERSCORES. By following these conventions you make it easier for other people to quickly understand your code. How about FullName or PersonName instead of Nameof_emp? A three-part name isn't inherently limited to employees, and thus the class name shouldn't imply such a limitation. It's fields would be firstName, middleName, and lastName with methods like I've used above. Welcome to JavaRanch!
|
 |
 |
|
|
subject: toString() method overloading
|
|
|