ulrail.gif

Homework J1

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

Purpose

A computer program represents a useful computation (or, to be precise, a set of possible computations) in a human-readable form. The computer, so programmed, is valuable in its ability to automate useful computations at high speed. One important class of programs are those that accept input values from the person using the computer (colloquially “the user”), that compute a useful result, and that then present the result to the user. The steps in such a computation are thus (1) get input values, (2) compute result, (3) output result.

In this homework assignment, you will write a computer program representing a simple but useful computation in this style. You can download the skeleton code here.

The Problem

A bicyclist wants to bike a certain number of miles on each given day but has to figure out how many times to go around a given loop to reach that distance. In this assignment, you will write a program to answer how many times must the biker complete the loop to reach the goal. The biker also wants to know, given a time taken per loop, how long the entire ride will take.  More specifically, you need to ask for three inputs (in the specified order): ride length, loop length, and loop time.  All of these values should be of type double. Your program needs to provide a number of outputs:

  • Number of loops needed for the ride (this should be a floating point (i.e. double) value)
  • Estimated time for the entire ride, based on the time per loop and the total distance.  This should be printed in both total number of minutes and total number of hours (both are floating point  (i.e. double) values)
  • Estimated speed of the entire ride in both miles/hour and kilometers/hour (both in floating point  (i.e. double) values)

Sample execution

To help you understand what is required for this homework, we have included an execution run of this program.  Your program needs to print out similar information, but the format does not have to be the same. However, the values computed should be the same. Note that the text in red is what was input by the user. And don't worry about really long decimal numbers (such as 2.533333333333333 ) -- we'll see how to convert this into 2.53 (or similar) later in the semester.

Welcome to the bike loop calculator!

Please enter the number of miles to bike (in miles): 25
Please enter the length of the loop you'll be riding (in miles): 4
Please enter the average time per loop (in minutes): 15
The number of 4.0-mile loops required to bike for 25.0 miles is 6.25
It should take 93.75 minutes, or 1.5625 hours
The average speed should be 16.0 mph, or 25.749504 kph

Program Design

One of the key problems that software developers face is to determine what sequences of operations are needed to solve a specified problem (such as the number-of-loops problem). In this case, we’re going to get you off to a good start by giving you a sequence of steps that work. Your primary task is to refine this program by converting each of these high-level steps into an appropriate statement or sequence of statements in the Java language. In the software development field, we call such a task step-wise refinement -- meaning that we take program steps expressed in a high-level, easy-to-understand form (such as the 6 steps below that are listed in English), and we refine them into one or more steps in a lower-level, more concrete language: in this case Java. This is an incredibly important skill for the software developer -- to be able to sketch programs at a high level before refining them into statements in a formal programming language, such as Java. The list below expresses the essence of the solution -- it's an abstract solution. Refining it into a language such as Java is relatively straightforward, once you know the language.

  1. Print an appropriate legend (see below)
  2. Define the constants that you will be using in your program.  In particular, there are 1.609344 kilometers per mile.
  3. Set up an input stream (Hint: create a new Scanner object. Only create one for your whole program!)
  4. Prompt the user for the three inputs described above, read in those input values and store them in appropriately named variables
  5. Compute the desired results.
  6. Print out the computed results with an explanatory message. The output should appear something like this: “The number of <loop_length>-mile loops required to bike for <ride_length> miles is <number_of_loops>.” And print something similar for the time taken and the rate traveled. 

Remember that if you want to print out multiple things with a System.out.println() statement, you need to separate them with a + (a plus sign). You are welcome to print out the values as they are computed, if you would like.  You should be able to determine the formulas needed to solve the problem -- see a TA for help if you are unsure. When asking for input, please only ask for the required 3 values in the specified order (ride length, loop length, loop time). We will test your program automatically, so if you ask for more input values, your program will not execute correctly for the graders.

While we have provided most of the design for this program above, you need to supply some details -- the formula for computing the output values, the variables needed to store inputs and the result, and how to express all of these ideas in the Java programming language.

How and where to start

We have provided a skeleton BikeLoops.java file for you to start with. Please don't change the class name -- doing so will cause it not to compile correctly and result in a loss of lots of points.

The best way to proceed is to go through the seven steps described above, testing the program after each step or two. This process is called incremental development. If you take a small step and it doesn’t work, it’s easy to step back and do it again. If you take lots of steps and end up with something that doesn’t work, you can easily get completely lost. Take small steps then test before proceeding! For example, after you write the code to read in the input from the user, you should print those values out to the screen, so that you can test your input routines. And after you make some partial calculations, print those out to the screen to test the program. You are welcome to try to write the whole program and debug it all at once – however, this will very likely take you more time than developing and testing it incrementally. Of course, if you have lots of print statements in there, you should probably remove them when the program is working properly, so that the output looks similar to what is shown below. Lastly, be sure you fully understand how to do the conversions before you try to program that in Java.

Lastly, you may want to look at the BMICalculator.java program (also on page 75 of the textbook and discussed in lecture). This program, while it computes something different, uses a lot of the same steps described above.

Good programming practices

The following are considered good programming practices. All homeworks must include these whenever possible. Not including them will receive points off from the grade. The BMICalculator.java program (on page 75 of the textbook) is an example of a program that follows most of these practices (all but the test code part).

  • Header: The comments at the very beginning of the file, and need to include your name, e-mail ID, 101 section (or 101-E, if appropriate), and a line or two about what this program does.
  • Comments: A line or two before each major "block" (section of code, class, method, variable, etc.) describing what it does.
  • Whitespace: A line or two between separate elements of the code (methods, code segments, etc.) and good indentation.
  • Legend: The program should print out a line or two that states what the purpose of the program is (i.e. "This is a BMI calculator", or "Are you ready for an Ironman Triathlon? Let's find out!"). This should be the first line(s) printed to the screen.
  • Variable names: All final variables should be in all caps with underscores between the words, such as BOX_WIDTH. Non-final variable names should have the firstLetterOfEachWordCaptilized in the variable name (excepting the very first letter). Variable names should be relevant and informative, such as mileage and not m.
  • Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not go past the end of the screen. You can have a println() statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator tells you the column that the cursor is currently in (at the bottom on the status bar), so you can use that to gauge how long your lines are.
  • Test code: ALL programs must include proof that you tested the program. For this program, the test is just required as comments -- enter what values you used to test your program, and what the output is. You do not need to copy-and-paste the output run. When we test your program, we will only use positive values (i.e., greater than zero) for the input values. You only need to test with these types of values as well.

Grading

The following lists the criteria that the graders will be look at when the homework is graded. Note that these criteria will not be repeated on the other homeworks, only on this first one. Also, the points for each of the following criteria have not yet been determined.

  • Program header (includes name, e-mail ID, and purpose)
  • Good commenting and use of whitespace
  • Prints out an appropriate legend
  • Proper use of Scanner for the input stream
  • Properly prompts user for input
  • Properly computes the necessary values
  • Prints out the necessary values
  • Variable declaration (good names, proper initialization, etc.)
  • Test cases shown in comments

If your program does not compile, you will receive 25 points off, in addition to any other penalties. If you do not understand a compilation error, see a TA or professor (thus, you probably don't want to wait until the night before to start this homework). We have lots of office hours available, please make use of them!

Submission

When you are finished, submit the BikeLoops.java file. You will notice that we ask more survey questions for the homeworks.

 

spacer.gif
spacer.gif footer-middle.gif spacer.gif