Difference between revisions of "CommissionCalculator"

From CompSciWiki
Jump to: navigation, search
(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.
  
  
<pre>public class Sales{
+
{{CodeBlock
 +
|Code=public class Sales{
 
public static void main(String [] args){
 
public static void main(String [] args){
double [] sales = new double [7]; //array of days in the week, for the sales per day
+
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); // fill the array with the sales from each employee
+
sales = fillSales(sales.length);
printArray("Sales for employee #", sales); //print the array to confirm that you have filled it properly
+
printArray("Sales for employee #", sales);
 
}
 
}
  
public static double[] fillSales (int length){ //fillArray method from earlier example, slightly modified
+
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){ //printArray method from earlier problem, slightly modified
+
public static void printArray(String tagline, double [] sales){
//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
+
for (int i=0; i<sales.length;i++){
System.out.println(tagline + (i+1)+ ": " + sales[i]); //print the current element
+
System.out.println(tagline + (i+1)+ ": " + sales[i]);
 
}
 
}
 
System.out.println();
 
System.out.println();
 
}
 
}
}</pre>
+
}
 
+
}}
|SideSection=
+
  
<BR>
+
|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 Description
+
|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=<pre>import javax.swing.*;
+
|SolutionCode=import javax.swing.*;
  
 
public class Sales{
 
public class Sales{
Line 98: Line 97:
  
 
}
 
}
</pre>
 
 
}}
 
}}

Latest revision as of 15:32, 8 December 2011

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

Back to the Program-A-Day homepage