L02

Due by the end of Tuesday, September 19

Starter Files:


SUBMIT / CHECK

Today's lab has a pretty long description. It's telling you lots of stuff that you need to know, and that you'll use on every lab and assignment hereafter. Please read it carefully. We won't be going into so much detail on later labs and assignments, but you'll still be expected to do all the same sorts of things.


Today's Activities

Activity 1: Creating folders
You should have created a folder for CSCI1226 in L01. If you haven't, then now is the time to do it. You will not be marked based on how well you organize your files, but you are going to create a lot of files for this course, and it'll be very helpful for you if you keep them organized.
Activity 2: Downloading Netbeans
To edit and execute Java code in CSCI1226 you should use an IDE (Integrated Development Environment). We recommend using Netbeans.

If you are using a lab computer you need to start up AppsAnywhere and open up Netbeans. You will also need to have a thumb drive to save your files on.

Students may also use AppsAnywhere on their home computers to run Netbeans.

For those of you going on in CS, we recommend you actually install Netbeans on your computer. Other students might like to do so as well. For those students, start by downloading JDK 17 (see the link in the Reference section of the navigation bar). After it is downloaded and installed, you can download download any version of Netbeans after v8.0.
Refer to the Lecture Slides for weeks 1 and 2, and to the "How to Use NetBeans" link in the "Reference" section of the navigation pane, for information on using Netbeans.
Activity 3: Creating a Program
At this point you should refer to the section of the course website titled "How to Use NetBeans" in the "Reference" section.

You might want to open it in a new window so you can switch back-and-forth between it and this lab description.
Pay particular attention to the sections "Creating a Program", "Downloading Code for a Lab" and "Running a Program".

You must use the following names:

You should put your project in the CSCI1226 folder you created.

Activity 4: Downloading a Java Program and Fixing It
If you haven't already read the section "Downloading Code for a Lab" mentioned in Activity 3, then read it now. Use the link above (under Starter Files) to download BMICalculator.java into the L02/src/l02 (that last bit is ell-zero-two) project folder that NetBeans created in the previous step.

The program I have provided for you has mistakes in it. Most of those mistakes are syntax errors -- you can see them because NetBeans draws squiggly red lines under them. Before you can run this program, you need to fix them.

Note that some bits of code have squiggly grey lines under them. Those are not errors; they are just warnings. NetBeans is telling you that you haven't used those variables yet But that's OK; we'll get around to using those variables by the end of the lab.

Sometimes you might see code with squiggly yellow lines under it. That's also not an error, but it is a sign that you're doing some unusual thing. We'll talk more about that later in the term.

Once you have all the syntax errors fixed, you should look for some style mistakes. That is, there are places in the code where I've done something contrary to the style rules of the course. For example, a couple of the variables have names that don't look like variable names should. Some of the lines are not indented like they should be. And some commands' line breaks are in the wrong place.

Some of the mistakes can be fixed easily by using NetBeans' Format command (right-click in a blank area of the code pane). The variable names can be fixed easily by double-clicking the name in one place, then right-clicking it and using the Refactor > Rename... command from the pop-up menu. Of course, if you want to make those fixes by hand, you can do that, too. Just be aware that the variable names have to be changed the same way in every place they appear.

One last thing. You have now made changes to this program, so you and I are now joint authors of the version you're working on. Find where it says that "Mark Young" is an author, and add a line immediately below that says that you're an author, too. It should be just like Mark's line, but with your name and A-number.

Activity 5: Running a Java Program
We will now "run" the program by clicking the green triangle icon in the toolbar. Now is the perfect time to read "Running a Program", in the same section as the others.

After running the program, you should see the output in the output window (typically at the bottom of the program).

This program calculates your body mass index. Enter your height in feet and inches:

Click your mouse once, to position your cursor at the end of the last line outputted. Then type a 5, a space, and a 7, then press the enter key.

I've written those characters in blue followed by a grey arrow in the diagram below. Do not try to make blue or grey output!
When the program asks you to press enter, press the enter key.
Shown by the grey arrow. Don't try to print a grey arrow!
You should see:

This program calculates your body mass index. Enter your height in feet and inches: 5 7 Enter your weight in pounds: Press enter... You said you were feet' inches" tall. You said you weighed poundWeight lb. Your height in metres is 0. Your weight in kilograms is 0.
Notice that it never stopped to let you answer the question about your weight.

Also notice that there are logic errors in your program. The output for your height and weight don't look right at all. Fix them so that the program prints out the feet and inches that the user typed in, and the number of pounds that the user is going to type in (after you fix that part of the program.)

Run the program again, using 6 and 2 as the input. Your output should look like this:

This program calculates your body mass index. Enter your height in feet and inches: 6 2 Enter your weight in pounds: Press enter... You said you were 6' 2" tall. You said you weighed 0 lb. Your height in metres is 0. Your weight in kilograms is 0.
Activity 6: Adding to a Java Program
Your task now is to write the code that will accept the integer value at the prompt "Enter your weight in pounds: ", and assign it to the variable "poundWeight" already declared for you. This new input statement directly follows the prompt. It should replace the assignment statement that's already there.

Now re-run the program and enter five foot seven (in numerals) at the first prompt, and 150 at the second prompt, where your newly created input statement will receive the value. It is because of your input statement the program suspends its execution, and waits for your input. You should see:

This program calculates your body mass index. Enter your height in feet and inches: 5 7 Enter your weight in pounds: 150 Press enter...

And if you did the input properly, it would have paused at the "Press enter..." prompt, and you should not see this bit (until you press the enter key again):

You said you were 5' 7" tall. You said you weighed 150 lb. Your height in metres is 0. Your weight in kilograms is 0.
Did it pause when it should have? If it didn't, then look again at the command(s) for reading your height and weight. What did I do after reading the height that you didn't do after reading the weight?

Activity 7: Adding the Calculations
There are a few more changes that need to be made to this program to complete Activity 6. Four of them are calculations that need to be done: Add those calculations to the program and check to see if the output comes out right:
This program calculates your body mass index. Enter your height in feet and inches: 5 7 Enter your weight in pounds: 150 Press enter... You said you were 5' 7" tall. You said you weighed 150 lb. Your height in metres is 1.7018. Your weight in kilograms is 68.18181818181817.
Don't worry if your numbers are a little bit different -- 1.70179999997 say, instead of 1.7018. That's called round-off error, and it's normal.

Note that we didn't print out the BMI. Add commands to print out the BMI. Put it on a line of its own at the bottom, separated from the one above it by a blank line:

This program calculates your body mass index. Enter your height in feet and inches: 5 7 Enter your weight in pounds: 150 Press enter... You said you were 5' 7" tall. You said you weighed 150 lb. Your height in metres is 1.7018. Your weight in kilograms is 68.18181818181817. Your body mass index (BMI) is 23.54244365022884.

Try your program with some different heights and weights. Some examples:

Activity 8: int to double
One last change. Try entering four foot three and one half inches as the height. What happens?

That's called a crash. The user has done something the program was not expecting at all, and couldn't cope with. And yet, it's something that a reasonable user might expect it to be able to do. Similarly, giving a weight of 87.5 lbs would also be reasonable. (Try it.)

The two crashes are pretty much the same. The error message is Exception in thread "main" java.util.InputMismatchException. It means that the input the user typed didn't match the kind of input the program asked for. The last line of the message has a link back to the program line where the crash occurred. For example, it might say: at l02.BMICalculator.main(BMICalculator.java:27). That says that the crash happened on line 27 of my program file (BMICalculator.java).

Don't worry if your line number is different! Your message is talking about your version of the program, which could be a bit different from mine.

Change your program so that the number of inches and the weight in pounds that the user enters can be double values instead of just int values. (The number of feet should still be an int value! It doesn't make sense to say you're "five and a half feet four inches" tall.)

This program calculates your body mass index. Enter your height in feet and inches: 4 6.5 Enter your weight in pounds: 86.5 Press enter... You said you were 4' 6.5" tall. You said you weighed 86.5 lb. Your height in metres is 1.3842999999999999. Your weight in kilograms is 39.31818181818181. Your body mass index (BMI) is 20.51790378075557.

Now take a moment and go over the code you've written. Make sure the indentation is consistent, and blank lines are used to highlight the pieces that make up the program. Be sure the variable names are fitting, and that the data types are appropriate.

Once all the activities are completed you can submit your Java program by one of the SUBMIT links above/below.

Make sure you pass in the correct file and that it has the correct name:

In particular, make sure you submit your modified copy of the .java file. You should find it in your L02/src/l02 folder.

The pass in page will print a little "receipt" for your records. Make sure you read it carefully. Make sure it doesn't have any red text on it at all. Make sure the submitted size is larger than the original size (you should have added quite a bit of code).

Submit this/these files:

You will be graded on the following:

  1. You are identified by both name and A-number as an author of the program,
  2. ... in the same way that Mark Young was (and STILL IS)
  3. all syntax errors have been removed (and none added)
  4. all style errors have been fixed (and none added)
  5. the correct variable(s) have been changed from int to double (not Double!)
  6. ... and the correct variable(s) left as int
  7. the program reads the user's weight
  8. the user can enter decimal values for inches and pounds
  9. the input command for the weight is followed immediately by a command to "tidy up" the input stream
  10. three or more of the calculations for the calculated variables are correct
  11. ... all four calculations for the calculated variables are correct
  12. the output of height and weight has been fixed (to show the height and weight the user entered)
  13. output for the BMI has been added, is correct and clear and formatted as required
  14. Code is properly indented and uses white space appropriately

SUBMIT   /   CHECK