a. Create a class named Salesperson. Data fields for Salesperson include an inte
ID: 3740822 • Letter: A
Question
a. 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. Save the files as salesperson.java andDemoSalesperson.java.
b. Modify the DemoSalesperson application so each Salesperson has a successive 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 Salesperson. Save the file as DemoSalesperson2.java.
Explanation / Answer
a)
SalesPerson.java
public class SalesPerson {
//Declaring instance variables
private int ID;
private double annualSales;
//Parameterized constructor
public SalesPerson(int iD, double annualSales) {
ID = iD;
this.annualSales = annualSales;
}
// getters and setters
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public double getAnnualSales() {
return annualSales;
}
public void setAnnualSales(double annualSales) {
this.annualSales = annualSales;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "[ID=" + ID + ", annualSales=" + annualSales + "]";
}
}
_________________
DemoSalesperson.java
public class DemoSalesperson {
public static void main(String args[]) {
//Creating an SalesPerson class array
SalesPerson persons[] = new SalesPerson[10];
//CReating SalesPerson class Object and populate into SalesPerson array
for (int i = 0; i < persons.length; i++) {
SalesPerson s = new SalesPerson(9999, 0);
persons[i] = s;
}
//Displaying each SalesPerson Info
for (int i = 0; i < persons.length; i++) {
System.out.println("SalesPerson#"+(i+1)+":"+persons[i]);
}
}
}
________________
Output:
SalesPerson#1:[ID=9999, annualSales=0.0]
SalesPerson#2:[ID=9999, annualSales=0.0]
SalesPerson#3:[ID=9999, annualSales=0.0]
SalesPerson#4:[ID=9999, annualSales=0.0]
SalesPerson#5:[ID=9999, annualSales=0.0]
SalesPerson#6:[ID=9999, annualSales=0.0]
SalesPerson#7:[ID=9999, annualSales=0.0]
SalesPerson#8:[ID=9999, annualSales=0.0]
SalesPerson#9:[ID=9999, annualSales=0.0]
SalesPerson#10:[ID=9999, annualSales=0.0]
______________
2)
DemoSalesperson2.java
public class DemoSalesperson2 {
public static void main(String args[]) {
int id=111,salesAmt=25000;
//Creating an SalesPerson class array
SalesPerson persons[] = new SalesPerson[10];
//CReating SalesPerson class Object and populate into SalesPerson array
for (int i = 0; i < persons.length; i++) {
SalesPerson s = new SalesPerson(id,salesAmt);
persons[i] = s;
id++;
salesAmt+=5000;
}
//Displaying each SalesPerson Info
for (int i = 0; i < persons.length; i++) {
System.out.println("SalesPerson#"+(i+1)+":"+persons[i]);
}
}
}
________________
Output:
SalesPerson#1:[ID=111, annualSales=$25000.0]
SalesPerson#2:[ID=112, annualSales=$30000.0]
SalesPerson#3:[ID=113, annualSales=$35000.0]
SalesPerson#4:[ID=114, annualSales=$40000.0]
SalesPerson#5:[ID=115, annualSales=$45000.0]
SalesPerson#6:[ID=116, annualSales=$50000.0]
SalesPerson#7:[ID=117, annualSales=$55000.0]
SalesPerson#8:[ID=118, annualSales=$60000.0]
SalesPerson#9:[ID=119, annualSales=$65000.0]
SalesPerson#10:[ID=120, annualSales=$70000.0]
__________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.