Assignment: Two Methods

Collaboration: Solo. Complete this by yourself with help from section leaders.

This assignment asks you to write two methods in classTwoMethods and test methods for both in class TwoMethodsTest. Develop (code and test) the isOdd method first.

Implement a Unit test and 2 methods: boolean isOdd(int) and int largest(int, int, int)

  • In Eclipse, create new Java Project with the name TwoMethodsTest
  • Create a class withFile > New > Class
  • AfterName:type the name of the class you want to create. The first class you create will be calledTwoMethods. After you type that in, clickFinish(at bottom).
  • Here is the beginning of the first required class:

// This class has tests for two methods to provide practice for testing

// methods that process Strings and some of the primitive types.

// Programmer: Your Name

importstatic org.junit.Assert.*;

import org.junit.Test;

publicclass TwoMethodsTest {

@Test

publicvoid testIsOdd() {

TwoMethods myFuns = new TwoMethods();

// One test case to test the isOdd method

assertTrue(myFuns.isOdd(3));

// TODO: Add more assertions to fully test isOdd

}

// One more test method will be completed here later

}

  • You are probably seeing errors because JUnit 4 is not on the build path. Click on a light bulb and choose the quick fix 'Add JUnit 4 to the build path' .
  • Even though you are probably seeing compiletime errors (red lines), add more assertions until you feel you have covered all test cases such as odd numbers, negative integers and 0.
  • Now make the unit test compile by adding class TwoMethods to your project with the isOdd method (it is shown below with a non working method body--okay it may work about half of the time).

/*

* This class has two unrelated methods to provide practice for

* implementing methods that are tested from a unit test.

* Programmer: Your Name

*/

publicclassTwoMethods {

// Return true ifanIntegeris an even integer

// isOdd(1) -> true

// isOdd(2) -> false

public booleanisOdd(int anInteger) {

// TODO: Change this method body

returnfalse;

}

// One more method will be completed below

}

  • Write the method body so isOdd works
  • Go back to the file ControlFunTest (click the tab above the edit window)
  • Run ControlFunTest as a JUnit test by selecting Run > Run as > Junit Test

2) publicint largest(int a, intb, int c)

  • After completing isOdd, write a test method for largest in class TwoMethodsTest and write method largestin class TwoMethods.

// Return the larger of the three given integers

// largest(1, 2, 3) -> 3

// largest(9, 9, 9) -> 9

publicint largest(int a, intb, int c)

Submit your project to WebCat

You will be graded on a scale of 0.0 through 20.0 automatically by WebCat. Here are the instructions for getting the Submit plugin for Eclipse and Submitting your first Webcat project.
  • Read your email to get for yourWeb-Cat account ID (almost always what comes before @email.arizona.edu) and the randomly generated password
  • Login to WebCat at this url:
  • Change your password to a good password that no one else can guess.
  • If you haven't already, download WebCat's submit plugin to make it easy to submit projects:

webcat-eclipse-submitter-1.2.1.zip

  • Unzip this archive file.
  • Copy three highlighted folders (beginning with net.sf.webcat) into the Eclipse plugin. For Window, the Eclipse plugin folder should be C:\Program Files\eclipse\ or c:\Program Files\eclipse-SDK-3.6-win32\eclipse\
  • For the Mac, you should copy these three folders into your folder ending with /Applications/eclipse/plugins

Submit your Project to WebCat using the Plugin
  • From Eclipse, select Window > Preferences > Electronic Submissionfor Windows or Ubuntu, for a Mac, select Preferences > Electronic Submission
  • Using your own Default username and your own email address, fill in the four fields using this url for the Assignment definition URL (Recommended: Copy and paste this into the first field):

Click OK
Submit your project to WebCat
  • Right click on your project name
  • Select the Submit button (near the bottom of the list of options). WebCat will return the list of published projects under C Sc 127 and C Sc 227

  • Click on the appropriate project name , which should be TwoMethodsTest under C Sc 227
  • Make sure UserName has your correct WebCat user name
  • Type in your WebCat password
  • Click Next and Finish
  • Webcat will post a page to the Eclipse Web Browser, which may indicate an incorrect password or feedback indicating your code is being examined.
  • Click view your graded results and wait until the report is posted back to you
  • Wait until WebCat posts results back to the Eclipse browser--could take 15-45 seconds
  • If you get 100%, Congratulations!
Interpreting Results

If you get 20 points, great. Otherwise scroll down and click on the plus symbol to the left of Estimate of Problem Coverage and read the hints.

You will also loose points of you do don't execute every line of code. This is known as code coverage. Open files that are not 100% code coverage and look for pink lines that have executed 0 times. To fix this, test your code better. Call each method at least once. Write tests that will cover every branch of code.