Difference between revisions of "Know Your Standards"
From CompSciWiki
Line 3: | Line 3: | ||
|Problem= | |Problem= | ||
− | Your sixteen year old brother comes to you for help in a programming assignment he has in school. He claims doesn't even understand his own code. His program is supposed to | + | Your sixteen year old brother comes to you for help in a programming assignment he has in school. He claims doesn't even understand his own code. His program is supposed to determine what the interest rate would be in a $15,000 car loan, depending on how long the user wants to take to pay it off. Here are the conditions: |
+ | <br>36 months: 7.5% | ||
+ | <br>48 months: 8% | ||
+ | <br>60 months: 9% | ||
+ | |||
+ | You glance over his work and notice how badly structured his program is. Edit the snippet of code below without the use of any editors and compilers, and explain why the edit was necessary. | ||
<pre> | <pre> | ||
+ | int answer=0; | ||
+ | |||
+ | int months = Integer.parseInt(String answer = JOptionPane.showInputDialog("How long will it take you to pay off your loan? 36, 48, or 60 months?:")); | ||
+ | if(36 == months); answer = 7.5; | ||
+ | else if(48 == months) answer = 8; | ||
+ | else if(60 == months) answer = 9; | ||
+ | </pre> | ||
|SideSectionTitle=<center>If Statements and Named Constants | |SideSectionTitle=<center>If Statements and Named Constants |
Revision as of 10:59, 8 April 2010
Back to the Program-A-Day homepage
ProblemYour sixteen year old brother comes to you for help in a programming assignment he has in school. He claims doesn't even understand his own code. His program is supposed to determine what the interest rate would be in a $15,000 car loan, depending on how long the user wants to take to pay it off. Here are the conditions:
You glance over his work and notice how badly structured his program is. Edit the snippet of code below without the use of any editors and compilers, and explain why the edit was necessary. int answer=0; int months = Integer.parseInt(String answer = JOptionPane.showInputDialog("How long will it take you to pay off your loan? 36, 48, or 60 months?:")); if(36 == months); answer = 7.5; else if(48 == months) answer = 8; else if(60 == months) answer = 9; |
| |
---|---|---|
SolutionThe solution... | ||
CodeSolutionCode goes here. Please DO NOT put your code in <pre> tags! |