Chapter 2 Part 2 Recitation – Java API, String Class, Variable Scope, Comments, Code Style

Introduction

In this recitation, you will practice what you have learned about the following topics: Java API, String Class, Variable Scope, Comments, Code Style. Are you ready? Here we go!

 

Activity #1: Java API

Java API has thousands of pre-written codes, known as classes, readily available for you to use. The best way of figuring out about how to use a given class is by searching the internet, studying examples and then adapting them to your needs.

For this first activity, imagine that you work for a company that installs house external lighting. One of the main products that your company sells is LED snowflakes light projectors. The distance that these devices must be from the house front door is computed by the following equation:

The distance equation is as follows:   distance = house height / tangent(aperture angle)

where the aperture angle of this device is 30 degrees.

Your manager has asked you to make a Java program that does such kind of calculation, and she expects that by entering (hard-coding) the house height, the program would provide the optimal distance to install the projector in the front yard.

1. Do an internet search and figure out how to use a Java API class to compute the tangent of a given angle (pay attention to units! Angles sometimes are given in units of degrees and sometimes in units of radians)

2. Create a java program with the equation above and then run it with a 20 foot-tall house. Your program should print something like this:

Distance from the front door should be 34.6 feet for a 20 foot tall house

 

Activity #2: Scope, Comments, and Code Style

A Real-State company has hired you to take a look at someone’s old code and do all the improvements required to make it work, print the expected numbers, put some comment lines to clarify some code lines and finally reformat the code (code style) based on the Java code standards (e.g., adding tabs, camelCase variable names, etc.). The code is a mess, but they know that you can do it.

1. In NetBeans create a new project named ScopeCommentsAndCodeStyle. When NetBeans displays the java window in the IDE, delete all the contents found in that window and paste the source code found below:

package scopecommentsandcodestyle; public class ScopeCommentsAndCodeStyle public static void main(String[] args) {int Number_Of_Bedrooms = 3;int number_of_bathrooms = 2;
boolean househasswimmingpool = false;double sales_price = 200000.00;String houseLocation = “1234 Vila Sesame St”;
char BUILDING_type = ‘R’;boolean is_Price_Negociable = true;double discount = 0.0; System.out.println(“House address: ” + houseLocation);if (isPriceNegociable) { discount = 30000;}System.out.println(“House minumum price is ” + salesPrice-discount + “dollars”); System.out.println(“Number of beds and baths in the house: ” +
(NumberOfBedrooms + umber_of_bathrooms));System.out.println(“Seller asked price is: $”+ salesPrice);}}

Note: sometimes when we copy code from a website, the double quotes are not recognized by Java, so you might need to retype some or all double-quotes from your code to remove compilation errors. This also happens sometimes for single quotes, minus signs, etc. NetBeans shows you errors on those lines. Just fix them accordingly.

2. You realize that this is code is really in bad shape! Your first task is to reformat it by making each command in a separated line, then correcting their alignments using TABs. See my video about Code Style if you are not sure what should be done here.

3.The variable names found in this code do not follow Java Standards (camelCase), so go ahead and fix all the variables that do not follow such convention.

4. NetBeans should be alerting you about code errors due to variable scope issues. Without change the method headers (since we are a long way from studying methods in this course), I want you to move the necessary variables that are out-of-scope to some other place in this source code that will make them in-scope. But only the necessary ones! Do not move all of them.

5. If you get stuck ask the TA to help you. Don’t be shy; you are already paying him to help you!

6. Place comment lines prior to any variable definition and put in those comment lines what the variable is for. Example

// Is the sale price negociable
boolean isPriceNegociable = true;

7. At this point, if you have fixed all the issues with this code, NetBeans should not show any red dots in your source code. If you still have any of those, fix them and if needed ask your TA for help.

8. Run your code and see its output. Something is wrong with the number of beds and baths in the house. Fix this issue.

Show your code to your TA and let he tell you what you have missed and fix it. Show the final version again to the TA for grading.

A question for you to think about: “Have the comment lines added any important/meaningful information to your code?”

 

Activity #3: Java String

1. Create a NetBeans project under the name PlayingWithJavaStrings.

2. In the source code do the following:

i) Create two separated String variables, one for the first and the other for the last names and set them to “John” and “Smith”, respectively.

ii) Create a String variable for the full name and then concatenate the first and last name variables into it.

3. Using the String methods found in the Java API:

  1. Print the full name in the terminal window
  2. print in the terminal window the contents of the full name variable in all CAPITAL LETTERS
  3. same as above but all in lower case letters
  4. Print the number of letters that the full name has
  5. Print the 6th letter of the full name
  6. Create two more String objects with the contents of “Mary” (first name) and “Smith” (last name). Compare if John and Mary have the same last name and if so print that they are related, otherwise that they are not