• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

For loop instead of while loop

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to modify this program to use a for loop instead of a while loop, and have the user enter the commission sought instead
of it being declared as a constant. Help, please!!!
package Chapter2;
public class FindSalesAmount
{
// The commission sought
final static double COMMISSIONSOUGHT = 25000;
// Main method
public static void main(String[] args)
{
double commission = 0;
double salesAmount = 1;
while (commission < COMMISSIONSOUGHT)<br /> {<br /> // Compute commission<br /> if (salesAmount >= 10001)
commission = 5000*0.08 + 5000*0.1 + (salesAmount-10000)*0.12;
else if (salesAmount >= 5001)
commission = 5000*0.08 + (salesAmount-5000)*0.10;
else
commission = salesAmount*0.08;
salesAmount++;
}
// Display the sales amount
System.out.println("The sales amount " + salesAmount +
" is needed to make a commission of $" + COMMISSIONSOUGHT);
}
}
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this:
package Chapter2;
public class FindSalesAmount
{
// The commission sought
final static double COMMISSIONSOUGHT = 25000;
// Main method
public static void main(String[] args)
{
double commission = 0;
double salesAmount = 1;
for ( ;
{
// Compute commission
if (salesAmount >= 10001)
commission = 5000*0.08 + 5000*0.1 + (salesAmount-10000)*0.12;
else if (salesAmount >= 5001)
commission = 5000*0.08 + (salesAmount-5000)*0.10;
else
commission = salesAmount*0.08;
salesAmount++;
if ( commision < COMMISSIONSOUGHT ) break; // Cheap but will work
}
// Display the sales amount
System.out.println("The sales amount " + salesAmount +
" is needed to make a commission of $" + COMMISSIONSOUGHT);
}
}
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LOL i place 2 semicolumns in a loop initilizer and it gave a smily face just place this in your loop
[code
for ( ;; )
[/code]
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Smells like a homework assignment to me...
BTW, Val, you can always select the "Disable Smilies in This Post" checkbox at the bottom of the reply window.
~Ryan
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2nd requirement: "and have the user enter the commission sought instead of it being declared as a constant" can be accomplish with:
double wishfulThinking = Double.parseDouble(args[0]);
I'm assuming you'll pass the commission sought as the first parameter into your program.
-Peter
 
MaryEllen Volb
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ryan,
Yes, it is homework...this is very new to me)programming that is). If you knew it was homework, why didn't you help:-)!!!

Originally posted by ryan burgdorfer:
Smells like a homework assignment to me...
BTW, Val, you can always select the "Disable Smilies in This Post" checkbox at the bottom of the reply window.
~Ryan


 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hehe
Sorry for not posting something more helpful. But it looks like it is pretty much covered for now.
~Ryan
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thoery is that you are supposed to do your OWN homework so that you learn from it. Then if you have a specific question (rather that just "do it for me") ask it in the forum.
However, there are times when a person just doesn't even know how to begin. In that case you can ask for guidance or suggestions on the approach and folks will try to help without actually completing the whole thing for you.
We want to help. We just don't want to help you "trick" an instructor into giving you a higher grade than you deserve.
 
MaryEllen Volb
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I'm a little offended that it would be implied that I want ANYONE to DO my work for me, when you know absolutely nothing about me. I am a beginner as far as Java is concerned, by am an honors student who NEVER had to CHEAT or TRICK anyone into a higher grade than I deserved. I just wanted a suggestion, not a lecture.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I didn't mean to lecture you (sorry if you took it that way), I was trying to explain why Ryan may have chosen not to respond to the question when he thought it was homework.
I don't think there is any problem asking for help on homework. I remember when I would look at a snippet of java and it would look like greek. Not a clue where to start.
Actually just the fact that you are attempting to learn java puts you the top % of folks in the world.
 
MaryEllen Volb
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Cindy.....I needed the boost! I am learning Javascript and data structures at the same time as Java, so you can imagine I have a boggled brain........and I always need help:-)!!! so glad you folks are here!
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason that I moderate here is that it propmts me to keep learning new stuff. There are SOOOO many related fields, each with it's own benefits - how can any one person absorb it all! So I just keep reading and learning.
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Cindy wholeheartedly. I'm learning more from helping people with their questions than from reading a book on my own. These forums have helped me figure out what my weaknesses are and have forced me to really think about how I can answer these questions in the clearest way possible. I really admire authors who can write clearly.
-Peter
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic