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

Using Java write a code called Tracking Sales. In a store, there are 10 salesper

ID: 3605366 • Letter: U

Question

Using Java write a code called Tracking Sales.

In a store, there are 10 salesperson who sells different items throughout the day. We would like to know two things i) average sales for that day, and ii) who has the highest sales amount.

Inside the main method,

Ask the user to insert 10 sales amounts (that is, the total amount of sales made by each salesperson).

Store these 10 sales amounts in an array of 10 elements.

Example: If the first salesperson had a sale of $516.98, part of the array would look like this: myArray[0] = 516.98.

Choose the type of the array wisely and use a meaningful variable name!

Design a method called getAverage. The method takes an array of double variable, and returns a double value.

Find and return the average salesamount.

To compute the average, you should sum up all the sales amount of all the salesperson and divide the resultant sum by the number of salesperson in the store.

Design a method called getHighestSalesperson. The method takes an array of double variable, and returns an integer.

Find the salesperson who has the largest sales amount. Return the index of the array of that person.

Example: If the first salesperson made the highest sales, the method can return 0 (or, 1 if you keep the index consistent throughout your program).

Call getAverage from the main method. Display the average sales of the day.

Example: “Today the average sales was $2101.98”

Call getHighestSalesperson from the main method. Display the salesperson who made the highest sales and the corresponding sales amount.

Example: “Salesperson 3 made the highest sales of $771.21”

Explanation / Answer

import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
//declare sales array
double[] sales = new double[10];
//get user input of 10 sales amounts
System.out.print("Enter 10 sales amounts:");
for(int i=0;i<10;i++){
sales[i] = sc.nextDouble();
}
  
//call getAverage method
double avg = getAverage(sales);
System.out.printf("Today the average sales was $%.2f ",avg);
  
//call getHighestSalesPerson method
int ind = getHighestSalesPerson(sales);
System.out.printf("Salesperson %d made the highest sales of $%.2f ",ind,sales[ind-1]);
}
  
public static double getAverage(double sales[]){
double s = 0;
//loop through sales array and add each element to s
for(int i=0;i<10;i++){
s += sales[i];
}
//return average by dividing sum with 10
return s/10;
}
public static int getHighestSalesPerson(double sales[]){
//initial highest element and initial index
double high = sales[0];
int index = 0;
//loop through sales array
for(int i=1;i<10;i++){
//if ith sale is greater than high, update high and index with current element
if(sales[i]>high){
high = sales[i];
index = i;
}
}
//return index+1
return index+1;
}
}
/*
Enter 10 sales amounts: 2.5 1.2 3.4 5.6 7.5 8.9 9.8 3.7 4.1 7.4
Today the average sales was $5.41
Salesperson 7 made the highest sales of $9.80
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote