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

Hi! The following code works well, however, there are two things I am interested

ID: 3711449 • Letter: H

Question

Hi! The following code works well, however, there are two things I am interested in. I would like to use decimal format so the output has two decimal places after the decimal, now it only goes to one place if I enter values like 10, 20 etc. The second thing is, can the code be altered to use printf, or a loop to output the sales totals, instead of the multiple use of print statements at the end. I could use some help!

import java.text.DecimalFormat;

import java.util.Scanner;

public class TwoDimArrayAAA

{

public static void main( String [] args)

{

   Scanner scan = new Scanner( System.in );

   // 1) Declare and allocate Storage

   String [] workDays = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

   double [][] saleTotals = new double [3][5];

   // 2) Populate Array

   for (int i = 0; i < saleTotals.length; i++ )

   {

    for ( int j = 0; j < saleTotals[0].length; j++ )

    {

    

     System.out.print( "Enter " + workDays[j] + " Totals for Salesperson " +  (i+1) + ": " + " " );

     saleTotals[i][j] = scan.nextDouble( );

    }

   }

    // 3) Process Array - Accumulators

   

   double total = 0;

   

   for ( int i = 0; i < saleTotals.length; i++ )

    for ( int j = 0; j < saleTotals[0].length; j++ )

     total += saleTotals[i][j];

   

   System.out.println( );

   System.out.println(" Weekly Sales Report ");

   System.out.println( );

   

   

   // 4) Output Array

   

  double totalA;

   

   System.out.print(" ");

   System.out.print("Mon" + " ");

   System.out.print("Tue" + " ");

   System.out.print("Wed" + " ");

   System.out.print("Thurs" + " ");

   System.out.print("Fri"+ " ");

   System.out.println("Total");

   

  for ( int i = 0; i < saleTotals.length; i++ )

  {

   totalA = 0; // Set totalA to zero so we can start adding the new row

   System.out.print(i+1 + " ");

   for ( int j = 0; j < saleTotals[0].length; j++ )

   {

    System.out.print(saleTotals[i][j] + " ");

    totalA += saleTotals[i][j];

   }

   

   System.out.println(totalA); // After we print all the values we print the accumulated total of that row

   }

   

   System.out.println(" ------------------------------------");

   System.out.print(" ");

   System.out.print((saleTotals[0][0]+saleTotals[1][0]+saleTotals[2][0]) + " ");

   System.out.print((saleTotals[0][1]+saleTotals[1][1]+saleTotals[2][1]) + " ");

   System.out.print((saleTotals[0][2]+saleTotals[1][2]+saleTotals[2][2]) + " ");

   System.out.print((saleTotals[0][3]+saleTotals[1][3]+saleTotals[2][3]) + " ");

   System.out.print((saleTotals[0][4]+saleTotals[1][4]+saleTotals[2][4])+ " ");

   System.out.println(total);

}

}

Explanation / Answer

I wrote loop instead of printing multiple printf statements

import java.text.DecimalFormat;

import java.util.Scanner;

public class TwoDimArrayAAA

{

public static void main( String [] args)

{
Scanner scan = new Scanner( System.in );
String [] workDays = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
double [][] saleTotals = new double [3][5];
for (int i = 0; i < saleTotals.length; i++ )
{
for ( int j = 0; j < saleTotals[0].length; j++ )
{
System.out.print( "Enter " + workDays[j] + " Totals for Salesperson " + (i+1) + ": " + " " );
saleTotals[i][j] = scan.nextDouble( );
}
}
double total = 0;
for ( int i = 0; i < saleTotals.length; i++ )
for ( int j = 0; j < saleTotals[0].length; j++ )
total += saleTotals[i][j];
System.out.println( );
System.out.println(" Weekly Sales Report ");
System.out.println( );
double totalA;
System.out.print(" ");
System.out.print("Mon" + " ");
System.out.print("Tue" + " ");
System.out.print("Wed" + " ");
System.out.print("Thurs" + " ");
System.out.print("Fri"+ " ");
System.out.println("Total");

for ( int i = 0; i < saleTotals.length; i++ )
{
totalA = 0;
System.out.print(i+1 + " ");
for ( int j = 0; j < saleTotals[0].length; j++ )
{
System.out.print(saleTotals[i][j] + " ");
totalA += saleTotals[i][j];
}

System.out.println(totalA);

}

System.out.println(" ------------------------------------");

System.out.print(" ");
for ( int i = 0; i < 1; i++ )

for ( int j = 0; j < saleTotals.length; j++ )
{
System.out.print((saleTotals[j][i]+saleTotals[j+1][i]+saleTotals[j+2][i]) + " ");

}
}

System.out.println(total); }}

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