You are viewing our Forum Archives. To view or take place in current topics click here.
Java Assignment - Need Help
Posted:

Java Assignment - Need HelpPosted:

MongoBreath
  • Rising Star
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
I've received a Java assignment for my programming module.

I'm halfway through the program but currently running into problems. The program is running fine and doing it's job but I need some advice for displaying the actual name of the month eg. January 2014 instead of 01/2014 just because it looks far better once printed out.

Click the below spoiler to view my code so far, you can copy it and run it to see what I'm talking about.



import java.util.Scanner;
class Assignment2
{
    public static void main(String [] args)
    {
       Scanner keyboard = new Scanner(System.in);
       int [] year2014 = new int [6];
       int [] year2015 = new int [6];

       //This will input and store the sales figures for 2014
       for (int count = 0; count<6; count++)
       {
          System.out.println("Enter the Monthly Sales Figures for 0"+(count+1)+"/2014");
          year2014[count] = keyboard.nextInt();
       }



       //This will input and store the sales figures for 2015
       for (int count = 0; count<6; count++)
       {
          System.out.println("Enter the MonthlySales Figures for 0"+(count+1)+"/2015");
          year2015[count] = keyboard.nextInt();
       }

       //Below the results of the methods will be output
       System.out.println("2014 Average Monthly Sales January-June");
       calcYearAverage(year2014);

       System.out.println("2015 Average Monthly Sales January-June");
       calcYearAverage(year2015);

       System.out.println("Highest selling month for 2014");
       calcHighest(year2014);

       System.out.println("Highest selling month for 2015");
       calcHighest(year2015);

       System.out.println("Lowest selling month for 2014");
       calcLowest(year2014);

       System.out.println("Lowest selling month for 2015");
       calcLowest(year2015);

       System.out.println("Average Sales for Each Month");
       eachMonthAverage(year2014,year2015);

   }



   //Method for calculating average for each 6 month period
   static void calcYearAverage(int [] array)
    {
       int total = 0;
       double average;

       for (int z = 0; z<6; z++)
       {
          total = total + array[z];
       }

       average = total/array.length;

       System.out.println("£"+average);
    }



    //Method for calculating highest selling month in each 6 month period
    static void calcHighest(int [] highest)
   {
      int highestFigure = 0;
      int highestMonth = 0;

      for (int x = 0; x<6; x++)
      {
         if(highest[x]>highestFigure)
            highestFigure = highest[x];
            highestMonth = x;
      }

      System.out.println("Month 0"+(highestMonth+1)+" with £"+highestFigure);
   }



   //Method for calculating lowest selling month in each 6 month period
    static void calcLowest(int [] lowest)
   {
      int lowestFigure = 999999;
      int lowestMonth = 0;

      for (int y = 0; y<6; y++)
      {
         if(lowest[y]<lowestFigure)
            lowestFigure = lowest[y];
            lowestMonth = y;
      }

      System.out.println("Month 0"+(lowestMonth+1)+" with £"+lowestFigure);
   }



   //Method for calculating the average for each month across the 2 years
   static void eachMonthAverage(int [] eachAv1, int [] eachAv2)
   {
      int total;
      double average;

      for (int i = 0; i<6; i++)
      {
         total = eachAv1[i] + eachAv2[i];
         average = total/2.0;
         System.out.println("The average mark for month 0"+(i+1)+" is "+average);
      }
   }
}



As you can see, I've just used the count in each for loop to print the month because I don't know how to do otherwise.

If you can help me I would very grateful for your input and will rep you as much as possible!

Thanks
#2. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
What you could do is do a switch statement on the integer.


String month = "";
switch (myInt) {
    case 1:
        month = "January";
        break;
    case 2:
        month = "February";
        break;
}
#3. Posted:
Cyimking
  • E3 2016
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Simple:

1. Import the date format package

import java.text.DateFormatSymbols;



2. Create a new method that will return the month's name based on the month's index. For simplicity, I made this method static to match your methods.

public static String getMonth(int monthIndex) {
    return new DateFormatSymbols().getMonths()[monthIndex];
}


3. Use it in your code...

 //Method for calculating highest selling month in each 6 month period
    static void calcHighest(int [] highest)
   {
      int highestFigure = 0;
      int highestMonth = 0;

      for (int x = 0; x<6; x++)
      {
         if(highest[x]>highestFigure)
            highestFigure = highest[x];
            highestMonth = x;
      }

//      System.out.println("Month 0"+(highestMonth+1)+" with £"+highestFigure);
        System.out.println(getMonth(highestMonth) +" with £" + highestFigure);
   }
#4. Posted:
MongoBreath
  • Rising Star
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
Thanks both of you for your input, it was really helpful and I've repped you both as much as possible.

I used the the date format package and it is working fine now - Thanks again.

Although, I'm having problems elsewhere.

In the method for calculating the highest and lowest selling months, I keep getting "June" no matter what I input.

Also, my percentage change calculation in the last method is not working.


Please see below my updated code


import java.text.DateFormatSymbols;
import java.util.Scanner;
class Assignment2
{
    public static void main(String [] args)
    {
       Scanner keyboard = new Scanner(System.in);
       int [] year2014 = new int [6];
       int [] year2015 = new int [6];


       //This will input and store the sales figures for 2014
       for (int count = 0; count<6; count++)
       {
          System.out.println("Enter the Monthly Sales Figures for "+(getMonth(count))+" 2014");
          year2014[count] = keyboard.nextInt();
       }



       //This will input and store the sales figures for 2015
       for (int count = 0; count<6; count++)
       {
          System.out.println("Enter the Monthly Sales Figures for "+(getMonth(count))+" 2015");
          year2015[count] = keyboard.nextInt();
       }

       //Below the results of the methods will be output

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

       System.out.print("2014 Average Monthly Sales January-June: ");
       calcYearAverage(year2014);

       System.out.println();

       System.out.print("2015 Average Monthly Sales January-June: ");
       calcYearAverage(year2015);

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

       System.out.print("Highest selling month for 2014: ");
       calcHighest(year2014);

       System.out.println();

       System.out.print("Highest selling month for 2015: ");
       calcHighest(year2015);

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

       System.out.print("Lowest selling month for 2014: ");
       calcLowest(year2014);

       System.out.println();

       System.out.print("Lowest selling month for 2015: ");
       calcLowest(year2015);

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

       System.out.println("Average Sales for Each Month");
       eachMonthAverage(year2014,year2015);

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

       System.out.println("Percentage Increase/Decrease for Each Month");
       eachMonthChange(year2014,year2015);
   }


    //Method for returning the name of the month after being sent the count in each case
    public static String getMonth(int monthIndex)
    {
       return new DateFormatSymbols().getMonths()[monthIndex];
    }


   //Method for calculating average for each 6 month period
   static void calcYearAverage(int [] array)
    {
       int total = 0;
       double average;

       for (int z = 0; z<array.length; z++)
       {
          total = total + array[z];
       }

       average = total/array.length;

       System.out.println("£"+average);
    }


    //Method for calculating highest selling month in each 6 month period
    static void calcHighest(int [] highest)
   {
      int highestFigure = 0;
      int highestMonth = 0;

      for (int x = 0; x<highest.length; x++)
      {
         if(highest[x]>highestFigure)
            highestFigure = highest[x];
            highestMonth = x;
      }

      System.out.println(getMonth(highestMonth) +" with £" + highestFigure);
   }


   //Method for calculating lowest selling month in each 6 month period
    static void calcLowest(int [] lowest)
   {
      int lowestFigure = 999999;
      int lowestMonth = 0;

      for (int y = 0; y<lowest.length; y++)
      {
         if(lowest[y]<lowestFigure)
            lowestFigure = lowest[y];
            lowestMonth = y;
      }
      System.out.println(getMonth(lowestMonth)+" with £"+lowestFigure);
   }


   //Method for calculating the average for each month for both years
   static void eachMonthAverage(int [] eachAverage1, int [] eachAverage2)
   {
      int total;
      double average;

      for (int i = 0; i<eachAverage1.length; i++)
      {
         total = eachAverage1[i] + eachAverage2[i];
         average = total/2.0;
         System.out.println("The average sales for "+getMonth(i)+" is "+average);
      }
   }


   //Method for calculating the percentage change in sales figures for each month
   static void eachMonthChange(int [] eachChange1, int [] eachChange2)
   {
      int increase;
      double percentage;

      for (int p = 0; p<eachChange1.length; p++)
      {
         increase = eachChange1[p] - eachChange2[p];
         percentage = (increase/eachChange1[p]) *100;
         System.out.println("The Percentage Change in Sales between "+getMonth(p)+" 2014 and "+getMonth(p)+" 2015 is "+percentage+"%");
      }
   }
}
#5. Posted:
ip
  • 2 Million
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
I would look into bubble sorting for the method on where you are taking the lowest to highest selling month's. It can be a little confusing at first but this pseudocode will help you out with it.

You can also find the article [ Register or Signin to view external links. ]

procedure bubbleSort( list : array of items )

   loop = list.count;
   
   for i = 0 to loop-1 do:
      swapped = false
      
      for j = 0 to loop-1 do:
     
         /* compare the adjacent elements */   
         if list[j] > list[j+1] then
            /* swap them */
            swap( list[j], list[j+1] )      
            swapped = true
         end if
         
      end for
     
      /*if no number was swapped that means
      array is sorted now, break the loop.*/
     
      if(not swapped) then
         break
      end if
     
   end for
   
end procedure return list
#6. Posted:
MongoBreath
  • Rising Star
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
ip wrote I would look into bubble sorting for the method on where you are taking the lowest to highest selling month's. It can be a little confusing at first but this pseudocode will help you out with it.

You can also find the article [ Register or Signin to view external links. ]

procedure bubbleSort( list : array of items )

   loop = list.count;
   
   for i = 0 to loop-1 do:
      swapped = false
      
      for j = 0 to loop-1 do:
     
         /* compare the adjacent elements */   
         if list[j] > list[j+1] then
            /* swap them */
            swap( list[j], list[j+1] )      
            swapped = true
         end if
         
      end for
     
      /*if no number was swapped that means
      array is sorted now, break the loop.*/
     
      if(not swapped) then
         break
      end if
     
   end for
   
end procedure return list


Thanks for the help mate.

I know your suggestion is probably a more efficient way of doing things but I haven't learned about bubble sorting so my lecturer would be very skeptical lol.

Can you spot in particular why I keep getting 6 (June) for the x value in calcHighest and also the y value in calcLowest?

And also the percentage problem
#7. Posted:
MongoBreath
  • Rising Star
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
Alright, I've sorted the Highest and Lowest print out problem - I didn't have the {} brackets around my IF statement!!

Now all I need help with is sorting out the percentage problem, I'm having real difficulty with it

Here is my further updated code:

import java.text.DateFormatSymbols;
import java.util.Scanner;
class Assignment2
{
    public static void main(String [] args)
    {
       Scanner keyboard = new Scanner(System.in);
       int [] year2014 = new int [6];
       int [] year2015 = new int [6];
 
       
       //This will input and store the sales figures for 2014
       for (int count = 0; count<6; count++)
       {
          System.out.println("Enter the Monthly Sales Figures for "+getMonth(count)+" 2014");
          year2014[count] = keyboard.nextInt();
       }



       //This will input and store the sales figures for 2015
       for (int count = 0; count<6; count++)
       {
          System.out.println("Enter the Monthly Sales Figures for "+getMonth(count)+" 2015");
          year2015[count] = keyboard.nextInt();
       }

       //Below the results of the methods will be output

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

       System.out.print("2014 Average Monthly Sales January-June: ");
       calcYearAverage(year2014);

       System.out.println();

       System.out.print("2015 Average Monthly Sales January-June: ");
       calcYearAverage(year2015);

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

       System.out.print("Highest selling month for 2014: ");
       calcHighest(year2014);

       System.out.println();

       System.out.print("Highest selling month for 2015: ");
       calcHighest(year2015);

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

       System.out.print("Lowest selling month for 2014: ");
       calcLowest(year2014);

       System.out.println();

       System.out.print("Lowest selling month for 2015: ");
       calcLowest(year2015);

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

       System.out.println("Average Sales for Each Month");
       eachMonthAverage(year2014,year2015);

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

       System.out.println("Percentage Increase/Decrease for Each Month");
       eachMonthChange(year2014,year2015);
   }
   
   
    //Method for returning the name of the month after being sent the count in each case
    public static String getMonth(int monthIndex)
    {
       return new DateFormatSymbols().getMonths()[monthIndex];
    }

   
   //Method for calculating average for each 6 month period
   static void calcYearAverage(int [] array)
    {
       int total = 0;
       double average;

       for (int z = 0; z<array.length; z++)
       {
          total = total + array[z];
       }

       average = total/array.length;

       System.out.println("£"+average);
    }


    //Method for calculating highest selling month in each 6 month period
    static void calcHighest(int [] highest)
   {
      int highestFigure = 0;
      int highestMonth = 0;

      for (int x = 0; x<highest.length; x++)
      {
         if(highest[x]>highestFigure)
         {
            highestFigure = highest[x];
            highestMonth = x;
         }
      }

      System.out.println(getMonth(highestMonth) +" with £" + highestFigure);
   }


   //Method for calculating lowest selling month in each 6 month period
    static void calcLowest(int [] lowest)
   {
      int lowestFigure = 999999;
      int lowestMonth = 0;

      for (int y = 0; y<lowest.length; y++)
      {
         if(lowest[y]<lowestFigure)
         {
            lowestFigure = lowest[y];
            lowestMonth = y;
         }
      }

      System.out.println(getMonth(lowestMonth)+" with £"+lowestFigure);
   }


   //Method for calculating the average for each month for both years
   static void eachMonthAverage(int [] eachAverage1, int [] eachAverage2)
   {
      int total;
      double average;

      for (int i = 0; i<eachAverage1.length; i++)
      {
         total = eachAverage1[i] + eachAverage2[i];
         average = total/2.0;
         System.out.println("The average sales for "+getMonth(i)+" is £"+average);
      }
   }


   //Method for calculating the percentage change in sales figures for each month
   static void eachMonthChange(int [] eachChange1, int [] eachChange2)
   {
      int increase;
      double percentage;

      for (int p = 0; p<eachChange1.length; p++)
      {
         increase = eachChange1[p] - eachChange2[p];
         percentage = (increase/eachChange1[p]) *100;
         System.out.println("The Percentage Change in Sales between "+getMonth(p)+"/2014 and "+getMonth(p)+"/2015 is "+percentage+"%");
      }
   }
}
#8. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
Whats wrong with the percentage method is there an error or what?
#9. Posted:
MongoBreath
  • Rising Star
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
Status: Offline
Joined: Oct 01, 201014Year Member
Posts: 723
Reputation Power: 29
Xaldin wrote Whats wrong with the percentage method is there an error or what?


Not an actual error compiling, it runs okay but it's not doing anywhere near what I want it to do

The resulting percentages are all 0.0%
#10. Posted:
Xaldin
  • 2 Million
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
MongoBreath wrote
Xaldin wrote Whats wrong with the percentage method is there an error or what?


Not an actual error compiling, it runs okay but it's not doing anywhere near what I want it to do

The resulting percentages are all 0.0%

I think its because you are dividing two integers which would be a decimal but that's getting truncated because its an int resulting in zero times 100 every time.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.