Results 1 to 7 of 7

Thread: Assignment Help

  1. #1
    deantmcg is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Post 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 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+"%");
    		}
    	}
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default 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);
    result is now 20.0
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default 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;
    At first sight, this looks ok, but if you look closely, the curly braces are missing, so the if statment only executes the first statement after it and the second statement is ALWAYS executed. This is what the compiler sees:
    Java Code:
    if(highest[x]>highestFigure) {
      highestFigure = highest[x];
    }
    highestMonth = x;
    But this is not what you want, that would be this:
    Java Code:
    if(highest[x]>highestFigure) {
       highestFigure = highest[x];   
       highestMonth = x;
    }
    Make it a habit to ALWAYS use curly braces around an if, even if it's just one statement.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    deantmcg is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default Re: Assignment Help

    Quote Originally Posted by SurfMan View Post
    About the "june", you're missing some curly braces in those methods:
    Java Code:
    if(highest[x]>highestFigure)
                    highestFigure = highest[x];
                    highestMonth = x;
    At first sight, this looks ok, but if you look closely, the curly braces are missing, so the if statment only executes the first statement after it and the second statement is ALWAYS executed. This is what the compiler sees:
    Java Code:
    if(highest[x]>highestFigure) {
      highestFigure = highest[x];
    }
    highestMonth = x;
    But this is not what you want, that would be this:
    Java Code:
    if(highest[x]>highestFigure) {
       highestFigure = highest[x];   
       highestMonth = x;
    }
    Make it a habit to ALWAYS use curly braces around an if, even if it's just one statement.


    Thanks for the help I was stuck on that for a while and it turns out to be something so simple, thanks again!


    Although, I really am having difficulty wrapping my head around the percentage errors

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Assignment Help

    Quote Originally Posted by deantmcg View Post
    Although, I really am having difficulty wrapping my head around the percentage errors
    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
    while either of the following prints out 2.5.

    Java Code:
    double v = (double)10/4;
    System.out.println(v); // prints 2.5
    or

    Java Code:
    double v = 10/(double)4;
    System.out.println(v);// prints 2.5
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    deantmcg is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default 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 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);
    
    		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));
    		}
    	}
    }

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Assignment Help

    Glad you got it to work. Learn something new every day! :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. need help at assignment
    By ng min teck in forum New To Java
    Replies: 20
    Last Post: 08-01-2012, 04:57 PM
  2. Assignment
    By neowolf in forum New To Java
    Replies: 3
    Last Post: 07-15-2012, 05:20 PM
  3. Need help with assignment.
    By smileybear33 in forum New To Java
    Replies: 2
    Last Post: 04-26-2011, 03:47 AM
  4. assignment
    By mr.othman in forum New To Java
    Replies: 4
    Last Post: 12-03-2010, 02:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •