Answer:
Here is the modified DemoSalesperson2
public class DemoSalesperson2{ // class DemoSalesperson2
public static void main(String[] args) { //start of main() function body
SalesPerson[] sp = new SalesPerson[10]; // creates an array of SalesPerson object named sp
int start_id =111; // assign 111 to starting id number
double start_sal=25000; //assign 25000 to starting salary means salary will start from amount 25000
for (int i =0; i<sp.length; i++) { //loop iterates until the value of loop variable i remains less then length of the array object sp
sp[i] =new SalesPerson(start_id+i,start_sal+5000*(i)); /*at each iteration ID number from 111 through 120 and a sales value that ranges from $25,000 to $70,000, increasing by $5,000 for each successive is displayed using object array sp[] which calls the SalesPerson constructor to access id and sales */
System.out.println(sp[i].getId()+" "+sp[i].getAnnualSales() ); } } } /*uses object sp of class SalesPerson to access the methods getId which returns the id and getAnnualSales method which returns the annual sales, to display id and annual sales */
Step-by-step explanation:
The question first requires a SalesPerson class with these requisites:
Class name: Salesperson.
Data fields for Salesperson: int ID number , double annual sales amount.
Methods: constructor SalesPerson() that requires values for both data fields, as well as get and set methods for each of the data fields.
So according to the complete question's requirement, I have implemented SalesPerson class:
public class SalesPerson { // class name
//data members: integer type id variable and double type sales variable
private int id;
private double sales;
public SalesPerson(int id_no, double annual_sales) { //constructor that requires values for id and sales
id = id_no;
sales = annual_sales; }
public int getId() { //accessor getId() method to get or access the value of id data field
return id; }
public void setId(int id_no) { //mutator setId to set the value of id data field
id = id_no; }
public double getAnnualSales() { //accessor getAnnualSales() method to get or access the value of sales data field
return sales; }
public void setAnnualSales(double annual_sales) { //mutator setAnnualSales to set the value of sales data field
sales = annual_sales; } }
However you can use my DemoSalesperson2 application for your own SalesPerson class.
Now I will explain the working of for loop used in DemoSalesperson2
Iteration 1:
i=0
i<sp.length is true because length of sp[] is 10 so i<10
This means the body of for loop will execute.
The statement inside the body of for loop is :
sp[i] =new SalesPerson(start_id+i,start_sal+5000*(i));
class SalesPerson constructor is used to access the data members of SalesPerson class
start_id = 111
start_sal=25000
start_id+i = 111 + 0 = 111
start_sal+5000*(i) = 25000 + 5000 (0) = 25000
The values 111 and 25000 are stored to sp[0] means at the first index of the sp array.
System.out.println(sp[i].getId()+" "+sp[i].getAnnualSales() );
This statement uses sp object to get access to the getId and getAnnualSales methods to print the id number and sales amount.
So the output at 1st iteration is:
111 25000.0
Iteration 1:
The value of i is incremented to 1 so now i = 1
i<sp.length is true because length of sp[] is 10 so i<10
This means the body of for loop will execute.
The statement inside the body of for loop is :
sp[i] =new SalesPerson(start_id+i,start_sal+5000*(i));
class SalesPerson constructor is used to access the data members of SalesPerson class
start_id = 111
start_sal=25000
start_id+i = 111 + 1 = 112
start_sal+5000*(i) = 25000 + 5000 (1) = 25000 + 5000 = 30000
The values 111 and 25000 are stored to sp[1] means at the first index of the sp array.
System.out.println(sp[i].getId()+" "+sp[i].getAnnualSales() );
This statement uses sp object to get access to the getId and getAnnualSales methods to print the id number and sales amount.
So the output at 1st iteration is:
112 30000.0
The loop keeps iterating and the value of id is incremented by 1 and value of sales is increased by 5000 at every iteration.
This loop continues to execute until the value of i exceeds the length of sp array i.e. 10.
The values from iteration 1 to 8 following the above explanation are:
Iteration 1:
111 25000.0
Iteration 2:
112 30000.0
Iteration 3:
113 35000.0
Iteration 4:
114 40000.0
Iteration 5:
115 45000.0
Iteration 6:
116 50000.0
Iteration 7:
117 55000.0
Iteration 8:
118 60000.0
Iteration 9:
119 65000.0
Iteration 10:
120 70000.0
At iteration 11 the for loop body will not execute as value of i at 11th iteration is i = 10 and i<sp.length evaluates to false as i = sp.length because length of sp array is 10 and now the value of i is also 10. So the loop ends. Screenshot of the program and its output is attached.