|
|
Line 42: |
Line 42: |
| | | |
| | | |
− | [[Image:Wiki_chars01.jpg|center|267px]] | + | [[Image:Wiki_chars01.jpg|center]] |
| <BR> | | <BR> |
| | | |
Revision as of 00:09, 9 April 2010
Back to the Program-A-Day homepage
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.
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.
|
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.
|
Code
Solution Code
import javax.swing.*;
public class Sales{
public static void main(String [] args){
double [] sales = new double [7]; //array of days in the week, for the sales per day
double [] commission = new double[sales.length];
double percentage = 0.07;
sales = fillSales(sales.length); // fill the array with the sales from each employee
printArray("Sales for employee #", sales); //print the array to confirm that you have filled it properly
commission = calculateCommission(sales, percentage); //calculate the commission on each employee's sales
printArray("Commission for employee #", commission); //print out the commission for each employee
}
public static double[] fillSales (int length){ //fillArray method from earlier example, slightly modified
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){ //printArray method from earlier problem, slightly modified
//this time it takes a string to print before each element of the array
//so that it prints out, for example, "This is the sales for employee
//#1: xxx"
for (int i=0; i<sales.length;i++){ //for loop to go through each element in the array
System.out.println(tagline + (i+1)+ ": " + sales[i]); //print the current element
}
System.out.println();
}
public static double[] calculateCommission(double[] sales, double percentage){//method to calculate the commission for each employee
double [] commission = new double[sales.length]; //creates new array to put the commissions into
for (int i=0; i< sales.length; i++){ //for loop to go through the sales array
commission [i] = sales[i] * percentage; //and calculate each employee's commission
}
return commission;
}
}
|
Back to the Program-A-Day homepage