Difference between revisions of "CommissionCalculator"
(Added quotations to student quotes) |
|||
(9 intermediate revisions by 4 users not shown) | |||
Line 2: | Line 2: | ||
|Problem= Today we'll look at combining everything you've done this week into one "large" program. | |Problem= Today we'll look at combining everything you've done this week into one "large" program. | ||
+ | |||
Imagine you are a worker in the Human Resources department, responsible for calculating the commission each salesperson earns on his or her sales. Each sales person earns 7% commission on their total sales, and the total sales for each person are stored in an array, with the array index corresponding to each salesperson's employee number. | Imagine you are a worker in the Human Resources department, responsible for calculating the commission each salesperson earns on his or her sales. Each sales person earns 7% commission on their total sales, and the total sales for each person are stored in an array, with the array index corresponding to each salesperson's employee number. | ||
− | + | {{CodeBlock | |
+ | |Code=public class Sales{ | ||
public static void main(String [] args){ | public static void main(String [] args){ | ||
− | double [] sales = new double [7]; | + | double [] sales = new double [7]; |
double [] commission = new double[sales.length]; | double [] commission = new double[sales.length]; | ||
double percentage = 0.07; | double percentage = 0.07; | ||
− | sales = fillSales(sales.length); | + | sales = fillSales(sales.length); |
− | printArray("Sales for employee #", sales); | + | printArray("Sales for employee #", sales); |
} | } | ||
− | public static double[] fillSales (int length){ | + | public static double[] fillSales (int length){ |
double [] sales =new double[length]; | double [] sales =new double[length]; | ||
String temp; | String temp; | ||
Line 28: | Line 30: | ||
return sales; | return sales; | ||
} | } | ||
− | public static void printArray(String tagline, double [] sales){ | + | public static void printArray(String tagline, double [] sales){ |
− | + | ||
− | + | ||
− | + | ||
− | for (int i=0; i<sales.length;i++){ | + | for (int i=0; i<sales.length;i++){ |
− | System.out.println(tagline + (i+1)+ ": " + sales[i]); | + | System.out.println(tagline + (i+1)+ ": " + sales[i]); |
} | } | ||
System.out.println(); | System.out.println(); | ||
} | } | ||
− | } | + | } |
− | + | }} | |
− | + | ||
− | + | |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." | ||
− | |Solution= | + | |Solution= Create 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. |
− | |SolutionCode= | + | |SolutionCode=import javax.swing.*; |
public class Sales{ | public class Sales{ | ||
Line 98: | Line 97: | ||
} | } | ||
− | |||
}} | }} |
Latest revision as of 15:32, 8 December 2011
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 Students"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." | |
---|---|---|
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 |