Difference between revisions of "Know Your Standards"

From CompSciWiki
Jump to: navigation, search
m (Changed to codeblocks)
 
(4 intermediate revisions by 4 users not shown)
Line 2: Line 2:
  
 
|Problem=   
 
|Problem=   
 
+
You've been hired by a bank to rewrite some financial softwareThe program is supposed to determine the interest rate on a $15,000 car loan.  The interest rate depends on the term of the loan, based on the following conditions:
Your sixteen year old brother comes to you for help in a programming assignment he has in schoolHe 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 offHere are the conditions:
+
 
<br>36 months: 7.5%  
 
<br>36 months: 7.5%  
 
<br>48 months: 8%
 
<br>48 months: 8%
 
<br>60 months: 9%
 
<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/or compilers, and explain why the edit was necessary.
+
You glance over the software and find a section of code that is badly structured.   
<pre>
+
 
 +
Edit the snippet of code below without the use of any editors and/or compilers, and explain why the edit was necessary.
 +
{{CodeBlock|Code=
 
int answer=0;
 
int answer=0;
  
int months = Integer.parseInt(String input = JOptionPane.showInputDialog("How long will it take you to pay off your loan? 36, 48, or 60 months?:"));
+
int months = Integer.parseInt(String input = JOptionPane.showInputDialog("How long will it" +
 +
  " take you to pay off your loan? 36, 48, or 60 months?:"));
 
if(36 == months); answer = 7.5;
 
if(36 == months); answer = 7.5;
 
else if(48 == months) answer = 8;
 
else if(48 == months) answer = 8;
 
else if(60 == months) answer = 9;
 
else if(60 == months) answer = 9;
</pre>
+
}}
  
 
|SideSectionTitle=<center>If Statements and Named Constants
 
|SideSectionTitle=<center>If Statements and Named Constants
Line 24: Line 26:
 
|Solution=
 
|Solution=
  
Start editing the code by declaring all variables and constants in the beginning of the program.  This is for organization purposes, and will help to understand the code better.  Providing comments on what these variables and constants contain will also be useful.  Notice that the integer holding the answer is replaced with a double instead of an integer to handle decimals.
+
Start editing the code by declaring all variables and constants in the beginning of the program.  This is for organization purposes, and will help to understand the code better.  Providing comments on what these variables and constants contain will also be useful.  Notice that the integer holding the answer is replaced with a double to handle decimals.
<pre>
+
{{CodeBlock|Code=
 
double answer=0;  //This will contain the interest rate.
 
double answer=0;  //This will contain the interest rate.
 
String input="";
 
String input="";
Line 37: Line 39:
 
final double REGULAR_RATE = 8.0;
 
final double REGULAR_RATE = 8.0;
 
final double HIGHEST_RATE = 9.0;
 
final double HIGHEST_RATE = 9.0;
</pre>
+
}}
  
Every instruction will be clearer if each were executed in one line.  It will be easier to spot any mistakes.  Idendentation is also necessary when breaking up this code to follow programming standards.  The semi colon was deleted in the original code and will generate a syntax error if compiled.  Braces are also good programming practice, in case more instructions need to be added within the condition.
+
Every instruction will be clearer if each were executed in one line.  It will be easier to spot any mistakes.  Indentation is also necessary when breaking up this code to follow programming standards.  The semi colon was deleted in the original code and will generate a syntax error if compiled.  Braces are also good programming practice, in case more instructions need to be added within the condition.
<pre>
+
{{CodeBlock|Code=
 
input = JOptionPane.showInputDialog("How long will it take you to pay off your loan? 36, 48, or 60 months?:");
 
input = JOptionPane.showInputDialog("How long will it take you to pay off your loan? 36, 48, or 60 months?:");
 
months = Integer.parseInt(input);
 
months = Integer.parseInt(input);
Line 60: Line 62:
 
   System.out.println("Error: Invalid Output");
 
   System.out.println("Error: Invalid Output");
 
}
 
}
</pre>
+
}}
  
 +
|SolutionCode=
 +
import javax.swing.JOptionPane;
 +
 +
public class Interest
 +
{
 +
 +
    public static void main(String[]args)
 +
    {
 +
 +
        double answer=0;  //This will contain the interest rate.
 +
        String input="";
 +
        int months=0;    //The String input will be converted to an int for comparison.
 +
 +
        //Constants must be declared, ensuring that all names follow the proper naming standards.
 +
        final int SHORTEST_TIME = 36;
 +
        final int AVERAGE_TIME = 48;
 +
        final int LONGEST_TIME = 60;
 +
        final double SMALLEST_RATE = 7.5;
 +
        final double REGULAR_RATE = 8.0;
 +
        final double HIGHEST_RATE = 9.0;
 +
 +
        input = JOptionPane.showInputDialog("How long will it take you to pay off your loan? 36, 48, or 60 months?:");
 +
        months = Integer.parseInt(input);
 +
 +
        if(SHORTEST_TIME == months)
 +
        {
 +
            answer = SMALLEST_RATE;
 +
        }
 +
        else if(AVERAGE_TIME == months)
 +
        {
 +
            answer = REGULAR_RATE;
 +
        }
 +
        else if(LONGEST_TIME == months)
 +
        {
 +
            answer = HIGHEST_RATE;
 +
        }
 +
        else
 +
        {
 +
          System.out.println("Error: Invalid Output");
 +
        }
 +
       
 +
        System.out.print("The insterest rate is: ");
 +
        System.out.println(answer);
 +
 +
    }
 +
 +
}
 
}}
 
}}

Latest revision as of 15:08, 4 December 2011

Back to the Program-A-Day homepage

Problem

You've been hired by a bank to rewrite some financial software. The program is supposed to determine the interest rate on a $15,000 car loan. The interest rate depends on the term of the loan, based on the following conditions:
36 months: 7.5%
48 months: 8%
60 months: 9%

You glance over the software and find a section of code that is badly structured.

Edit the snippet of code below without the use of any editors and/or compilers, and explain why the edit was necessary.

 int answer=0;

int months = Integer.parseInt(String input = 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; 
 

If Statements and Named Constants

Wiki sign01.jpg

Solution

Start editing the code by declaring all variables and constants in the beginning of the program. This is for organization purposes, and will help to understand the code better. Providing comments on what these variables and constants contain will also be useful. Notice that the integer holding the answer is replaced with a double to handle decimals.

 double answer=0;  //This will contain the interest rate.
String input="";
int months=0;     //The String input will be converted to an int for comparison.

//Constants must be declared, ensuring that all names follow the proper naming standards.
final int SHORTEST_TIME = 36;
final int AVERAGE_TIME = 48;
final int LONGEST_TIME = 60;
final double SMALLEST_RATE = 7.5;
final double REGULAR_RATE = 8.0;
final double HIGHEST_RATE = 9.0; 

Every instruction will be clearer if each were executed in one line. It will be easier to spot any mistakes. Indentation is also necessary when breaking up this code to follow programming standards. The semi colon was deleted in the original code and will generate a syntax error if compiled. Braces are also good programming practice, in case more instructions need to be added within the condition.

 input = JOptionPane.showInputDialog("How long will it take you to pay off your loan? 36, 48, or 60 months?:");
months = Integer.parseInt(input);

if(SHORTEST_TIME == months)
{
    answer = SMALLEST_RATE;
}
else if(AVERAGE_TIME == months)
{
    answer = REGULAR_RATE;
}
else if(LONGEST_TIME == months)
{
    answer = HIGHEST_RATE;
} 
else
{
   System.out.println("Error: Invalid Output");
} 

Code

Solution Code

Back to the Program-A-Day homepage