Write a program(In java) that lets the user enter four quarterly sales figures f
ID: 3622436 • Letter: W
Question
Write a program(In java) 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 division's increase or decease from the previous quarter (This will not be displayed for the first quarter)
- The total sales for the quarter
- The company's increase or decrease from the previous quarter (This will not be displayed for the first quarter)
- the average sales for all divisions that quarter
- the division with the highest sales for that quarter
Also we have to enter the missing code from the Divisions class and SaleStatics Class as below:
public class Divisions
{
private final int DIVISIONS = 6; // Number of divisions
private final int QUARTERS = 4; // Number of quarters
// Create an array to hold company-wide sales data.
private double[][] sales = new double[DIVISIONS][QUARTERS];
/**
* setSales method
* This method sets a division's amount of sales for a
* specified quarter.
*/
public void setSales(int div, int quarter, double amount)
{
/* Missing code starts here */
/* Missing code ends here */
}
/**
* getSales method
* This method returns a division's sales for a specified
* quarter.
*/
public double getSales(int div, int quarter)
{
// The array subscripts are zero-based, so subtract 1
// from div and quarter to get the correct subscript.
return sales[div - 1][quarter - 1];
}
/**
* totalQuarterSales method
* Returns the total sales for the specified quarter.
*/
public double totalQuarterSales(int quarter)
{
/* Missing code starts here */
/* Missing code ends here */
}
/**
* getQuarterlyIncrease method
* Returns the quarterly increase for a specified
* quarter, for a specified division. If the quarter
* is 1, or if an invalid value is passed for the
* quarter or the division, the method returns 0.0.
*/
public double getQuarterlyIncrease(int div, int qtr)
{
/* Missing code starts here */
/* Missing code ends here */
}
}
AND
import java.io.*;
import java.util.Scanner;
import java.text.DecimalFormat;
public class SalesStats
{
public static void main(String[] args) throws IOException
{
// Create a Divisions object.
Divisions d = new Divisions();
// Read the values from the file into d.
readValues(d);
// Display the values in d.
displaySales(d);
// Display the quarterly increase or decrease.
displayQtrDifference(d);
// Display the total sales by quarter.
displaySalesByQtr(d);
// Display the quarterly increase or decrease
// by quarter.
displayCompanyQtrDifference(d);
// Display the average sales per quarter.
displayAverageSalesPerQtr(d);
// Display the division with the highest sales
// by quarter.
displayHighestDivisionPerQtr(d);
}
/**
* readValues method.
* Reads the values from Sales.txt into the object.
*/
public static void readValues(Divisions d) throws IOException
{
/* Missing code starts here */
/* Missing code ends here */
}
/**
* displayValues method.
* Displays a list of sales figures by division.
*/
public static void displaySales(Divisions d)
{
System.out.println("SALES AMOUNTS BY DIVISION");
System.out.println("=========================");
/* Missing code starts here */
/* Missing code ends here */
}
/**
* displayQtrDifference method.
* Displays the quareterly increase or decrease for each
* division, starting at quarter 2.
*/
public static void displayQtrDifference(Divisions d)
{
System.out.println("QUARTERLY INCREASE/DECREASE BY DIVISION");
System.out.println("=======================================");
/* Missing code starts here */
/* Missing code ends here */
}
/**
* displaySalesByQtr method.
* Displays total sales by quarter.
*/
public static void displaySalesByQtr(Divisions d)
{
DecimalFormat dollar = new DecimalFormat("#,##0.00");
System.out.println("SALES AMOUNTS BY QUARTER");
System.out.println("========================");
for (int qtr = 1; qtr <= 4; qtr++)
{
System.out.println("Quarter " + qtr + ": $" +
dollar.format(d.totalQuarterSales(qtr)));
}
System.out.println();
}
/**
* displayCompanyQtrDifference method.
* Displays the quareterly increase or decrease for the
* company, starting at quarter 2.
*/
public static void displayCompanyQtrDifference(Divisions d)
{
double increase;
DecimalFormat dollar = new DecimalFormat("#,##0.00");
System.out.println("QUARTERLY INCREASE/DECREASE FOR THE COMPANY");
System.out.println("===========================================");
/* Missing code starts here */
/* Missing code ends here */
}
/**
* displayAverageSalesPerQuarter method.
* Displays the average sales for all divisions per quarter.
*/
public static void displayAverageSalesPerQtr(Divisions d)
{
/* Missing code starts here */
/* Missing code ends here */
}
/**
* displayHighestDivisionPerQtr method.
* Displays the division with the highest sales per quarter.
*/
public static void displayHighestDivisionPerQtr(Divisions d)
{
System.out.println("DIVISION WITH THE HIGHEST SALES PER QUARTER");
System.out.println("===========================================");
/* Missing code starts here */
/* Missing code ends here */
}
}
Explanation / Answer
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{ Scanner in = new Scanner(System.in);
double[][]sales=new double[6][4];
int i,j;
getsales(sales);
printsales(sales);
quarterchange(sales);
totalaveragesales(sales);
highest(sales);
}
public static void getsales(double sales[][])
{int i,j;
Scanner in=new Scanner(System.in);
System.out.println("Enter Sales");
for(i=0;i<6;i++)
{System.out.println("For Division "+(i+1)+": ");
for(j=0;j<4;j++)
{System.out.print("For quarter "+(j+1)+": ");
sales[i][j]=in.nextDouble();
while(sales[i][j]<0)
{System.out.println("must be>=0");
System.out.print("For quarter "+(j+1)+": ");
sales[i][j]=in.nextDouble();
}
}
}
}
public static void quarterchange(double sales[][])
{int i,j;
double quarter[ ]={0,0,0};
System.out.print("Increase from last quarter quarter div ");
for(i=1;i<4;i++)
System.out.print((i+1)+" ");
System.out.println();
for(i=0;i<6;i++)
{System.out.print(i+1);
for(j=1;j<4;j++)
{System.out.print(" "+(sales[i][j]-sales[i][j-1]));
quarter[j-1]+=(sales[i][j]-sales[i][j-1]);
}
System.out.println();
}
System.out.println("Company Increase from last quarter");
for(i=0;i<3;i++)
System.out.println("quarter "+(i)+" to "+(i+1)+": "+quarter[i]);
System.out.println(" ");
System.out.println();
}
public static void totalaveragesales(double sales[][])
{int i,j;
double sum;
System.out.println("Total Sales Average Sales");
for(i=0;i<4;i++)
{sum=0;
for(j=0;j<6;j++)
sum+=sales[j][i];
System.out.println("For Quarter "+(i+1)+": "+sum+" "+sum/6.);
}
System.out.println(" ");
}
public static void printsales(double sales[][])
{int i,j;
System.out.print("SALES BY DIVISION quarter div ");
for(i=0;i<4;i++)
System.out.print((i+1)+" ");
System.out.println();
for(i=0;i<6;i++)
{System.out.print(i+1);
for(j=0;j<4;j++)
System.out.print(" "+sales[i][j]);
System.out.println();
}
System.out.println();
}
public static void highest(double sales[][])
{int i,j,div=0;
double max=0;
System.out.println("Division with highest sales");
for(i=0;i<4;i++)
{max=0;
div=0;
for(j=0;j<6;j++)
if(sales[j][i]>max)
{max=sales[j][i];
div=j+1;
}
System.out.println("For Quarter "+(i+1)+" division with highest sales is: "+div);
}
System.out.println(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.