Lab 3 Method Testing Compsci 121 PDF

Title Lab 3 Method Testing Compsci 121
Course computer science intro
Institution University of Massachusetts Amherst
Pages 7
File Size 493.4 KB
File Type PDF
Total Downloads 96
Total Views 145

Summary

Lab 3...


Description

Lab 3 Method Testing COMPSCI121 Content Learning Objectives   

Use two classes to create objects. Create UML diagram. Run JUnit tests and interpret the results.

Process Learning Objectives 

Leveraging knowledge and experience of other students (Teamwork).

2.1. Downloading zip file and creating a folder Log in to Moodle, download the LAB3.zip file and save it in your COMPSCI121 folder. HOW TO UNZIP: MAC: by double-clicking on it. Windows: right click, select extract to... A LAB3 folder will be created. Note: Do not click/double click on the zip file in jGRASP! Open JGRASP by clicking on the icon on your laptop. Close all projects and java files if open from previous sessions. Using the Navigation pane navigate to the LAB3 folder. IMPORTANT: Click on LAB3.gpj to open the JGRASP project file. FIGURE 1

Your “Open Projects” window should resemble that shown in Figure 2. Note: To continue, you must be able to see the colourful cross and running man icons. Otherwise, you may not have opened the .gpj project file.

If you still have a problem, your JUNIT installation is incorrect. You may continue the lab, but you should go to office hours for help. FIGURE 2

Compile your files: Calculator.java, CalculatorMain.java,and CalculatorTest.java.

Report to your TA that all team members have finished step 2.1. Otherwise, help each other! Note - you cannot continue if your files do not compile.

2.2 Opening the UML diagram

Click on the UML symbol Your UML diagram should look like that in figure 3. Close the window.

FIGURE 3

2.3 Running the main class Open CalculatorMain.java. Run it. You should see the menu shown in figure 4. Type F and then 84 in the console window (use the or key on your keyboard to indicate the end of your input). FIGURE 4

Q2.3.1. What information does the program return to you? Write the answer here. Answer: 84 deg Fahrenheit = 28.88888888888889 deg Celsius. Q2.3.2. Next, choose C as your option and enter 0. What information does the program return to you? Write the answer here. Answer: 0 deg Celsius = 32.0 deg Fahrenheit.

Q2.3.3. Now try the I and E options and satisfy yourself that the program works.

Report to your TA that all team members have finished step 2.3.3. Otherwise, help each other!

(15 min) Activity 3. Figuring how classes interact Start time: 11:26

Note that you were able to use the main method without having to see the code implementation. Now open Calculator.java.

3.1 Analyzing methods Q3.1.1. What type of variable does the inchesToCentimeters method return back to the main method? Answer: This returns a double value . Q3.1.2. What type of parameter does the inchesToCentimeters method expect from the main method? Answer: double value

3.2 Analyzing main method Open CalculatorMain.java. Q3.2.1. In CalculatorMain.java, a new instance of the Calculator class is created. Below, write the statement that creates this instance in the main method. Answer: Calculator calc = new Calculator();

Q3.2.2 In the main method, find the statement that invokes (calls) and sends arguments (information) to the method that calculates Fahrenheit to Celsius in the Calculator class .

Answer: result = calc.fahrenheitToCelsius(Double.parseDouble(inputStr));

Report to your TA that all team members are able to finish activity 3 Otherwise, help each other!

(15 min) Activity 4. Running the Tests Start time: 11:32

4.1. Running tests Open CalculatorTest.java and run the tests. All the test should have passed as in figure 5. Otherwise, you may not have opened the .gpj project file. If you still have a problem running tests, your JUNIT installation is incorrect. Go to office hours for help. Your group will now proceed without you. FIGURE 5

In the JUNIT tests, the assertEquals() method checks that the two doubles are equal to within a positive delta. If they are not, an AssertionErroris thrown

In the test file, you see the first example:

The signature for the assertEquals method is:

Click on the link and read more about the above method at: http://junit.sourceforge.net/javadoc/org/junit/Assert.html Don’t worry if you can’t get this now. You’ll soon become proficient at reading tests.

In the CalculatorTest file, see how a new instance of the Calculator class is created with the statements: Calculator calc; calc = new Calculator();

Q4.1.1. In the centimetersToInchesTest method (see figure below) what argument is sent to the calc.centimetersToInches method?

FIGURE 6

Answer: 2.54

Q4.1.2. What result is assertEqualsexpecting from the centimetersToInchesTest? Answer: 1.0

Good job! You now have worked on a project with two Java classes and a test file. You created the UML diagram. Give yourself a pat on the back! Create a pdf of this file and upload in MOODLE. There is no need to upload the java files. Reflector fill in the Reflector's Questionnaire in Moodle! If you have time left over in the lab, try to write an additional test in the CalculatorTest file. First, think about what you would like to test and what the test result will be. Then code the test, compile the file and run it....


Similar Free PDFs