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

New Topic/Question
Reply




MultiQuote








|