Results 1 to 7 of 7
Thread: Assignment Help
 12142016, 12:34 PM #1Member
 Join Date
 Dec 2016
 Posts
 8
 Rep Power
 0
Assignment Help
Hello Everyone, I'm new to Java and this is my first post of probably many on this forum.
I'm having problems with an assignment I'm currently doing. I'm writing a program that reads in and analyses monthly sales figures.
In the method for calculating the highest [starting line 97] and lowest [starting line 114] selling months, I keep getting the value 6 (June) no matter what I input.
Also, my final method for calculating the percentage change isn't working either. [starting line 145]
Thanks in advance for any help you can give me, I would be very grateful!
Have a look at my code below
Java 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 JanuaryJune: "); calcYearAverage(year2014); System.out.println(); System.out.print("2015 Average Monthly Sales JanuaryJune: "); 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+"%"); } } }
 12142016, 12:42 PM #2
Re: Assignment Help
About the precentages, if you divide by an int, the result is an int. You should cast either operand to a double, which is safe because int to double is a widening cast, which is allowed:
Java Code:int i = 10; int j = 2; double result = (j / (double)i) * 100; System.out.println("result = " + result);
"It's not fixed until you stop calling the problem weird and you understand what was wrong."  gimbal2™ © 2013
 12142016, 12:46 PM #3
Re: Assignment Help
About the "june", you're missing some curly braces in those methods:
Java Code:if(highest[x]>highestFigure) highestFigure = highest[x]; highestMonth = x;
Java Code:if(highest[x]>highestFigure) { highestFigure = highest[x]; } highestMonth = x;
Java Code:if(highest[x]>highestFigure) { highestFigure = highest[x]; highestMonth = x; }
"It's not fixed until you stop calling the problem weird and you understand what was wrong."  gimbal2™ © 2013
 12142016, 06:49 PM #4Member
 Join Date
 Dec 2016
 Posts
 8
 Rep Power
 0
 12142016, 07:20 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Assignment Help
When you divide two integers, the fractional part is discarded. so the following prints out 2.
Java Code:double v = 10/4; System.out.println(v); // prints 2
Java Code:double v = (double)10/4; System.out.println(v); // prints 2.5
Java Code:double v = 10/(double)4; System.out.println(v);// prints 2.5
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 12162016, 02:27 AM #6Member
 Join Date
 Dec 2016
 Posts
 8
 Rep Power
 0
Re: Assignment Help
If anyone is interested, I fixed this code thanks to some of your help!
I'm very grateful and happy with the end product of my first java program.
Here is the code below if you would like to see what I did.
Thanks a lot everyone!
Java Code:import java.text.DateFormatSymbols; import java.text.DecimalFormat; 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 JanuaryJune: "); calcYearAverage(year2014); System.out.println(); System.out.print("2015 Average Monthly Sales JanuaryJune: "); 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); System.out.println("  "); System.out.println("Projected Sales for Each Month in 2016"); eachProjected(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; DecimalFormat moneyFormat = new DecimalFormat("0.00"); for (int z = 0; z<array.length; z++) { total = total + array[z]; } average = total/array.length; System.out.println("£"+moneyFormat.format(average)); } //Method for calculating highest selling month in each 6 month period static void calcHighest(int [] highest) { int highestFigure = 0; int highestMonth = 0; DecimalFormat moneyFormat = new DecimalFormat("0.00"); for (int x = 0; x<highest.length; x++) { if(highest[x]>highestFigure) { highestFigure = highest[x]; highestMonth = x; } } System.out.println(getMonth(highestMonth) +" with £" + moneyFormat.format(highestFigure)); } //Method for calculating lowest selling month in each 6 month period static void calcLowest(int [] lowest) { int lowestFigure = 999999; int lowestMonth = 0; DecimalFormat moneyFormat = new DecimalFormat("0.00"); for (int y = 0; y<lowest.length; y++) { if(lowest[y]<lowestFigure) { lowestFigure = lowest[y]; lowestMonth = y; } } System.out.println(getMonth(lowestMonth)+" with £"+moneyFormat.format(lowestFigure)); } //Method for calculating the average for each month for both years static void eachMonthAverage(int [] eachAverage1, int [] eachAverage2) { int total; double average; DecimalFormat moneyFormat = new DecimalFormat("0.00"); 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 £"+moneyFormat.format(average)); } } //Method for calculating the percentage change in sales figures for each month static void eachMonthChange(int [] eachChange1, int [] eachChange2) { double increase; double percentage; DecimalFormat percentFormat = new DecimalFormat("0.000"); // format percentage change to 3 DP for (int p = 0; p<eachChange1.length; p++) { increase = eachChange2[p]  eachChange1[p]; percentage = (increase/eachChange1[p]) *100; System.out.println("The Percentage Change in Sales between "+getMonth(p)+" 2014 and "+getMonth(p)+" 2015 is "+percentFormat.format(percentage)+"%"); } } //Method for calculating the projected sales figures for each month in 2016 static void eachProjected(int [] sales2014, int [] sales2015) { double increase; double multiplier; double newIncrease; double sales2016; DecimalFormat moneyFormat = new DecimalFormat("0.00"); for (int v = 0; v<sales2014.length; v++) { increase = sales2015[v]  sales2014[v]; multiplier = (increase/sales2014[v]); newIncrease = sales2015[v]*multiplier; sales2016 = sales2015[v] + newIncrease; System.out.println("The projected sales for "+getMonth(v)+" 2016 is £"+moneyFormat.format(sales2016)); } } }
 12162016, 09:09 AM #7
Similar Threads

need help at assignment
By ng min teck in forum New To JavaReplies: 20Last Post: 08012012, 04:57 PM 
Assignment
By neowolf in forum New To JavaReplies: 3Last Post: 07152012, 05:20 PM 
Need help with assignment.
By smileybear33 in forum New To JavaReplies: 2Last Post: 04262011, 03:47 AM 
assignment
By mr.othman in forum New To JavaReplies: 4Last Post: 12032010, 02:55 PM
Bookmarks