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评论79阅读模式
英文:

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:

public class Salesperson {
	int ID;
	double sales;

	public Salesperson(int iDnumber, double sales) {
		this.ID = iDnumber;
		this.sales = sales;
	}

	public void setID(int iDnumber) {
		this.ID = iDnumber;
	}
	
	public void setSales(double sales) {
		this.sales = sales;
	}
	
	public int getID() {
		return ID;
	}
	
	public double getSales() {
		return sales;
	}
}

DemoSalesPerson.java:

public class DemoSalesperson {
	public static void main(String[] args) {
		Salesperson[] salesperson = new Salesperson[10]; // Changed array size to 10
		
		for (int i = 0; i < 10; ++i) { // Changed loop limit to 10
			salesperson[i] = new Salesperson(9999, 0);
		}

		for (int i = 0; i < 10; ++i) { // Changed loop limit to 10
			System.out.println(salesperson[i].getID() + ", " + salesperson[i].getSales());
		}
	}
}

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:

@Test
public void unitTest() {
    Salesperson salesperson = new Salesperson(999, 0.0);
    salesperson.setID(101); // Fixed method name
    assertEquals(salesperson.getID(), 101); // Fixed method name
    salesperson.setID(1009); // Fixed method name
    assertEquals(salesperson.getID(), 1009); // Fixed method name
}

NtTest427994f5.java:

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

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:

public class Salesperson {
	int ID;
	double sales;
	
	
	public Salesperson(int iDnumber, double sales) {
		this.ID = iDnumber;
		this.sales = sales;
	}
	
	public void setID(int iDnumber) {
		this.ID = iDnumber;
	}
	public void setSales(double sales) {
		this.sales = sales;
	}	
	public int getID() {
		return ID;
	}
	public double getSales() {
		return sales;
	}

}

DemoSalesPerson.java:


public class DemoSalesperson {

	public static void main(String[] args) {

		Salesperson[] salesperson = new Salesperson[12];
		//creates 12 salespeople and sets their id to 9999 and sales to 0
		
		for (int i = 0; i &lt; 12; ++i){
			salesperson[i] = new Salesperson(9999, 0);
		}
		
		for (int i = 0; i &lt; 12; ++i){
		System.out.println(salesperson[i].getID() + &quot;, &quot; + salesperson[i].getSales());
		}
		
	}

}

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

NtTestcda27358.java:8: error: cannot find symbol
    salesperson.setId(101);
               ^
  symbol:   method setId(int)
  location: variable salesperson of type Salesperson
NtTestcda27358.java:9: error: cannot find symbol
    assertEquals(salesperson.getId(), 101);
                            ^
  symbol:   method getId()
  location: variable salesperson of type Salesperson
NtTestcda27358.java:10: error: cannot find symbol
    salesperson.setId(1009);
               ^
  symbol:   method setId(int)
  location: variable salesperson of type Salesperson
NtTestcda27358.java:11: error: cannot find symbol
    assertEquals(salesperson.getId(), 1009);
                            ^
  symbol:   method getId()
  location: variable salesperson of type Salesperson
4 errors

Test Contents
@Test
public void unitTest() {
    Salesperson salesperson = new Salesperson(999, 0.0);
    salesperson.setId(101);
    assertEquals(salesperson.getId(), 101);
    salesperson.setId(1009);
    assertEquals(salesperson.getId(), 1009);
}

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

NtTest427994f5.java:8: error: cannot find symbol
    assertEquals(salesperson.getId(), 997);
                            ^
  symbol:   method getId()
  location: variable salesperson of type Salesperson
NtTest427994f5.java:11: error: cannot find symbol
    assertEquals(salesperson2.getId(), 78);
                             ^
  symbol:   method getId()
  location: variable salesperson2 of type Salesperson
2 errors
Test Contents
@Test
public void unitTest() {
    Salesperson salesperson = new Salesperson(997, 56.87);
    assertEquals(salesperson.getId(), 997);
    assertEquals(salesperson.getSales(), 56.87, 1e-15);
    Salesperson salesperson2 = new Salesperson(78, 1001.34);
    assertEquals(salesperson2.getId(), 78);
    assertEquals(salesperson2.getSales(), 1001.34, 1e-15);
}

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:

确定