Setters and getters prompts me a "Java cannot find symbol error" which makes the code to fail the test of my learning platform

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

Setters and getters prompts me a "Java cannot find symbol error" which makes the code to fail the test of my learning platform

问题

salesperson.java:

  1. public class Salesperson {
  2. int ID;
  3. double sales;
  4. public Salesperson(int iDnumber, double sales) {
  5. this.ID = iDnumber;
  6. this.sales = sales;
  7. }
  8. public void setID(int iDnumber) {
  9. this.ID = iDnumber;
  10. }
  11. public void setSales(double sales) {
  12. this.sales = sales;
  13. }
  14. public int getID() {
  15. return ID;
  16. }
  17. public double getSales() {
  18. return sales;
  19. }
  20. }

DemoSalesPerson.java:

  1. public class DemoSalesperson {
  2. public static void main(String[] args) {
  3. Salesperson[] salesperson = new Salesperson[10]; // Changed array size to 10
  4. for (int i = 0; i < 10; ++i) { // Changed loop limit to 10
  5. salesperson[i] = new Salesperson(9999, 0);
  6. }
  7. for (int i = 0; i < 10; ++i) { // Changed loop limit to 10
  8. System.out.println(salesperson[i].getID() + ", " + salesperson[i].getSales());
  9. }
  10. }
  11. }

To fix the errors in the test code, make sure you're using the correct method names. Use setID() instead of setId(), and getID() instead of getId():

NtTestcda27358.java:

  1. @Test
  2. public void unitTest() {
  3. Salesperson salesperson = new Salesperson(999, 0.0);
  4. salesperson.setID(101); // Fixed method name
  5. assertEquals(salesperson.getID(), 101); // Fixed method name
  6. salesperson.setID(1009); // Fixed method name
  7. assertEquals(salesperson.getID(), 1009); // Fixed method name
  8. }

NtTest427994f5.java:

  1. @Test
  2. public void unitTest() {
  3. Salesperson salesperson = new Salesperson(997, 56.87);
  4. assertEquals(salesperson.getID(), 997); // Fixed method name
  5. assertEquals(salesperson.getSales(), 56.87, 1e-15);
  6. Salesperson salesperson2 = new Salesperson(78, 1001.34);
  7. assertEquals(salesperson2.getID(), 78); // Fixed method name
  8. assertEquals(salesperson2.getSales(), 1001.34, 1e-15);
  9. }

Additionally, ensure that the class names in your test code match the ones in your main code, i.e., Salesperson and DemoSalesperson.

英文:

The assignment goes as follows:
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a double annual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects.

salesperson.java:

  1. public class Salesperson {
  2. int ID;
  3. double sales;
  4. public Salesperson(int iDnumber, double sales) {
  5. this.ID = iDnumber;
  6. this.sales = sales;
  7. }
  8. public void setID(int iDnumber) {
  9. this.ID = iDnumber;
  10. }
  11. public void setSales(double sales) {
  12. this.sales = sales;
  13. }
  14. public int getID() {
  15. return ID;
  16. }
  17. public double getSales() {
  18. return sales;
  19. }
  20. }

DemoSalesPerson.java:

  1. public class DemoSalesperson {
  2. public static void main(String[] args) {
  3. Salesperson[] salesperson = new Salesperson[12];
  4. //creates 12 salespeople and sets their id to 9999 and sales to 0
  5. for (int i = 0; i &lt; 12; ++i){
  6. salesperson[i] = new Salesperson(9999, 0);
  7. }
  8. for (int i = 0; i &lt; 12; ++i){
  9. System.out.println(salesperson[i].getID() + &quot;, &quot; + salesperson[i].getSales());
  10. }
  11. }
  12. }

The code works properly as it should, however, the platform throws me these errors when defining setId and getId in the Salesperson class:

  1. NtTestcda27358.java:8: error: cannot find symbol
  2. salesperson.setId(101);
  3. ^
  4. symbol: method setId(int)
  5. location: variable salesperson of type Salesperson
  6. NtTestcda27358.java:9: error: cannot find symbol
  7. assertEquals(salesperson.getId(), 101);
  8. ^
  9. symbol: method getId()
  10. location: variable salesperson of type Salesperson
  11. NtTestcda27358.java:10: error: cannot find symbol
  12. salesperson.setId(1009);
  13. ^
  14. symbol: method setId(int)
  15. location: variable salesperson of type Salesperson
  16. NtTestcda27358.java:11: error: cannot find symbol
  17. assertEquals(salesperson.getId(), 1009);
  18. ^
  19. symbol: method getId()
  20. location: variable salesperson of type Salesperson
  21. 4 errors
  22. Test Contents
  23. @Test
  24. public void unitTest() {
  25. Salesperson salesperson = new Salesperson(999, 0.0);
  26. salesperson.setId(101);
  27. assertEquals(salesperson.getId(), 101);
  28. salesperson.setId(1009);
  29. assertEquals(salesperson.getId(), 1009);
  30. }

also when defining the constructor and get methods for the Salesperson class:

  1. NtTest427994f5.java:8: error: cannot find symbol
  2. assertEquals(salesperson.getId(), 997);
  3. ^
  4. symbol: method getId()
  5. location: variable salesperson of type Salesperson
  6. NtTest427994f5.java:11: error: cannot find symbol
  7. assertEquals(salesperson2.getId(), 78);
  8. ^
  9. symbol: method getId()
  10. location: variable salesperson2 of type Salesperson
  11. 2 errors
  12. Test Contents
  13. @Test
  14. public void unitTest() {
  15. Salesperson salesperson = new Salesperson(997, 56.87);
  16. assertEquals(salesperson.getId(), 997);
  17. assertEquals(salesperson.getSales(), 56.87, 1e-15);
  18. Salesperson salesperson2 = new Salesperson(78, 1001.34);
  19. assertEquals(salesperson2.getId(), 78);
  20. assertEquals(salesperson2.getSales(), 1001.34, 1e-15);
  21. }

How do I fix those errors and how do I avoid them?

答案1

得分: 1

你已经提供了 getID()setID() 方法,但测试需要方法名为 getId()setId()。这与预期有关,销售人员 ID 字段的名称将为 id,而不是 ID,这与广泛使用的 Java 约定一致,即实例变量的名称将以小写字母开头。

英文:

You have provided methods getID() and setID(), but the test expects methods named getId() and setId(). This is related to an expectation that the field for the salesperson ID will be named id, not ID, consistent with strong, widespread Java convention that instance variables will be named with an initial lowercase letter.

huangapple
  • 本文由 发表于 2020年10月16日 23:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392244.html
匿名

发表评论

匿名网友

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

确定