The purpose of this lab is to:
· Gain practice writing methods with arithmetic. Programming languages, including Java, are fairly good at this, though this is a small part of regular programming, as it will be through much of this course, though not in all specialties.
o Basic operations (+, -, /, *, %, and parentheses, “()”) are all you should need, which should be enough since that’s almost all you need to get to Calculus.
o * is multiplication
o % is “modulus”. For our purposes you can think of it as “remainder”
· A secondary objective is to write code and test it with JUnit test cases. This is actually one of the better ways of testing code, especially when compared to dropping everything into a main() method and testing by hand. The main() method is really the entry point into a program and should be where the programmer is actually defining the execution of the program; adding testing just confuses what might be a complicated method already. Unit tests are designed to test smaller program parts, generally methods, individually. The tests are included in the Dr. Java project – you don’t have to write them yourself.
· Another objective is to practice working within a given specification. The methods of a class, e.g. SackOfFunctions.java, may be given to you and, while you may be able to define other methods to aid your work, you will not be able to change specified methods since that would break or require changes in any code relying on your code, which you may not have control over. This is a very common issue in projects made by groups, which are very common in industry.
· Gain a little exposure to writing methods (outside of main()). Most code does not go in the main() method.
· Use a Math library method.
· Do not change the test file, TestSackOfFunctions.java, if you do so accidentally your code may never pass all the tests. If you do so on purpose, which will be obvious, it will be considered unethical (cheating) since you will be attempting to get credit for work you did not do.
· Only fill in the code in SackOfFunctions.java where indicated. Changing a method name or signature (parameters/arguments) will break the test associated with the method.
· Don’t wait until the end to test your code. As soon as you’re done with a method, click “Test Project”, besides “Run”, saving first, as always.
· Java does not distribute across a sum in parentheses, e.g.:
12(13+14)
is a compiler error, but
12*(13+14)
is not.
· When working with doubles and division, make sure you write the numbers with a “.0” after them – otherwise they may perform integer division if both are integral values ( 4/5 is 0, in Java, because it will round down, but 4.0/5.0 is 0.8)
1. Download the project from here and place it somewhere you can find it.
2. Extract the folder. Right click on it and click “Extract All”. A new Folder should appear.
3. Open Dr. Java.
4. In Dr. Java, Click Project -> Open
5. Navigate to the Lab04 folder in the “Open” window, to wherever you unzipped it, and select the “SackOfFunctions.drjava” project. The project will open with two files on the left.
6. ONLY MAKE CHANGES TO SackOfFunctions.java file and even then only inside the method definitions.
7. Click “Test Project”, besides “Run”. One test should pass and four should fail (the green one passed). Note that the unit tests are written, as all good unit tests should, to help you figure out what the actual error was:
8. Open SackOfFunctions and examine, but do not change the twice() method. Note:
a. The parameter a in parentheses after twice, the method name at the top. It is a double value. You can treat it like a variable inside the twice() method. It is not visible outside the method (after its closing curly brace, } ), meaning that it is only accessible between the curly braces, { … }.
b. Note the return type, double, before the method name, twice. It indicates the type of the value returned, double in this case.
c. The return statement. The return statement is what actually “returns” or yields the value to the code that calls the method. Many, perhaps most, methods are called to yield some sort of result or otherwise do something and then possibly return other information.
9. Define the square() method, which calculates the square of a number.
a. Be sure change the return value from 0. (That is there to ensure it will compile successfully).
b. Your answer need only be one character different than twice(), excepting the method name.
c. Run the tests: click “Test Project”
i. If 2 of 6 tests pass, good job and go to the next step
ii. Otherwise, figure out your mistake.
10. Define the calcBMI(double weight, double height) method.
a. Its input is a weight, followed by a height – order matters.
b. The formula for this is
c. (you don’t have to worry about units, which are lbs. and inches, respectively)
d. Run tests.
11. Define idealGasVolume(double Patm, double nMoles, double TKelvin)
a. It calculates the volume of an ideal gas given:
b. P -- pressure in atmospheres
c. n -- number of moles of gas
d. T -- temperature in Kelvin
e. R = 0.082057 L atm / (mol*K)
f. returns V -- volume in liters
g. Ideal Gas Law:
PV = nRT
h. You'll need to do a single algebraic manipulation to use this equation
i. Again, just ignore units, but not in real life – that’s how you crash a Mars Orbiter.
j. "ideal gas" is chemistry term -- this law generally but not always applicable, depending on details
k. Run tests.
12. Define hypotenuseLength(double a, double b)
a. Define a method to calculate the length of a hypotenuse of a right triangle, given the lengths of the other two sides.
b. You may use Math.sqrt(), which will return the square root of a number.
c. Use the Pythagorean Theorem: a2+b2=c2
d. Again, you will have to do a single algebraic manipulation and one you’ve probably done before.
e. Run tests. You should now see the following if you’re done:
13. Make sure you upload your completed SackOfFunctions.java file (no need to upload anything else). It’s in the Lab04 directory you unzipped at the beginning.
1. Why was the twice() method not called double()?
2. How useful was the Math method, sqrt()? Do a little research on how square root is calculated in practice and give a quick summary (2-3 sentences).
3. Like math but hate the tedious calculations or just hate math in general? Well, a program may help avoid the number-crunching part. Think of a case from your work, class, or extracurricular activities where a small program that implements mathematical functions may be useful and describe it briefly.