Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Exercise 9-4 | Instructions Salespersonava Salespersonsor...+ 1 publ

ID: 3755797 • Letter: P

Question

Programming Exercise 9-4 | Instructions Salespersonava Salespersonsor...+ 1 public class Salesperson In Chapter 8, you created a Salesperson class with fields for an ID number and sales values. Now, create an application that allows a user to enter values for an array of seven Salesperson objects. Offer the user the choice of displaying the objects in order by either (I)D number or (S)ales value. private int d private double sales 5 Salesperson (int idNum, double ant) Grading salesant; Write your Java code in the area on the right. Use the Run button to compile and run the code. Clicking the Run Checks button will run pre-configured tests against your code to calculate a grade. public int getId() return d 12 13 4public double getsales() 15 16 17 18 public void setId(int idNum) 19 20 21 22 public void setSales(double ant) 23 24 25 26 27 28 29 30 Once you are happy with your results, click the Submit button to record your score return sales sales ant;

Explanation / Answer

If you have any doubts, please give me comment...

import java.util.*;

public class SalespersonSort{

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

Salesperson sp[] = new Salesperson[7];

int id;

double sales;

String message, entry;

for(int i=0; i<7; i++){

System.out.println("Salesperson "+(i+1));

System.out.print("ID: ");

id = in.nextInt();

System.out.print("Sales: ");

sales = in.nextDouble();

sp[i] = new Salesperson(id, sales);

}

System.out.println("Sort by (I)D, or (S)ales");

entry = in.next();

if (entry.charAt(0) == 'N') {

sortByID(sp);

message = "Sorted by ID ";

} else {

sortBySales(sp);

message = "Sorted by Sales ";

}

display(sp, message);

}

public static void sortByID(Salesperson[] array) {

int a, b;

Salesperson temp;

int highSub = array.length - 1;

for (a = 0; a < highSub; ++a) {

for (b = 0; b < highSub-a; ++b)

if (array[b].getId() > array[b + 1].getId()) {

temp = array[b];

array[b] = array[b + 1];

array[b + 1] = temp;

}

}

}

public static void sortBySales(Salesperson[] array){

int a, b;

Salesperson temp;

int highSub = array.length - 1;

for (a = 0; a < highSub; ++a) {

for (b = 0; b < highSub-a; ++b)

if (array[b].getSales() > array[b + 1].getSales()) {

temp = array[b];

array[b] = array[b + 1];

array[b + 1] = temp;

}

}

}

public static void display(Salesperson[] s, String msg) {

int len = s.length;

for (int i = 0; i < len; i++)

msg = msg + s[i].getId() + ", " + s[i].getSales() + " ";

System.out.println(msg);

}

}