5
\$\begingroup\$

Am I making good use of a 2-D array? Is this code inefficient? I am supposed to receive input from 6 departments for 4 quarterly periods in Java.

My assignment asks:

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.

public static void main(String[] args)
{
    double test1;
    double test2;
    double test3;
    double test4;

    /* These are just test variable that will show output,
    to assure that my methods are summing my 2-D array */

    Scanner  keyboard = new Scanner(System.in);

    test1 = Quarterly1();
    System.out.println(" Test 1 = " + test1);

    test2 = Quarterly2();
    System.out.println(" Test 2 = " + test2);

    test3 = Quarterly3();
    System.out.println(" Test 3 = " + test3);

    test4 = Quarterly4();
    System.out.println(" Test 4 = " + test4 " /n ");

}

This is a method that will receive input for my first quarterly report and sum it up.

public static double Quarterly1()
{
    Scanner  keyboard = new Scanner(System.in);
    double [][] sales = new double [6][4];
    int num = 1;
    double total = 0;

    // this for statement will assign a row to every department
    for (int row = 0; row < 6; row++)
    {
        // This for statement is taking one column from the array in order to fill with input
        for (int col = 0; col < 4 - 3; col++)
        {
            // This will receive sale numbers from the six departments
            System.out.print("Enter a The First Quarterly Sales For Department [ "+num+" ]. . . . . $ ");

            sales[row][col] = keyboard.nextDouble();
            num++;

            // This for statement will sum each department's input
            for (int i = 0; i < sales.length; i++);
            total += sales[row][col];
        }
    }

    return total;
}

This is a method that will receive input for my second quarterly report and sum it up.

public static double Quarterly2()
{
    Scanner  keyboard = new Scanner(System.in);
    double [][] sales = new double [6][4];
    int num = 1;
    double total = 0;
    for (int row = 0; row < 6; row++)
    {
        // This for statement is taking one column from the array in order to fill with input

        for (int col = 0; col < 4 - 3; col++)
        {
            // This will receive sale numbers from the six departments
            System.out.print("Enter a The Second Quarterly Sales For Department [ "+num+" ]. . . . . $ ");
            sales[row][col] = keyboard.nextDouble();
            num++;

            // This for statement will sum each department's input
            for (int i = 0; i < sales.length; i++);
            total += sales[row][col];
        }
    }

    return total;
}

This is a method that will receive input for my third quarterly report and sum it up.

public static double Quarterly3()
{
    Scanner  keyboard = new Scanner(System.in);
    double [][] sales = new double [6][4];
    int num = 1;
    double total = 0;
    for (int row = 0; row < 6; row++)
    {
        // This for statement is taking one column from the array in order to fill with input
        for (int col = 0; col < 4 - 3; col++)
        {
            // This will receive sale numbers from the six departments
            System.out.print("Enter a The Third Quarterly Sales For Department [ "+num+" ]. . . . . $ ");
            sales[row][col] = keyboard.nextDouble();
            num++;

            // This for statement will sum each department's input
            for (int i = 0; i < sales.length; i++);
            total += sales[row][col];
        }
    }

    return total;
}

This is a method that will receive input for my fourth quarterly report and sum it up.

public static double Quarterly4()
{
    Scanner  keyboard = new Scanner(System.in);
    double [][] sales = new double [6][4];
    int num = 1;
    double total = 0;

    // this for statement will assign a row to every department
    for (int row = 0; row < 6; row++)
    {
        // This for statement is taking one column from the array in order to fill with input
        for (int col = 0; col < 4 - 3; col++)
        {
            // This will receive sale numbers from the six departments
            System.out.print("Enter a The Forth Quarterly Sales For Department [ "+num+" ]. . . . . $ ");
            sales[row][col] = keyboard.nextDouble();
            num++;

            // This for statement will sum each department's input
            for (int i = 0; i < sales.length; i++);
            total = total + sales[row][col];
        }
    }

    return total;
}
\$\endgroup\$
3
\$\begingroup\$

I'm not really sure why you have 4 functions that are doing the exact same thing!?

The only difference seems to be the prompt text

I'm also not a big fan of the second for statement

// This for statement will sum each department's input

for (int i = 0; i < sales.length; i++);
total += sales[row][col];

what does that mean? My guess is:

// This for statement will sum each department's input
for (int i = 0; i < sales.length; i++){
total += sales[row][col];
}

but it still doesn't make sense here

I always suggest using {}'s it makes your code cleaner, easier to read and less error prone.

also why are you doing the 4-3? can't you just put 1?

Now my JAVA is a bit rusty but the point is still valid

public Enum Quarter{
   First,
   Second,
   Third,
   Forth
}

public class Quarterly{

     private final const ROW = 6;
     private final const COL = 4;
     private Scanner _scanner;

     public Quarterly(Scanner scanner){
          this._scanner = scanner;
     }
     private string GetMessage(Quarter quarter, int departmentNumber){
         switch(quarter){
             case First:
                return "Enter a The Third Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ "
             case Second:
                 ...

         }
         //May even want to use a template string
         //const string QuartlyTemplate = "Enter a The {0} Quarterly Sales for Department [{1}]. . . . . .$ "
         //then do a string format on the template to populate it
     }


     public double GetTotalForDepartments(Quarter quarter){
             double [][] sales = new double [ROW][COL];
             int num = 1;
             double total = 0;
            for (int row = 0; row < ROW ; row++)
            {
            // This for statement is taking one column from the array in order to fill with input
                 for (int col = 0; col < 4 - 3; col++)
                 {
                    // This will receive sale numbers from the six departments
                    System.out.print(message + "[" + num + " ]. . . . . $ ");
                    sales[row][col] = _scanner.nextDouble();
                    num++;
                    // This for statement will sum each department's input
                    for (int i = 0; i < sales.length; i++);
                    total += sales[row][col];
                }
        }

    return total;
}
 }

Now I know this may not compile right off the bat, because I don't have a java compiler handy, but it will be easier to read than having 4 functions doing the same exact work.

then your main will be

 public static void main(String[] args)
 {
     double test1;
     double test2;
     double test3;
     double test4;

     Scanner  keyboard = new Scanner(System.in);
     Quarterly helper = new Quarterly(keyboard);

     test1 = helper.GetTotalForDepartments(Quarter.First);
     System.out.println(" Test 1 = " + test1);

     test2 = helper.GetTotalForDepartments(Quarter.Second);
     System.out.println(" Test 2 = " + test2);

     test3 = helper.GetTotalForDepartments(Quarter.Third);
     System.out.println(" Test 3 = " + test3);

     test4 = helper.GetTotalForDepartments(Quarter.Fourth);
     System.out.println(" Test 4 = " + test4 " /n ");
 }
\$\endgroup\$
  • \$\begingroup\$ Awesome!!! I just have just reviewed you code and I will compile it to check for any syntax and logical errors. I will post the corrected version. \$\endgroup\$ – eruano57 Nov 7 '13 at 16:59
  • \$\begingroup\$ I can't seem to get this code complied!!! I am having trouble with the case statement communicating with the enum class \$\endgroup\$ – eruano57 Nov 8 '13 at 16:55
  • \$\begingroup\$ @eruano57 hmm guess enums are different in java you don't have to qualify it with the enum name(Just remove the Quarter prefix in the case statement) \$\endgroup\$ – dbarnes Nov 11 '13 at 3:23
  • 1
    \$\begingroup\$ public Enum Quarter should be public enum Quarter and a Java coding convention is to use start method names with a lowercase letter (not like C#) \$\endgroup\$ – Simon Forsberg Nov 19 '13 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.