# Arrays and Methods

Page 1 of 1

## 4 Replies - 863 Views - Last Post: 08 November 2009 - 08:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=137579&amp;s=fe7ca74f332a5509911dc4f65a07790c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tricket_7

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

# Arrays and Methods

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;
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);
{
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

• Games, Graphs, and Auctions

Reputation: 11237
• Posts: 42,289
• 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.

### #3 tricket_7

Reputation: 0
• 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

### #4 jtphenom

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

## Re: Arrays and Methods

Posted 08 November 2009 - 08:50 PM

tricket_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!

### #5 Fuzzyness

• Comp Sci Student

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

## Re: Arrays and Methods

Posted 08 November 2009 - 08:54 PM

tricket_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