Question: import java.util.Scanner; public class Lab_Q2 { public static void mai
ID: 3547640 • Letter: Q
Question
Question:
import java.util.Scanner;
public class Lab_Q2 {
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
double [][] sales = new double [3][5];
double [] totalsales = new double [3];
int i=0;
int j=0;
double average=0;
for(i=0; i<3; i++)
{
System.out.println("Employee "+(i+1));
for(j=0; j<5; j++)
{
System.out.print("Day "+(j+1)+": $");
sales[i][j] = input.nextDouble();
}
System.out.print(" ");
}
getTotalSales(sales,totalsales);
for(i=0 ;i<3; i++)
{
System.out.println("Employee "+(i+1)+" total sales :"+totalsales[i]);
}
average=getAverage(sales);
System.out.println("Average sales for this week: $ "+average);
}
public static void getTotalSales(double [][] sales, double [] totalsales)
{
int i=0;
int j=0;
for(i=0; i<3; i++)
{
for(j=0; j<5; j++)
{
totalsales[i]+=sales[i][j];
}
}
}
public static int findHighest(double [] totalsales)
{
int highest=0;
if(totalsales[0]>totalsales[1])
System.out.println("Highest sales Employee 1");
else if(totalsales[1]>totalsales[0])
System.out.println("Highest sales Employee 2");
else if(totalsales[2]>totalsales[1])
System.out.println("Highest sales Employee 3");
else if(totalsales[1]>totalsales[2])
System.out.println("Highest sales Employee 2");
else if(totalsales[2]>totalsales[0])
System.out.println("Highest sales Employee 3");
else if(totalsales[0]>totalsales[2])
System.out.println("Highest sales Employee 1");
else
{
}
return highest;
}
public static double getAverage(double[][] sales)
{
double average=0;
int i=0;
int j=0;
for(i=0; i<3; i++)
{
for(i=1; i<5; i++)
{
average+=sales[i][j];
}
}
average=average/3;
return average;
}
}
Please correct my error, explain using comment.
following by this output:
Explanation / Answer
/*i have corrected all the error's and shown in comments what was wrong
if any problem,understanding the code,please let me know by commenting on the solution
*/
Solution:
import java.util.Scanner;
public class Lab_Q2 {
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
double [][] sales = new double [3][5];
double [] totalsales = new double [3];
int i=0;
int j=0;
double average=0;
for(i=0; i<3; i++)
{
System.out.println("Employee "+(i+1));
for(j=0; j<5; j++)
{
System.out.print("Day "+(j+1)+": $");
sales[i][j] = input.nextDouble();
}
System.out.print(" ");
}
getTotalSales(sales,totalsales);
for(i=0 ;i<3; i++)
{
System.out.println("Employee "+(i+1)+" total sales :"+totalsales[i]);
}
findHighest(totalsales); //you misses this statement to print highest sale
System.out.println("Total collection of sales for this week:$"+(totalsales[0]+totalsales[1]+totalsales[2]));//you also //missed this to //print total collection
average=getAverage(sales);
System.out.println("Average sales for this week: $ "+average);
}
public static void getTotalSales(double [][] sales, double [] totalsales)
{
int i=0;
int j=0;
for(i=0; i<3; i++)
{
for(j=0; j<5; j++) //your were using 'i' in this loop as well
{
totalsales[i]+=sales[i][j];
}
}
}
public static int findHighest(double [] totalsales)
{
int highest=0;
if(totalsales[0]>totalsales[1])
System.out.println("Highest sales Employee 1");
else if(totalsales[1]>totalsales[0])
System.out.println("Highest sales Employee 2");
else if(totalsales[2]>totalsales[1])
System.out.println("Highest sales Employee 3");
else if(totalsales[1]>totalsales[2])
System.out.println("Highest sales Employee 2");
else if(totalsales[2]>totalsales[0])
System.out.println("Highest sales Employee 3");
else if(totalsales[0]>totalsales[2])
System.out.println("Highest sales Employee 1");
else
{
}
return highest;
}
public static double getAverage(double[][] sales)
{
double average=0;
int i=0;
int j=0;
for(i=0; i<3; i++)
{
for(j=0; j<5; j++) //you assigned 'j' to intially 1 instead of 0
{
average+=sales[i][j];
}
}
average=average/3;
return average;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.