Difference between revisions of "Cycling Speed"
Line 137: | Line 137: | ||
==Store user input on bicycle information== | ==Store user input on bicycle information== | ||
− | In order to retrieve user input, we will be using the class JOptionPane. We are able to call this class and its methods when we type in "import javax.swing.*;" at the top of our program. Future computer science courses will explain what the <b>import</ | + | In order to retrieve user input, we will be using the class JOptionPane. We are able to call this class and its methods when we type in "import javax.swing.*;" at the top of our program. Future computer science courses will explain what the <b>import</b> statement does. |
<br />How JOptionPane works: | <br />How JOptionPane works: |
Revision as of 09:35, 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 constantsBased on The Boss's email to you, and the description of the program and formula, we can easily identify all the variables and constants important to the solution.
Declaration of Constants
final int FRONT_TEETH = 45; We also know the constant for the diameter is 70cm every-time. However, the diameter is not used in the calculations. Rather, the circumference is used to calculate the meters of development.
final int double WHEEL_CIRCUMFERENCE = (70 * Math.PI)/100.0; Now that we have our constants, let go ahead to declare the rest of the variables we will need based on the formula. We will also include a string variable to store our user's input. int rpm; int rearTeeth; double mdev; //a double because it will contain decimal spaces because of the circumference double speed; String input; Store user input on bicycle informationIn order to retrieve user input, we will be using the class JOptionPane. We are able to call this class and its methods when we type in "import javax.swing.*;" at the top of our program. Future computer science courses will explain what the import statement does.
input = JOptionPane.showInputDialog ("Enter rpms."); rpm = Integer.parseInt(input); input = JOptionPane.showInputDialog ("Enter number of teeth on rear gear."); rearTeeth = Integer.parseInt(input); Now we have all the user input we need to perform the calculations. Perform the calculationsWe have the directions from The Boss, and we have all the variables and user input we require. Now we calculate!
mdev = circumference[m] * (number front teeth / number rear teeth). Translating this into Java code: mdev = WHEEL_CIRCUM * (FRONT_TEETH / (double) rearTeeth); Next we calculate the speed: speed = mdev * rpm * 60/1000 Translating this into Java code: speed = mdev * rpm; //speed in meters per minute speed = speed * (60.0 / 1000); //speed in km per hour I took the liberty of breaking the speed calculation into meters per minute and multiplying it by 60.0/1000 in order to provide the result in kilometers per hour (the normal speed scale). 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 |