Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Program Output not working

mike hew
Greenhorn
Posts: 27
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.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-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

Output
Exhibit C: Example Program Input and Output
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

fred rosenberger
lowercase baba
Bartender
Posts: 12231
36
there is a lot of stuff here for someone to wade through. Few, if any, are going to have the time and go through this line by line, to try and figure out what the problem is. Heck, i'm not even sure what your QUESTION is.

My advice would be to ask very specific, focused questions. only post the code relevant to that part. Say what it IS doing, and what you think it SHOULD be doing. Tell us what you've tried, and how that worked differently than what you expected, if you don't understand why it didn't work.

otherwise, you're probably never going to get an answer to your query.

mike hew
Greenhorn
Posts: 27
I get only the following to start on the out put then it hangs, will not continue on, I'm not sure how to debug the program or how to go about doing that either.

weekly sales and weekly pay by salesperson:
enter salesperson 1's weekly sales, or <ctrl>z to stop:
you enter the 1000 and hit enter it's just a blinking line.

I've try'd removing this or that to see what might be the problem but can't find my error, even though their is no errors in the program.

Is there a way to debug java programs?

mike hew
Greenhorn
Posts: 27
Was wondering if this isn't the problem

// 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

Garrett Rowe
Ranch Hand
Posts: 1296
That for loop is definitly a problem. Take a closer look at it. Will it ever execute? Even if you do get it to execute in its current incarnation (you most likely won't), is the weeklyPayRange variable ever changed?

About debugging Java programs, that is likely to be where most of your time is spent. You can help yourself with this process tremendously if you make an attempt to write short methods that only do one thing. Then you can debug methods in isolation and have a much better chance of tracking down the bugs. As it stands now, your countRanges() method has the responsibilty of
• Getting input from the user
• Calculating the weekly pay
• Maintaining some other array of weeklyPayCounts
• Displaying the results.
• It probably does even more stuff but I can't figure it out because I got lost halfway through. If I were you I would consider breaking this up into smaller methods that only do one thing. That will make your program easier to read and understand, and a lot easier to debug.

Does that make sense at all?
[ October 26, 2006: Message edited by: Garrett Rowe ]

fred rosenberger
lowercase baba
Bartender
Posts: 12231
36
if you're just learning to program, or just learning java, your best debugging tools are simple System.out.prinln statements. sprinkle them liberally through your program, with things like

System.out.println("got here 1");
System.out.println("got here 2");

etc. throw in some variables to see what your counters are doing...

System.out.println("got here 1, xyx is " + xyx);

thoroughly test every few new lines of code as you write them. don't write 200 lines before you test. that's BAD. some folks advocate compiling/testing/debugging after EVERY new line of code.

the smaller hunk you write/test, the smaller place you have to look to find what went wrong.

if you think you have an inf. loop, certainly put a print statement inside it, and print what the conditional value is. see if it's changing, and changing in the right direction.

mike hew
Greenhorn
Posts: 27
It makes perfectly sense, except for 1 thing!

I have to follow that stupid pseudo code verbatim or the instructor takes off points, go figure, short of offing the instructor I have recourse but todo it his stupid way!!!

Garrett Rowe
Ranch Hand
Posts: 1296
Ahhh.... I see. I guess I thought he pseudocode was something you had written to outline the steps you needed for this particular program. Well, since youe aren't able to change the overall design of the methods, you'll have to do what your instructor asks of you in the manner he wants it (one of the perils of being a student). Like I said, your for loop has some problems, remember the for loop syntax:

And heed fred's advice about liberally sprinkling your code with System.out.println() statements so you can see whats going on. You can always remove them when you have your program working the way you want it.

mike hew
Greenhorn
Posts: 27
Ok thank you both for your inputs!!