Arrays and Methods

each method to receive the array and return a result.

Page 1 of 1

4 Replies - 688 Views - Last Post: 08 November 2009 - 08:54 PM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Arrays and Methods

Post icon  Posted 08 November 2009 - 03:06 PM

Here you are asked to add three methods to the program below. Look at the main method that has already been provided and does not need to be changed.
The main method declares a variable to store an array of doubles named monthlyIncome, and another variable to store the month names. The main method uses a loop to
provide the user with a menu. The user can choose to quit or view the total, or the highest income, or a subtotal of any range of months. In order to obtain these
amounts the loop calls the methods getTotalIncome(), getHighestIncome(), and getSubTotal().

You must provide the code for these three methods. Each method receive the array and returns a double with the result. The getSubTotal method also receives the start
and end months. You will need to use a loop in each method to obtain the desired results. You already wrote the code to calculate the total in Ch0Ex01, and you have
seen examples of calculating the highest value. Be careful to determine the correct starting and ending index values for the subtotal - be sure to test your results
and remember that the array is indexed from 0 to 11 although the user will input starting and ending month numbers between 1 and 12.

This is the assignment, I need some help on this one, I have started with it, but have no idea where to go from here.
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Ch08Ex05
{
	public static void main (String [] args)
	{
		DecimalFormat currency = new DecimalFormat("$#,##0.00");
		boolean continuing = true;
		char menuChoice;
		int startMonth, endMonth;
		double result;
		
		double [] monthlyIncome = 	{	4000.00, 4000.00, 5000.00, 3000.00,
										5000.00, 4000.00, 4000.00, 5000.00,
										6000.00, 4000.00, 3000.00, 3000.00 
									};

		String[] monthNames = {	"January", "February", "March", "April", "May", "June", 
								"July", "August", "September", "October", "November", "December"};
 				
		while (continuing)
		{		
			menuChoice = JOptionPane.showInputDialog(	"SELECT 1, 2, or 3\n"+
													"0 Quit\n" +
													"1 View Total Income\n" +
													"2 View Highest Month\n" + 
													"3 View SubTotal\n").charAt(0);
			switch (menuChoice) 
			{
				case '0': 	continuing = false; 
							break;
				case '1': 	result = getTotalIncome(monthlyIncome);
							JOptionPane.showMessageDialog(null, "TOTAL INCOME: " + currency.format(result)); 
							break;
				case '2': 	result = getHighestIncome(monthlyIncome);
							JOptionPane.showMessageDialog(null, "HIGHEST MONTHLY INCOME: " + currency.format(result)); 
							break;
				case '3': 	startMonth = Integer.parseInt(JOptionPane.showInputDialog("Starting month: ")); 
							endMonth = Integer.parseInt(JOptionPane.showInputDialog("Ending month: ")); 
							
							result = getSubTotal(monthlyIncome, startMonth, endMonth);
							
							JOptionPane.showMessageDialog(null, "SUBTOTAL FOR : " + monthNames[startMonth - 1] + " to " +
							monthNames[endMonth - 1] + " is " + currency.format(result));
							break;
				default: 	JOptionPane.showMessageDialog(null, "You must choose 0, 1, 2 or 3");
			} //switch
		} // while
	} // main

	
	/**
		The getTotalIncome() method acccepts an arrray of doubles
		sums the values and returns the result.
		@param monthlyIncome An array of doubles
		@return A double containing the sum of values in the array
	
	*/
	
	// your code for getTotalIncome() here
		public double getTotalIncome(double [] monthlyIncome)
		{
		double result = 0; //Accumulator
		
		for (int index = 0; index < monthlyIncome.length; index++)
		result += monthlyIncome[index];
		
		return result;
		}
					
		
	/**
		The getHighestIncome() method acccepts an arrray of doubles
		determines the highest value and returns the result.
		@param monthlyIncome An array of doubles
		@return A double containing the highest values in the array
	
	*/
	
	// your code for getHighestIncome() here
	public double getHighestIncome(double [] monthlyIncome)
	{
		double result = monthlyIncome[0];
		for(int index = 1; index < monthlyIncome.length; index++)
		{
			if (monthlyIncome[index] > result)
			result = monthlyIncome[index];
		}
		return result;
	}
	/**
		The getSubTotal() method acccepts an array of doubles,
		a starting position, and an ending position.
		These positions indicate the order number, not the index 
		location (for example the first position is 1 not 0).
		The method calculates a subtotal by summing all values 
		between and including these two positions.
		@param monthlyIncome An array of doubles
		@param start An integer containing the start position
		@param and An integer containing the end position
		@return A double containing the subtotal
	
	*/
	
	// your code for getSubTotal() here
	public double getSubTotal(double [] monthlyIncome, int startMonth, int endMonth)
	{
//I have no idea what goes here
		double result;
		for (int index = 0; index < monthlyIncome.length; index++)
		result = startMonth - endMonth;
	return result;
}	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Arrays and Methods

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,571
  • Joined: 27-December 08

Re: Arrays and Methods

Posted 08 November 2009 - 03:19 PM

For subTotal, you start your loop at startMonth and terminate it at endMonth instead of starting at 0 and ending at length-1. Like so:
public double subTotal(double[] d, int startMonth, int endMonth){
	for(int i = startMonth; i < endMonth; i++)
..more code..
}



In this way, you are only reading a section of the array.
Was This Post Helpful? 0
  • +
  • -

#3 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Arrays and Methods

Posted 08 November 2009 - 08:37 PM

so I got that working, but I am stuck on the calculation to find out the subtotal for the months chosen
	// your code for getSubTotal() here
	public static double getSubTotal(double [] monthlyIncome, int startMonth, int endMonth)
	{
		double result = 0;
		 for(int index = startMonth; index < endMonth; index++)
		result += monthlyIncome[index];
	return result;
}	


Any hints would be appreciated
Was This Post Helpful? 0
  • +
  • -

#4 jtphenom  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 129
  • Joined: 06-November 09

Re: Arrays and Methods

Posted 08 November 2009 - 08:50 PM

View Posttricket_7, on 8 Nov, 2009 - 07:37 PM, said:

so I got that working, but I am stuck on the calculation to find out the subtotal for the months chosen
	// your code for getSubTotal() here
	public static double getSubTotal(double [] monthlyIncome, int startMonth, int endMonth)
	{
		double result = 0;
		 for(int index = startMonth; index < endMonth; index++)
		result += monthlyIncome[index];
	return result;
}	


Any hints would be appreciated

Have you tried this code and seen what happens? If so, post your results. If not, try it! :)
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Arrays and Methods

Posted 08 November 2009 - 08:54 PM

View Posttricket_7, on 8 Nov, 2009 - 07:37 PM, said:

so I got that working, but I am stuck on the calculation to find out the subtotal for the months chosen
	// your code for getSubTotal() here
	public static double getSubTotal(double [] monthlyIncome, int startMonth, int endMonth)
	{
		double result = 0;
		 for(int index = startMonth; index < endMonth; index++)
		result += monthlyIncome[index];
	return result;
}	


Any hints would be appreciated

You need to include the end month, for he does still have to pay for that month. index <=endMonth .That code should come up with the correct subtotal. however make the variable result = 0.0, for it is a double so you should treat it like one. :)

Other then that it should work.

This post has been edited by Fuzzyness: 08 November 2009 - 08:55 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1