Cycling Speed

From CompSciWiki
Jump to: navigation, search

Back to the Case Studies homepage

Problem

You are an employee 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 Bikers Forever. You are a great COMP 1010 student so you already know a bit about 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 of the software team:

Hi <insert name here>,

I need you to write a program that will allow 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 that 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 of the bicycle
  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 every-time.

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

CyclingHelmet casestudy.jpg

Solution

Eddie Root, famous Australian rugby player riding a bicycle

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

  1. Identify and declare all variables and constants necessary to implement the solution
  2. Store user input on bicycle information
  3. Perform the calculations for the meters of development and then the speed of the bicycle
  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 every-time
  • The diameter of the wheel - assumed to be 70cm every-time


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, it cannot be overwritten.



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 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 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 Java code showing the 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); //Remember to cast variable 
//rearTeeth as a double or you will get an int value for your result. That would be wrong! doubles/doubles = doubles :)

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

The outputs should closely resemble something like this:

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 ***

Let us do a breakdown of the output, line by line.

Line 1: "You have entered 75 RPM and 200 rear teeth" This result can be printed immediately after you have read user input and converted the input to integers. Printing out the input will also help to confirm that it was stored correctly. In this case:

System.out.println ("You have entered " + rpm + " rpms and " + rearTeeth + " rear teeth");

Line 2: "Meters of development: 4.948008429403924" This can be printed immediately after the calculation of the meters of development.

System.out.println("Meters of development: " + mdev);

Line 3: "Speed [km/hour]: 22.266037932317655" The speed also, can be printed out after its calculation has been done.

System.out.println("Speed [km/hour]: " + speed);

To complete the process we can write code to print out the rest of the output.

System.out.println("\nProgrammed by Bicycle Forever");
System.out.println("Date: ");
System.out.println ("*** End of Processing ***");

You will notice that the output for the date above is empty. You can hardcode the date in i.e. type it right into the program as part of the output, or use the Calendar class to show the current date every-time the program is run. I have included the process for showing the current date in the bonus section below.

Bonus Section: Outputting the Current Date

Just like we used the import statement in the Storing User Input section, we will also be using this statement to access the Calendar class in order to get the current date.

You can tell what import statement to use by going through the Java API documentation: [2]. The documentation also explains what classes are available and the methods attached to them.

For the purpose of this solution, we will be including import statements that will:

  • Give us access to the Calendar class and a method that provides the current time
  • Give us access to a SimpleDateFormat class and a method that helps format the current time
import java.text.SimpleDateFormat;
import java.util.Calendar;

Now that we have included the import statments, we can call the Calendar class and its method getInstance() to give us a working calendar, and then call the SimpleDateFormat class that we shall provide with a format for outputting the current time:

Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

Now that we have a working calendar, we will add the output gotten from formatting the current time of the Calendar (using the SimpleDateFormat class) into our solution output. This is the line that provides the output:

System.out.println("Date: " + dateFormat.format(calendar.getTime()));

Commenting and Testing

Although we have our calculations done and our output printed, we aren't done yet! For the sake of others, who will read your code, commenting must be done. Using the "//" characters, comment major lines to show what you are doing. This improves readability and tells The Boss that you are a professional.

In addition, you want to test your code, right after every calculation to be sure you do not provide a wrong bicycle speed. A program that outputs the wrong speed of a bicycle can make bad things happen! Therefore, check to be sure

  • Input is entered and converted correctly
  • Calculations are done correctly
  • Results are output correctly

I have included comments and tests using System.out.println() in the sample source code for your benefit.

COMPLETE!

Congratulations! You have now finished your Cycling Speed program. You can now compile and run your program. You can also click the Solution button below to see a full solution.

Code

Solution Code

Back to the Case Studies homepage