Difference between revisions of "Cycling Speed"

From CompSciWiki
Jump to: navigation, search
m
Line 47: Line 47:
  
 
<pre>
 
<pre>
speed = mdev * rpm * 60/1000
+
speed = mdev * rpm * 60/1000 //Note that 60/1000 helps to calculate the speed in kilometers per hour
 
</pre>
 
</pre>
 
</ol>
 
</ol>
Line 137: Line 137:
  
 
==Store user input on bicycle information==
 
==Store user input on bicycle information==
The code file contains more than one [[Your First Java Program#Statements|statements]] on each line.
+
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</import> statement does.
 +
 
 +
<br />How JOptionPane works:
 +
JOptionPane has an InputDialog method which shows a dialog box and accepts input typed into that box. This input can then be stored in strings. What we will do in this case, is store the user input into strings and parse (convert) them to integers using Integer.parseInt.
 +
 
 +
<br />How Integer.parseInt works:
 +
Integer.parseInt accepts a string and converts it to an int. The string however must contain numbers only or bad things happen :)
 +
 
 +
<br />
 +
Below is our storage of input and conversion to actual integers.
 +
 
 
<pre>
 
<pre>
int digit1 = (isbn % 10);int total = digit1 * 9;isbn = isbn / 10;
+
input = JOptionPane.showInputDialog ("Enter rpms.");
int digit2 = (isbn % 10);total = total + digit2 * 8;isbn = isbn / 10;
+
rpm = Integer.parseInt(input);
 +
 
 +
input = JOptionPane.showInputDialog ("Enter number of teeth on rear gear.");
 +
rearTeeth = Integer.parseInt(input);
 
</pre>
 
</pre>
Each of the above lines of code performs a similar function. Each line can be interpretted as a block of code. By placing each [[Your First Java Program#Statements|statements]] on a separate line and grouping the statements into appropriate code blocks, the result should look something like the following:
+
 
<pre>
+
Now we have all the user input we need to perform the calculations.  
int digit1 = (isbn % 10);
+
int total = digit1 * 9;
+
isbn = isbn / 10;
+
int digit2 = (isbn % 10);
+
total = total + digit2 * 8;
+
isbn = isbn / 10;
+
</pre>
+
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==
 
==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.
+
We have the directions from The Boss, and we have all the variables and user input we require. Now we calculate!
<pre>
+
int digit1 = (isbn % 10);
+
int total = digit1 * 9;
+
isbn = isbn / 10;
+
  
int digit2 = (isbn % 10);
+
<br />
total = total + digit2 * 8;
+
Following instructions we calculate the meters of development first. As per the email:
isbn = isbn / 10;
+
 
</pre>
+
mdev = circumference[m] * (number front teeth / number rear teeth).
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 [[Your First Java Program#Statements|statements]] should be grouped together.
+
 
 +
Translating this into Java code:
  
One of the code blocks that should be added is a variable declaration code block at the beginning of [[Your First Java Program#The Main Method|the main method]]. Throughout the [[Case Study I#Code|messy code]], integers are declared. All of the [[Variables and Literals#Variables|declaration statements]] should be placed at the beginning of [[Your First Java Program#The Main Method|the main method]] to ensure the code stays organized. Going to the previous example, two [[Variables and Literals#Variables|declaration statements]] can be moved to the top as depicted below.
 
 
<pre>
 
<pre>
int digit1;
+
mdev = WHEEL_CIRCUM * (FRONT_TEETH / (double)  rearTeeth);
int digit2;
+
</pre>
  
digit1 = (isbn % 10);
+
Next we calculate the speed:
int total = digit1 * 9;
+
isbn = isbn / 10;
+
  
digit2 = (isbn % 10);
+
speed = mdev * rpm * 60/1000
total = total + digit2 * 8;
+
 
isbn = isbn / 10;
+
Translating this into Java code:
 +
 
 +
<pre>
 +
speed = mdev * rpm; //speed in meters per minute
 +
speed = speed * (60.0 / 1000); //speed in km per hour
 
</pre>
 
</pre>
 +
 +
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 results==
 
==Output the results==

Revision as of 09:34, 31 March 2011

Back to the Case Studies homepage

Problem

You 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>,

I need you to write a program that allows customers who visit the Bicycle Forever to calculate the speed of their bicycles. One of the members of the team stumbled on the formula to calculate cycling speed, and knowing you are just itching for an assignment, I thought of you straightaway to implement the formula. The program description and the formula is below:

Program Description

  1. The program will prompt the user to input
    • The RPM (rotations per minute) of the bicycle
    • The number of teeth in the rear gear
  2. The program will also store as constants the number of teeth in the front gear and the diameter of the bicycle wheel. Assume the number of front teeth to be 45 and the diameter to be 70cm always.

Formula

  1. Before you calculate the speed, you must first calculate the meters of development. This is the length in meters covered by each rotation of the wheel of the bicycle. Meters of development (mdev, as we'll call them) are calculated this way:
    mdev = circumference[m] * (number front teeth / number rear teeth) 
    
  2. Finally, now that we have the mdev we can calculate the speed:
    speed = mdev * rpm * 60/1000 //Note that 60/1000 helps to calculate the speed in kilometers per hour
    

Output

To display your results, assuming a customer inputs 75 for RPM and 20 for rear teeth, the output will be displayed this way:

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.
The Boss

 

Cycling Speed

Wiki start01.jpg

Solution

To program our solution, we will go through 4 steps:

  1. Identify and declare all variables and constants
  2. Store user input on bicycle information
  3. Perform the calculations
  4. Output the results

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 constants

Based 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.
NOTE: some of these variables are dependent on the others.


CONSTANTS:

  • The number of front wheel teeth - assumed to be 45 everytime
  • The diameter of the wheel - assumed to be 70cm everytime

Declaration of Constants
When declaring constants, we use the keyword final. This ensures that whether it is an int, double, string or any other data type, if it is a final int for example, it cannot be changed throughout the code.

As the number of front wheel teeth is a number, we will declare it using "final int"

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.


So what should we do???


It's simple! Going back to your high school days you know that the Circumference of a circle (wheel) = 3.14 (pi) * the diameter.

  • Pi in Java has been provided for us through Math.PI
  • We also want to make sure we divide by 100.0 because our diameter is in centimeters right now as opposed to the meters we need to calculate the meters of development (if you remember!)
  • And finally, our constant will be a "final int double" to represent the circumference. We declare and calculate the circumference right away since we already know the diameter and we use a double to accommodate the decimal spaces


Therefore our declaration becomes:

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 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 import</import> statement does.


How JOptionPane works: JOptionPane has an InputDialog method which shows a dialog box and accepts input typed into that box. This input can then be stored in strings. What we will do in this case, is store the user input into strings and parse (convert) them to integers using Integer.parseInt.


How Integer.parseInt works: Integer.parseInt accepts a string and converts it to an int. The string however must contain numbers only or bad things happen :)


Below is our storage of input and conversion to actual integers.

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 calculations

We have the directions from The Boss, and we have all the variables and user input we require. Now we calculate!


Following instructions we calculate the meters of development first. As per the email:

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 results

At 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 Date

The 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 Testing

int 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

Solution Code

Back to the Case Studies homepage