无法正确调用Java中的void函数以将输出正确输出到控制台。

huangapple go评论93阅读模式
英文:

Unable to correcty call a void function in Java to output to the console

问题

以下是您提供的代码的翻译部分:

Employee.java

  1. public class Employee {
  2. private String _first_name;
  3. private String _last_name;
  4. private double _monthly_salary;
  5. public Employee(String first_name, String last_name, double monthlySalary) {
  6. setFirstName(first_name);
  7. setLastName(last_name);
  8. setMonthlySalary(monthlySalary);
  9. }
  10. public String getFirstName() {
  11. return _first_name;
  12. }
  13. public void setFirstName(String first_name) {
  14. _first_name = first_name;
  15. }
  16. public String getLastName() {
  17. return _last_name;
  18. }
  19. public void setLastName(String lastName) {
  20. _last_name = lastName;
  21. }
  22. public double getMonthlySalary() {
  23. return _monthly_salary;
  24. }
  25. public void setMonthlySalary(double monthlySalary) {
  26. if (monthlySalary < 0.0) {
  27. monthlySalary = 0.0;
  28. }
  29. _monthly_salary = monthlySalary;
  30. }
  31. public void developerInfo() {
  32. System.out.println("Name: Erin");
  33. System.out.println("Course: Object-Oriented Programming");
  34. System.out.println("Program: Three \n");
  35. }
  36. }

EmployeeTest.java

  1. public class EmployeeTest {
  2. public static void main(String[] args) {
  3. Employee employee1 = new Employee("John", "Doe", 2875.00);
  4. Employee employee2 = new Employee("Jane", "Doe", 3150.00);
  5. System.out.println("Before 20% raise:");
  6. System.out.printf("Employee 1: %s %s; Yearly Salary: $%.2f%n",
  7. employee1.getFirstName(), employee1.getLastName(),
  8. 12 * employee1.getMonthlySalary());
  9. System.out.printf("Employee 2: %s %s; Yearly Salary: $%.2f%n",
  10. employee2.getFirstName(), employee2.getLastName(),
  11. 12 * employee2.getMonthlySalary());
  12. employee1.setMonthlySalary(employee1.getMonthlySalary() * 1.2);
  13. employee2.setMonthlySalary(employee2.getMonthlySalary() * 1.2);
  14. System.out.println("After 20% raise:");
  15. System.out.printf("Employee 1: %s %s; Yearly Salary: $%.2f%n",
  16. employee1.getFirstName(), employee1.getLastName(),
  17. 12 * employee1.getMonthlySalary());
  18. System.out.printf("Employee 2: %s %s; Yearly Salary: $%.2f%n",
  19. employee2.getFirstName(), employee2.getLastName(),
  20. 12 * employee2.getMonthlySalary());
  21. }
  22. }

请注意,开头的 "I have the following class written in the Employee.java script:" 和 "With the corresponding test class written in a separate script called EmployeeTest.java:" 这两句话没有被翻译,因为您要求只返回翻译好的部分。

英文:

I have the following class written in the Employee.java script:

  1. public class Employee {
  2. //***************************************************************
  3. //
  4. // Developer: Erin
  5. //
  6. // Program #: 3
  7. //
  8. // File Name: Program3.java
  9. //
  10. // Course: Object-Oriented Programming - Java
  11. //
  12. // Due Date:
  13. //
  14. // Instructor: Prof. Fred
  15. //
  16. // Chapter: 3
  17. //
  18. // Description:
  19. // Program creates an Employee class that includes three (3) private instances:
  20. // 1) first name (String)
  21. // 2) last name (String)
  22. // 3) monthly salary (double)
  23. // Program provides:
  24. // a) a constructor that initializes instance variables
  25. // b) set and get methods for each instance variable
  26. // c) test app named EmployeeTest that demonstrates Employee class capabilities
  27. // d) two (2) Employee objs, from which obj&#39;s yearly salary is displayed
  28. // e) display of 20% raise for each Employee;s yearly salary
  29. //
  30. //***************************************************************
  31. private String _first_name;
  32. private String _last_name;
  33. private double _monthly_salary;
  34. public Employee(String first_name, String last_name, double monthlySalary)
  35. {
  36. setFirstName(first_name);
  37. setLastName(last_name);
  38. setMonthlySalary(monthlySalary);
  39. }
  40. public String getFirstName()
  41. {
  42. return _first_name;
  43. }
  44. public void setFirstName(String first_name)
  45. {
  46. _first_name = first_name;
  47. }
  48. public String getLastName()
  49. {
  50. return _last_name;
  51. }
  52. public void setLastName(String lastName)
  53. {
  54. _last_name =lastName;
  55. }
  56. public double getMonthlySalary()
  57. {
  58. return _monthly_salary;
  59. }
  60. public void setMonthlySalary(double monthlySalary)
  61. {
  62. if(monthlySalary &lt; 0.0)
  63. {
  64. monthlySalary = 0.0;
  65. }
  66. _monthly_salary = monthlySalary;
  67. }
  68. //***************************************************************
  69. //
  70. // Method: developerInfo
  71. //
  72. // Description: The developer information method of the program
  73. //
  74. // Parameters: None
  75. //
  76. // Returns: N/A
  77. //
  78. //**************************************************************
  79. public void developerInfo()
  80. {
  81. System.out.println(&quot;Name: Erin&quot;);
  82. System.out.println(&quot;Course: Object-Oriented Programming&quot;);
  83. System.out.println(&quot;Program: Three \n&quot;);
  84. }
  85. }

With the corresponding test class written in a separate script called EmployeeTest.java:

  1. public class EmployeeTest {
  2. public static void main(String[] args) {
  3. **developerInfo();**
  4. Employee employee1 = new Employee(&quot;John&quot;, &quot;Doe&quot;, 2875.00);
  5. Employee employee2 = new Employee(&quot;Jane&quot;, &quot;Doe&quot;, 3150.00);
  6. System.out.printf(&quot;%s %n&quot;, &quot;Before 20% raise:&quot;);
  7. System.out.printf( &quot;Employee 1: %s %s; Yearly Salary: $%.2f\n&quot;,
  8. employee1.getFirstName(), employee1.getLastName(),
  9. 12 * employee1.getMonthlySalary() );
  10. System.out.printf( &quot;Employee 2: %s %s; Yearly Salary: $%.2f\n&quot;,
  11. employee2.getFirstName(), employee2.getLastName(),
  12. 12 * employee2.getMonthlySalary() );
  13. employee1.setMonthlySalary(employee1.getMonthlySalary() * 1.2);
  14. employee2.setMonthlySalary(employee2.getMonthlySalary() * 1.2);
  15. System.out.printf(&quot;%s %n&quot;, &quot;After 20% raise:&quot;);
  16. System.out.printf( &quot;Employee 1: %s %s; Yearly Salary: $%.2f\n&quot;,
  17. employee1.getFirstName(), employee1.getLastName(),
  18. 12 * employee1.getMonthlySalary() );
  19. System.out.printf( &quot;Employee 2: %s %s; Yearly Salary: $%.2f\n&quot;,
  20. employee2.getFirstName(), employee2.getLastName(),
  21. 12 * employee2.getMonthlySalary() );
  22. }
  23. }

I cannot figure out how to call the developerInfo function in order get that to print to the console. Expected output is:

  1. Name: Erin
  2. Course: Object-Oriented Programming
  3. Program: Three
  4. Before 20% raise:
  5. Employee 1: John Doe; Yearly Salary: $34500.00
  6. Employee 2: Jane Doe; Yearly Salary: $37800.00
  7. After 20% raise:
  8. Employee 1: John Doe; Yearly Salary: $41400.00
  9. Employee 2: Jane Doe; Yearly Salary: $45360.00

Any insight would be so appreciated! Thank you!

答案1

得分: 2

存在两种可能性。首先,你可以将方法设为静态的:

  1. public static void developerInfo() {
  2. System.out.println("Name: Erin");
  3. System.out.println("Course: Object-Oriented Programming");
  4. System.out.println("Program: Three \n");
  5. }

然后你可以使用 Employee.developerInfo() 调用该方法。第二种方法是你可以使用 Employee 类的实例来调用该方法:

  1. employee1.developerInfo();

由于它与雇员本身不直接相关,我会选择第一种选项,或者将该方法外包出去。

英文:

There exists two possibilites. First you can make the method static:

  1. public static void developerInfo() {
  2. System.out.println(&quot;Name: Erin&quot;);
  3. System.out.println(&quot;Course: Object-Oriented Programming&quot;);
  4. System.out.println(&quot;Program: Three \n&quot;);
  5. }

Then you can call the method with Employee.developerInfo(). Second you can use an instance of the Employee class and you can call the method:

  1. employee1.developerInfo();

Since it is not directly related to an employee, I would take the first option or outsource the method.

huangapple
  • 本文由 发表于 2020年9月20日 04:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63973122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定