Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey I am getting some wonky figures for this. Any idea on what I am doing wrong? Thanks! Quarterly Sales Statistics Write a program that

Hey I am getting some wonky figures for this. Any idea on what I am doing wrong? Thanks!

Quarterly Sales Statistics Write a program that lets the user enter four quarterly sales figures for six divisions of a company. The figures should be stored in a two- dimensional array. Once the figures are entered, the program should display the following data for each quarter:

A list of the sales figures by division

Each divisions increase or decrease from the previous quarter ( this will not be dis-played for the first quarter)

The total sales for the quarter

The companys increase or decrease from the previous quarter ( this will not be dis-played for the first quarter)

The average sales for all divisions that quarter

The division with the highest sales for that quarter

Input Validation: Do not accept negative numbers for sales figures.

import java.util.Scanner;

public class CompSales

{

public static void main(String[] args)

{

final int DIV = 2;

final int QTR = 2;

double[][] SF = new double[DIV][QTR]; // array for Sales figures (SF) with two dimensional array, divisions and quarters

Scanner kb = new Scanner(System.in);

for (int div = 0; div < DIV; div++)

{

for (int qtr = 0; qtr < QTR; qtr++)

{

System.out.printf("Division %d, Quarter %d: $", (div + 1), (qtr + 1));

SF[div][qtr] = kb.nextDouble();

while(SF[div][qtr] < 0)

{

System.out.println("You dirty commie. You can't enter NEGATIVE money! ");

System.out.print("Enter sales of division " + (div + 1) + " for quarter " + (qtr + 1) + ": ");

SF[div][qtr] = kb.nextDouble();

}

}

System.out.println();

}

showSF(SF);

DivDiff(SF);

QtrTot(SF);

CompDiff(SF);

Average(SF);

Winner(SF);

}

private static void showSF(double[][]array)

{

// A list of the sales figures by division

System.out.println("SALES FIGURES BY DIVISION");

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

for (int row = 0; row < array.length; row++)

{

for (int col = 0; col < array[0].length; col++)

{

System.out.println(array[row][col]);

}

}

}

private static void DivDiff(double[][] array)

{

//Each divisions increase or decrease from the previous quarter ( this will not be dis-played for the first quarter)

System.out.println("DIFFERENCE BETWEEN QUARTERS BY DIVISION");

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

for(int row = 0; row < array.length; row++)

{

System.out.println("Division " + (row) + ":-");

for(int col = 1; col < array[0].length; col++)

{

double nowSale = array[row][col];

double thenSale = array[row][col-1];

if ( thenSale < nowSale)

System.out.println("Division " + (row+1) + " earned " + Math.abs(nowSale - thenSale));

else

System.out.println("Division " + (row+1) + " lost " + (nowSale - thenSale));

}

}

}

private static void QtrTot(double[][] array)

{

//The total sales for the quarter

System.out.println("TOTAL SALES FOR THE QUATER");

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

for (int col = 0; col < array[0].length; col++)

{

double total = 0.0;

for (int row = 0; row < array.length; row++)

total += array[row][col];

System.out.println("Total of Quarter " + (col + 1) + " is " + total);

}

}

private static void CompDiff(double[][] array)

{

//The companys increase or decrease from the previous quarter ( this will not be dis-played for the first quarter)

System.out.println("COMPANY'S INCREASE OR DECREASE IN REVENUE FROM PREVIOUS QUARTER");

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

for(int row = 0; row < array[0].length; row++)

{

for(int col = 1; col < array.length; col++)

{

double nowSale = array[row][col];

double thenSale = array[row][col-1];

if ( thenSale < nowSale)

System.out.println("In Quarter " + (col + 1) + " Company earned " + Math.abs(nowSale - thenSale));

else

System.out.println("In Quarter " + (col + 1) + " Company lost " + (nowSale - thenSale));

}

}

}

private static void Average(double[][] array)

{

//The average sales for all divisions that quarter

System.out.println("AVERAGE SALES FOR ALL DIVISIONS PER QUARTER");

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

for (int col = 0; col < array[0].length; col++)

{

double average = 0.00;

for (int row = 0; row < array.length; row++)

average += array[row][col]/array.length;

System.out.println("Average of quarter " + col + " is " + average);

}

}

private static void Winner(double[][] array)

{

//The division with the highest sales for that quarter

System.out.println("DIVISION WITH HIGHEST SALES FIGURES FOR THE QUARTER");

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

for(int row = 0; row < array[0].length; row++)

{

double hisale = 0;

int windiv = 0;

for (int row = 0; row < array.length; row++)

{

if(array[row][col] > hisale)

{

hisale = array[row][col];

windiv = row;

}

System.out.println(" For Quarter " + array[col] + " division " +(windiv + 1)+ "made the highest sale:" + hisale);

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions