Difference between revisions of "CommissionCalculator"
(Removed photo since there) |
|||
Line 40: | Line 40: | ||
|SideSectionTitle=By Students | |SideSectionTitle=By Students | ||
|SideSection=When I started with Java, I had the hardest time working with arrays and loops without drawing out the array. In a program like this one, I would have had to draw out the sales array and the commissions array, and then step-by-step go through it to calculate the commission. Once I was able to wrap my head around arrays, it got a lot easier. It was one of those things that just came with practice. | |SideSection=When I started with Java, I had the hardest time working with arrays and loops without drawing out the array. In a program like this one, I would have had to draw out the sales array and the commissions array, and then step-by-step go through it to calculate the commission. Once I was able to wrap my head around arrays, it got a lot easier. It was one of those things that just came with practice. | ||
− | |||
− | |||
− | |||
− | |||
Revision as of 00:10, 9 April 2010
Back to the Program-A-Day homepage
ProblemToday we'll look at combining everything you've done this week into one "large" program.
public class Sales{ public static void main(String [] args){ double [] sales = new double [7]; double [] commission = new double[sales.length]; double percentage = 0.07; sales = fillSales(sales.length); printArray("Sales for employee #", sales); } public static double[] fillSales (int length){ double [] sales =new double[length]; String temp; for (int i=0; i<length; i++){ temp = JOptionPane.showInputDialog(null, "Input the sales for employee " + (i+1) + ":"); sales[i] = Double.parseDouble(temp); } return sales; } public static void printArray(String tagline, double [] sales){ for (int i=0; i<sales.length;i++){ System.out.println(tagline + (i+1)+ ": " + sales[i]); } System.out.println(); } } |
By StudentsWhen I started with Java, I had the hardest time working with arrays and loops without drawing out the array. In a program like this one, I would have had to draw out the sales array and the commissions array, and then step-by-step go through it to calculate the commission. Once I was able to wrap my head around arrays, it got a lot easier. It was one of those things that just came with practice. | |
---|---|---|
SolutionCreate a method called calculateCommission which takes the sales array, as well as a commission percentage double as input, and returns a new array of doubles which contains the list of commissions for each employee. | ||
Code |