944,066 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2762
  • Java RSS
Oct 27th, 2006
0

I think Loop problem

Expand Post »
I'm not getting any errors but can't get the print out I should.
Any Idea's?

Pseudo code first, then Program Code, and the printout look;

Design, code and thoroughly test the following programming problem: A company pays its salespeople a base weekly salary plus a commission based on gross sales for the week. The salespeople receive $200 per week in base salary plus 9% of their gross sales for that week. For example, a salesperson who grosses $5,000 in sales in a week receives $200 in base salary plus a commission of 9% of $5,000 ($450), or total pay of $650 for the week.
1. Write an application that enables the user to enter a salesperson’s weekly sales and calculates and displays that salesperson’s weekly pay. In addition, the application keeps track of and displays a count of the total weekly pay of salespeople by the ranges described below;
2. In addition, your program must:
2.1. Use descriptive identifier names that reveal the exact purpose of the variable in your program. Do not use abbreviations;
2.2. Enable the user to enter the weekly sales for each salesperson;
2.3. Display the weekly pay for the current salesperson;
2.4. Use a one-dimensional array of counters to keep track of how many of the salespeople earn weekly pay in each of the following nine ranges (assume that each salesperson’s weekly pay is truncated to an integer amount):

2.4.1. $200 – 299;
2.4.2. $300 – 399;
2.4.3. $400 – 499;
2.4.4. $500 – 599;
2.4.5. $600 – 699;
2.4.6. $700 – 799;
2.4.7. $800 – 899;
2.4.8. $900 – 999;
2.4.9. $1,000 and over

2.5. Locate and increment the appropriate array entry counter for salesperson’s weekly pay;
2.6. Display the array of salespeople by weekly earning ranges.
2.7. Use the techniques shown in this document’s Exhibit C, Example Program Input and Output;
2.8. Follow the UML class diagram and the pseudo code shown in the document’s “Exhibit A-1” through “Exhibit B-2”;
Exhibit A-1: UML Class Diagram of Class SalesCommission
SalesCommission

+countRanges()

Exhibit A-2: Pseudo code for countRanges Method of Class SalesCommission
A-2-01) INSTANTIATE a local variable named input of the Scanner class
A-2-02) Define a local named constant of type double named commissionRateOnGrossSales with a value of 0.09
A-2-03) Define a local named constant of type double named baseWeeklySalary with a value of 200.0
A-2-04) Define a local named constant of type double named weeklyPayRangeIncrement with a value of 100.0
A-2-05) Define a local variable of type double named salesAmount
A-2-06) Define a local variable of type double named weeklyPay
A-2-07) Define a local named constant of type int named weeklyPayRanges with a value of 9
A-2-08) Define a local variable of type int named weeklyPayRange
A-2-09) Define a local named constant of type String named salesAmountPrompt with a value of
“Enter salesperson %2d’s weekly sales, or <ctrl>z to stop: Ä
A-2-10) Define a local variable of type int named salespersonNumber with a value of 1
A-2-11) INSTANTIATE a local array variable with weeklyPayRanges elements of type int named countOfWeeklyPayByRange
A-2-12) DISPLAY the task / programmer identification line
A-2-13) DISPLAY the “Weekly Sales and Weekly Pay by Salesperson” line
A-2-14) Initialize each element of the countOfWeeklyPayByRange array to zero:
A-2-14-1) FOR weeklyPayRange FROM zero TO (countOfWeeklyPayByRange.length – 1) BY 1
A-2-14-2) ASSIGN zero TO countOfWeeklyPayByRange [ weeklyPayRange ]
A-2-14-3) END FOR
A-2-15) Using method printf, DISPLAY the following prompt:
(“\tÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ” + salesAmountPrompt, salesPersonNumber)
A-2-16) WHILE (input.hasNext())
A-2-16-1) ASSIGN input.nextDouble() TO salesAmount
A-2-16-2) ASSIGN baseWeeklySalary plus (salesAmount multiplied by commissionRateOnGrossSales)
TO weeklyPay
A-2-16-3) ASSIGN (int) (weeklyPay / weeklyPayRangeIncrement) TO weeklyPayRange
A-2-16-4) IF (weeklyPayRange > (countOfWeeklyPayByRange.length + 1)) THEN
ASSIGN (countOfWeeklyPayByRange.length + 1) TO weeklyPayRange
END IF
A-2-16-5) INCREMENT countOfWeeklyPayByRange [ weeklyPayRange – 2 ]
A-2-16-6) Using method printf, DISPLAY the following prompt:
(“\t Weekly Pay for Salesperson %2d is $%,8.2f;Ä” + salesAmountPrompt,
salesPersonNumber, weeklyPay, ++salesPersonNumber)
END WHILE
A-2-17) DISPLAY the “Salesperson Count by Weekly Pay Range:” line
A-2-18) Using method printf, DISPLAY the following heading line: (“\n\t%14s\t\t%11s”, “ÄÄWeekly PayÄÄ”, “Salesperson”)
A-2-19) Using method printf, DISPLAY the following heading line: (“\n\t%14s\t\t%11s\n”, “ÄÄÄÄRangeÄÄÄÄ”, “ÄÄÄCountÄÄÄ”)
A-2-20) FOR weeklyPayRange FROM zero TO (countOfWeeklyPayByRange.length – 2) BY 1
A-2-20-1) Using method printf, DISPLAY the following line: (“\n\t$%,5dÄ-Ä$%,5d\t\t%,6d”,
A-2-20-2) (int)( baseWeeklySalary + (weeklyPayRangeIncrement * weeklyPayRange),
A-2-20-3) (int)(99.0 + baseWeeklySalary + (weeklyPayRangeIncrement * weeklyPayRange),
A-2-20-4) countOfWeeklyPayByRange[ weeklyPayRange ] )
A-2-21) END FOR
A-2-22) Using method printf, DISPLAY the following line: (“\n\t$1,000 and over\t\t%,6d”,
countOfWeeklyPayByRange[ (countOfWeeklyPayByRange.length – 1) ] )
A-2-23) DISPLAY a blank line followed by the “End of program” line

Exhibit B-1: UML Class Diagram of Class SalesCommissionTest
SalesCommissionTest

+main(args[ ] : String)

Exhibit B-2: Pseudo code for main Method of Class SalesCommissionTest
This method has no return value and has one parameter: arg[ ] as type String.
B-2-01) INSTANTIATE object application of class SalesCommission
B-2-02) CALL application’s countRanges method


Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2.  
  3. public class SalesCommission
  4. {
  5.  
  6. // begining of countRanges method
  7. public static void countRanges()
  8. {
  9.  
  10. // creates scanner to obtain input from command window
  11. Scanner input = new Scanner(System.in);
  12.  
  13.  
  14. final double CommssonRateOnGrossSales = 0.09;
  15. final double baseWeeklySalary = 200.0;
  16. final double weeklyPayRangeIncrement = 100.0;
  17. double salesAmount;
  18. double weeklyPay;
  19. final int weeklyPayRanges = 9;
  20. int weeklyPayRange;
  21.  
  22. // define constant of type String named salesAmountPrompt
  23. final String salesAmountPrompt = ("Enter salesperson %2d's weekly sales, or <ctrl>z to stop: ");
  24.  
  25. int salespersonNumber = 1;
  26.  
  27. //INSTANTIATE a local array variable with weeklyPayRanges elements of type int named countOfWeeklyPayByRange
  28. final int countOfWeeklyPayByRange[]; countOfWeeklyPayByRange = new int [9];
  29.  
  30. // displays the task id and programmer
  31. System.out.println("\nTask 10-02, Ch07, Programmed by Michael Statham\n");
  32.  
  33. //Displays the Weekly Sales and Weekly Pay by Salesperson line
  34. System.out.println("Weekly Sales and Weekly Pay by Salesperson:");
  35.  
  36. //Initialize each element of the countOfWeeklyPayByRange array to zero:
  37. int countOfWeeklyPayByRanges[] = {0, /* $200-$299 */
  38. 0, /* $300-$399 */
  39. 0, /* $400-$499 */
  40. 0, /* $500-$599 */
  41. 0, /* $600-$699 */
  42. 0, /* $700-$799 */
  43. 0, /* $800-$899 */
  44. 0, /* $900-$999 */
  45. 0}; /* $1000 and over */
  46.  
  47.  
  48. // FOR weeklyPayRange FROM zero TO (countOfWeeklyPayByRange.length – 1) BY 1
  49. //ASSIGN zero TO countOfWeeklyPayByRange [ weeklyPayRange ]
  50. for (weeklyPayRange = 0; weeklyPayRange > countOfWeeklyPayByRange.length -1;
  51. countOfWeeklyPayByRange[(int) weeklyPayRange] = 0);
  52. //END FOR
  53.  
  54. //Using method printf, DISPLAY the following prompt
  55. System.out.printf ("\t "+
  56. salesAmountPrompt, salespersonNumber);
  57.  
  58. while (input.hasNext());
  59.  
  60. //ASSIGN input.nextDouble() TO salesAmount
  61. salesAmount = input.nextDouble();
  62.  
  63. //ASSIGN baseWeeklySalary + salesAmount multiplied by commissionRateOnGrossSale to weeklyPays
  64. weeklyPay = (baseWeeklySalary + salesAmount * CommssonRateOnGrossSales);
  65.  
  66. //ASSIGN int weeklyPay / weeklyPayRangeIncrement to weeklyPayRange
  67. weeklyPayRange = (int) (weeklyPay/weeklyPayRange++);
  68.  
  69. if (weeklyPayRange > (countOfWeeklyPayByRange.length - 1));
  70. weeklyPayRange = (countOfWeeklyPayByRange.length -1);
  71.  
  72. //End if
  73.  
  74. //INCREMENT countOfWeeklyPayByRange [ weeklyPayRange - 2 ]
  75. ++countOfWeeklyPayByRange[(int) (weeklyPayRange - 2)];
  76.  
  77. //Using method printf, DISPLAY the following prompt:
  78. System.out.printf("\t Weekly Pay for Salesperson %2d is $%,8.2f; " + salesAmountPrompt,
  79. salespersonNumber, weeklyPay, ++salespersonNumber);
  80.  
  81. //End while
  82.  
  83. //DISPLAY the "Salesperson Count by Weekly Pay Range:" line
  84. System.out.println("Salesperson Count by Weekly Pay Range:");
  85.  
  86. //Using method printf, DISPLAY the following heading line; Weekly Pay, Salesperson
  87. System.out.printf("\n\t%14s\t\t%11s", " Weekly Pay ", "Salesperson");
  88.  
  89. //Using method printf, DISPLAY the following heading line; Range, Count
  90. System.out.printf ("\n\t%14s\t\t%11s\n", " Range ", " Count ");
  91.  
  92. //FOR weeklyPayRange FROM zero TO (countOfWeeklyPayByRange.length - 2) BY 1
  93. for (weeklyPayRange = 0; weeklyPayRange < countOfWeeklyPayByRange.length - 2;
  94.  
  95. //Using method printf, DISPLAY the following line: int baseWeeklySalary + weeklyPayRangeIncrement
  96. //* weeklyPayRange, int 99.0 + baseWeeklySalary + weeklyPayRangeIncrement * weeklyPayRange,
  97. //countOfWeeklyPayByRange weeklyPayRange
  98. System.out.printf ("\n\t$%,5d?-?$%,5d\t\t%,6d", baseWeeklySalary +
  99. (++weeklyPayRange * weeklyPayRange), + baseWeeklySalary +
  100. (weeklyPayRangeIncrement * weeklyPayRange),(countOfWeeklyPayByRange[(int) weeklyPayRange])));
  101.  
  102. //End for
  103.  
  104. //Using method printf, DISPLAY the following line; 1,000 and over
  105. //count Of Weekly Pay By Range, (countOfWeeklyPayByRange.length - 1) ] )
  106. System.out.printf ("\n\t$1,000 and over\t\t%,6d", weeklyPay,
  107. (countOfWeeklyPayByRange.length -1));
  108.  
  109. //DISPLAY a blank line followed by the "End of program" line
  110. System.out.println("\nEnd of program");
  111.  
  112. }//end method
  113.  
  114. }//end SalesCommission

Java Syntax (Toggle Plain Text)
  1. //SalesCommissionTest class file name
  2. public class SalesCommissionTest
  3. {
  4. // main method begins program execution
  5. public static void main( String args[] )
  6. {
  7.  
  8. // instantiate's object application of class SalesCommission
  9. SalesCommission mySalesCommission = new SalesCommission();
  10.  
  11. // calls the application onpenForBusiness method
  12. mySalesCommission.countRanges();
  13.  
  14. }//end method main
  15.  
  16. }//end class SalesCommissionTest
Output
Exhibit C: Example Program Input and Output
Task blah blah
weekly sales and weekly pay by salesperson:
enter salesperson 1's weekly sales, or <ctrl>z to stop: 1000
weekly pay for salesperson 1 is $ 290.00;
enter salesperson 2's weekly sales, or <ctrl>z to stop: 2000
weekly pay for salesperson 2 is $ 380.00;
enter salesperson 3's weekly sales, or <ctrl>z to stop: 3000
weekly pay for salesperson 3is $ 470.00;
enter salesperson 4's weekly sales, or <ctrl>z to stop: 4000
weekly pay for salesperson 4 is $ 560.00;
enter salesperson 5's weekly sales, or <ctrl>z to stop: 5000
weekly pay for salesperson 5 is $ 650.00;
enter salesperson 6's weekly sales, or <ctrl>z to stop: 6000
weekly pay for salesperson 6 is $ 740.00;
enter salesperson 7's weekly sales, or <ctrl>z to stop: 7000
weekly pay for salesperson 7 is $ 830.00;
enter salesperson 8's weekly sales, or <ctrl>z to stop: 8000
weekly pay for salesperson 8 is $ 920.00;
enter salesperson 9's weekly sales, or <ctrl>z to stop: 9000
weekly pay for salesperson 9 is $ 1,010.00;
enter salesperson 10's weekly sales, or <ctrl>z to stop: ^Z

salesperson count by weekly pay range:
weekly pay salesperson
range count

$ 200 - $ 299 1
$ 300 - $ 399 1
$ 400 - $ 499 1
$ 500 - $ 599 1
$ 600 - $ 699 1
$ 700 - $ 799 1
$ 800 - $ 899 1
$ 900 - $ 999 1
$1000 and over 1

end program
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iwlu is offline Offline
11 posts
since Sep 2006
Oct 29th, 2006
0

Re: I think Loop problem

I did some adjustment to your code(red colouring plus removed some tab characters and extra spaces) but you have to still finish it.
Now it is working trough loop, but then when you press ctrl+z it prints labels but can't get over your calculations. It will be much faster for you to check the problem then for me have go on code full of weeklyPay, weeklyPayRange etc :lol:
import java.util.Scanner;
 
public class SalesCommission
{
 
 	// begining of countRanges method
 	public static void countRanges() 
 	{
 
  		// creates scanner to obtain input from command window
  		Scanner input = new Scanner(System.in); 
 
	 
 		final double CommssonRateOnGrossSales = 0.09;
 		final double baseWeeklySalary = 200.0;
    	final double weeklyPayRangeIncrement = 100.0;
    	double salesAmount;
    	double weeklyPay;
   		final int weeklyPayRanges = 9;
    	int weeklyPayRange;
 
   		// define constant of type String named salesAmountPrompt
   		final String salesAmountPrompt = ("Enter salesperson %2d's weekly sales, or <ctrl>z to stop: ");
 
   		int salespersonNumber = 1;
 
   		//INSTANTIATE a local array variable with weeklyPayRanges  elements of type int named countOfWeeklyPayByRange 
  		final int countOfWeeklyPayByRange[]; countOfWeeklyPayByRange = new int [9];
 
  		// displays the task id and programmer
   		System.out.println("\nTask 10-02, Ch07, Programmed by Michael Statham\n");
    
   		//Displays the Weekly Sales and Weekly Pay by Salesperson line
   		System.out.println("Weekly Sales and Weekly Pay by Salesperson:\n");
 
   		//Initialize each element of the countOfWeeklyPayByRange array to zero:
   		int countOfWeeklyPayByRanges[] = {0, /* $200-$299 */
                                     0, /* $300-$399 */
                                     0, /* $400-$499 */
                                     0, /* $500-$599 */
                                     0, /* $600-$699 */
                                     0, /* $700-$799 */
                                     0, /* $800-$899 */
                                     0, /* $900-$999 */
                                     0}; /* $1000 and over */
 
 
   		// FOR weeklyPayRange FROM zero TO (countOfWeeklyPayByRange.length – 1) BY 1
   		//ASSIGN zero TO countOfWeeklyPayByRange [ weeklyPayRange ]
   		for (weeklyPayRange = 0; weeklyPayRange > countOfWeeklyPayByRange.length -1; 
   		countOfWeeklyPayByRange[(int) weeklyPayRange] = 0); 
   		//END FOR
   
   		//Using method printf, DISPLAY the following prompt
   		System.out.printf (salesAmountPrompt, salespersonNumber);
 
   		while (input.hasNext())
 		{
   		//ASSIGN input.nextDouble() TO salesAmount
   		salesAmount = input.nextDouble();
 
   		//ASSIGN baseWeeklySalary + salesAmount multiplied by commissionRateOnGrossSale to weeklyPays 
   		weeklyPay = (baseWeeklySalary + salesAmount * CommssonRateOnGrossSales);
 
   		//ASSIGN int weeklyPay / weeklyPayRangeIncrement to weeklyPayRange
   		weeklyPayRange = (int) (weeklyPay/weeklyPayRange++);
  		 
   		if (weeklyPayRange > (countOfWeeklyPayByRange.length - 1));  
   		weeklyPayRange = (countOfWeeklyPayByRange.length -1);
 
   		//End if
 
   		//INCREMENT countOfWeeklyPayByRange [ weeklyPayRange - 2 ]
   		++countOfWeeklyPayByRange[(int) (weeklyPayRange - 2)];	
 
   		//Using method printf, DISPLAY the following prompt:
   		System.out.printf("\t Weekly Pay for Salesperson %2d is $%,8.2f; \n" + salesAmountPrompt, 
   		salespersonNumber, weeklyPay, ++salespersonNumber);
 		}
   		//End while
 
   		//DISPLAY the "Salesperson Count by Weekly Pay Range:" line
   		System.out.println("Salesperson Count by Weekly Pay Range:");
 
   		//Using method printf, DISPLAY the following heading line; Weekly Pay, Salesperson
   		System.out.printf("\n\t%14s\t\t%11s", "  Weekly Pay  ", "Salesperson");
 
   		//Using method printf, DISPLAY the following heading line; Range, Count
   		System.out.printf ("\n\t%14s\t\t%11s\n", "    Range    ", "   Count   ");
 
   		//FOR weeklyPayRange FROM zero TO (countOfWeeklyPayByRange.length - 2) BY 1
   		for (weeklyPayRange = 0; weeklyPayRange < countOfWeeklyPayByRange.length - 2; weeklyPayRange++)
 		{
   		//Using method printf, DISPLAY the following line: int baseWeeklySalary + weeklyPayRangeIncrement
   		//* weeklyPayRange, int 99.0 + baseWeeklySalary + weeklyPayRangeIncrement * weeklyPayRange,
   		//countOfWeeklyPayByRange weeklyPayRange
   		System.out.printf ("\n\t$%,5d?-?$%,5d\t\t%,6d", baseWeeklySalary + 
   		(++weeklyPayRange * weeklyPayRange), + baseWeeklySalary + 
 		(weeklyPayRangeIncrement * weeklyPayRange),(countOfWeeklyPayByRange[(int) weeklyPayRange]));
 		}
   		 //End for
 
   		//Using method printf, DISPLAY the following line; 1,000 and over
   		//count Of Weekly Pay By Range, (countOfWeeklyPayByRange.length - 1) ] )
   		//System.out.printf ("\n\t$1,000 and over\t\t%,6d", weeklyPay, (countOfWeeklyPayByRange.length -1));
 
   		//DISPLAY a blank line followed by the "End of program" line
   		System.out.println("\nEnd of program");
 
   }//end method
 
}//end SalesCommission

Good work, nice commenting (i'm in final year at uni and don't do so much ) and good luck in next progress plus don't forget bracklets in for/while/while do loops or if loops if they have to do more then one process-line of code
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Oct 29th, 2006
1

Re: I think Loop problem

Yes I see some of the errors in you showed in red, ty and I see the problem lies here:
 
//Initialize each element of the countOfWeeklyPayByRange array to zero:
int countOfWeeklyPayByRange[] = {0, /* $200-$299 */
0, /* $300-$399 */
0, /* $400-$499 */
0, /* $500-$599 */
0, /* $600-$699 */
0, /* $700-$799 */
0, /* $800-$899 */
0, /* $900-$999 */
0}; /* $1000 and over */
because i get duplicate local variable of
countOfWeeklyPayByRange
but I'm still working on it, and good luck in your studies as well.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iwlu is offline Offline
11 posts
since Sep 2006
Oct 29th, 2006
0

Re: I think Loop problem

Thanx dude

if you have problem drop a message, also for future try to use litle more meaningful names or you will pay for it once
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help Java novice
Next Thread in Java Forum Timeline: triangles program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC