0

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 < 12; ++i){
            salesperson[i] = new Salesperson(9999, 0);
        }
        
        for (int i = 0; i < 12; ++i){
        System.out.println(salesperson[i].getID() + ", " + 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?

  • Since you say `The code works properly as it should` then the code works. We'll need to understand what this "platform" is in detail and how it works, otherwise there's no hope of guessing why it is throwing error. I think your instructor should be the one to help you. – markspace Oct 16 '20 at 15:41
  • Your method is called `getID()`. The unit test you showed calls `getId()`. That's why it doesn't find your method. Rename it to the name expected by the test and it should work. Same for `setId()`. – JustAnotherDeveloper Oct 16 '20 at 15:42
  • The platform is MindTap. I' – Jonathan Mejia Oct 16 '20 at 15:42
  • @JustAnotherDeveloper that fixed it. Thank you. I'll pay close attention to the details from now on. – Jonathan Mejia Oct 16 '20 at 15:44

1 Answers1

1

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157