Difference between revisions of "Cycling Speed"
(edited source code at the end of page) |
(edited solution headings in same order as source code solution) |
||
Line 73: | Line 73: | ||
|Solution= | |Solution= | ||
− | + | To program our solution, we will go through 4 steps: | |
+ | <ol> | ||
+ | <li>Identify and declare all variables and constants | ||
+ | <li>Store user input on bicycle information | ||
+ | <li>Perform the calculations | ||
+ | <li>Output the results | ||
+ | </ol> | ||
+ | In addition, we will be sure to comment through out our code, and follow correct programming standards as stated in [http://courses.cs.umanitoba.ca/Documents/104913/1010standards.pdf] | ||
− | == | + | ==Identify and declare all variables and constants== |
First, organized code is vital to the readability of the code. The [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 Coding Standards] give the foundation required to complete the code organization task for this [[Case_Study_I|case study]]. Each of the headings below describe a specific task that helps organize the code. | First, organized code is vital to the readability of the code. The [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 Coding Standards] give the foundation required to complete the code organization task for this [[Case_Study_I|case study]]. Each of the headings below describe a specific task that helps organize the code. | ||
− | === | + | ===Store user input on bicycle information=== |
The code file contains more than one [[Your First Java Program#Statements|statements]] on each line. | The code file contains more than one [[Your First Java Program#Statements|statements]] on each line. | ||
<pre> | <pre> | ||
Line 96: | Line 103: | ||
By placing each of the [[Your First Java Program#Statements|statements]] on a separate line, the readability of the code increases dramatically. | By placing each of the [[Your First Java Program#Statements|statements]] on a separate line, the readability of the code increases dramatically. | ||
− | === | + | ===Perform the calculations=== |
Once the [[Your First Java Program#Statements|statements]] are readable, the next step would be to organize them into code blocks as stated in [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 Coding Standards]. Continuing with our previous example, the functionality of the code can be broken into two distinct code blocks. | Once the [[Your First Java Program#Statements|statements]] are readable, the next step would be to organize them into code blocks as stated in [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 Coding Standards]. Continuing with our previous example, the functionality of the code can be broken into two distinct code blocks. | ||
<pre> | <pre> | ||
Line 123: | Line 130: | ||
</pre> | </pre> | ||
− | === | + | ===Perform the calculations=== |
The [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 coding standards], or the Funky Books Inc. coding standards, make numerous points concerning [[Comments|comments]] in code. To be specific, statements 1, 2, 4, and 7 can be applied to the code file for this [[Case Study I|case study]]. All major code blocks should be identified by now. Look over each code block and briefly explain what it does in a comment. Since all [[Variables and Literals#Variables|declaration statements]] have been moved to the top of the [[Your First Java Program#The Main Method|the main method]] , make sure to apply coding standard 7 from the [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 Coding Standards]. | The [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 coding standards], or the Funky Books Inc. coding standards, make numerous points concerning [[Comments|comments]] in code. To be specific, statements 1, 2, 4, and 7 can be applied to the code file for this [[Case Study I|case study]]. All major code blocks should be identified by now. Look over each code block and briefly explain what it does in a comment. Since all [[Variables and Literals#Variables|declaration statements]] have been moved to the top of the [[Your First Java Program#The Main Method|the main method]] , make sure to apply coding standard 7 from the [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 Coding Standards]. | ||
− | == | + | ==Output the results== |
At the top of [[Your First Java Program#The Main Method|the main method]], there should now be a number of [[Variables and Literals#Variables|variables]] | At the top of [[Your First Java Program#The Main Method|the main method]], there should now be a number of [[Variables and Literals#Variables|variables]] | ||
declared. Notice that there are nine different "digit" [[Variables and Literals#Variables|variables]] which are only used once to store the same calculation. | declared. Notice that there are nine different "digit" [[Variables and Literals#Variables|variables]] which are only used once to store the same calculation. | ||
Line 155: | Line 162: | ||
</pre> | </pre> | ||
− | == | + | ==Bonus Section: Outputting the Current Date== |
The [[Case Study I#Code|messy code]] contained a total of eight different coding errors. Each error presented in this section is ordered as it appears in the [[Case Study I#Code|code]] from the [[Case Study I|case study]]. | The [[Case Study I#Code|messy code]] contained a total of eight different coding errors. Each error presented in this section is ordered as it appears in the [[Case Study I#Code|code]] from the [[Case Study I|case study]]. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | == | + | ==Commenting and Testing== |
<pre> | <pre> | ||
int isbn = Temp; | int isbn = Temp; | ||
Line 174: | Line 173: | ||
isbn = Integer.parseInt(temp); | isbn = Integer.parseInt(temp); | ||
</pre> | </pre> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|SolutionCode= | |SolutionCode= |
Revision as of 08:34, 31 March 2011
Back to the Case Studies homepage
ProblemYou are an employer on the software team of Bikers Forever, a bicycle company. The software team uses Java and the knowledge of the Java SDK to write programs for Biker Forever. You are a great COMP 1010 student so you already have an idea of writing in Java, so in breathless anticipation, you await your next assignment from the head of the software team :) You arrive at your desk on Monday morning and open your email in-box (as usual) to see this message from the head:
Hi <insert name here>,
Program Description
Formula
Output
You have entered 75 RPM and 20 rear teeth. Meters of development: 4.948008429403924 Speed [km/hour]: 22.266037932317655 Programmed by Bicycle Forever Date: September 18, 2008 *** End of Processing *** Thanks!, <insert name here>, I am confident you will do a great job. |
Cycling Speed | |
---|---|---|
SolutionTo program our solution, we will go through 4 steps:
In addition, we will be sure to comment through out our code, and follow correct programming standards as stated in [1]
Identify and declare all variables and constantsFirst, organized code is vital to the readability of the code. The COMP 1010 Coding Standards give the foundation required to complete the code organization task for this case study. Each of the headings below describe a specific task that helps organize the code. Store user input on bicycle informationThe code file contains more than one statements on each line. int digit1 = (isbn % 10);int total = digit1 * 9;isbn = isbn / 10; int digit2 = (isbn % 10);total = total + digit2 * 8;isbn = isbn / 10; Each of the above lines of code performs a similar function. Each line can be interpretted as a block of code. By placing each statements on a separate line and grouping the statements into appropriate code blocks, the result should look something like the following: int digit1 = (isbn % 10); int total = digit1 * 9; isbn = isbn / 10; int digit2 = (isbn % 10); total = total + digit2 * 8; isbn = isbn / 10; By placing each of the statements on a separate line, the readability of the code increases dramatically. Perform the calculationsOnce the statements are readable, the next step would be to organize them into code blocks as stated in COMP 1010 Coding Standards. Continuing with our previous example, the functionality of the code can be broken into two distinct code blocks. int digit1 = (isbn % 10); int total = digit1 * 9; isbn = isbn / 10; int digit2 = (isbn % 10); total = total + digit2 * 8; isbn = isbn / 10; Almost each line of code in the messy code file can be considered a separate code block. Take the time to read the code and understand how everything works together before deciding which statements should be grouped together. One of the code blocks that should be added is a variable declaration code block at the beginning of the main method. Throughout the messy code, integers are declared. All of the declaration statements should be placed at the beginning of the main method to ensure the code stays organized. Going to the previous example, two declaration statements can be moved to the top as depicted below. int digit1; int digit2; digit1 = (isbn % 10); int total = digit1 * 9; isbn = isbn / 10; digit2 = (isbn % 10); total = total + digit2 * 8; isbn = isbn / 10; Perform the calculationsThe COMP 1010 coding standards, or the Funky Books Inc. coding standards, make numerous points concerning comments in code. To be specific, statements 1, 2, 4, and 7 can be applied to the code file for this case study. All major code blocks should be identified by now. Look over each code block and briefly explain what it does in a comment. Since all declaration statements have been moved to the top of the the main method , make sure to apply coding standard 7 from the COMP 1010 Coding Standards. Output the resultsAt the top of the main method, there should now be a number of variables declared. Notice that there are nine different "digit" variables which are only used once to store the same calculation. int digit1; int digit2; digit1 = (isbn % 10); int total = digit1 * 9; isbn = isbn / 10; digit2 = (isbn % 10); total = total + digit2 * 8; isbn = isbn / 10; The code sample from above shows two of the nine digit variables. These two variables can be replaced with a single one as follows: int digit; digit = (isbn % 10); int total = digit * 9; isbn = isbn / 10; digit = (isbn % 10); total = total + digit * 8; isbn = isbn / 10; Bonus Section: Outputting the Current DateThe messy code contained a total of eight different coding errors. Each error presented in this section is ordered as it appears in the code from the case study. Commenting and Testingint isbn = Temp; There are two problems with the above code sample. The first problem being that the variable "Temp" is of type string and not of the primitive type int. The second problem is the name of the variable "Temp". The variable was originally declared as "temp" and Java is a case sensitive language. Both programs are repaired by replacing the code with the line below. isbn = Integer.parseInt(temp); | ||
Code |