C Sc 127B Assignment 2: Two Methods

Getting Eclipse on your Machine

If working at home, either

  1. Watch this Video that shows the Eclipse download site, how to create a project and a class, and how to run an hello world programEclipse Download Videoor,
  2. Download Eclipse 3.5 (Galileo) from SelectEclipse IDE for Java Developers (91 MB).Unzip the archive and place the folder named "eclipse" into a convenient place where other applications exist. Make a shortcut of (or on a Mac, an alias for) Eclipse and copy that shortcut (alias) to your desktop.

Implement and Run a Welcome Program

  • If you are in the lab and you haven’t already done so, login to Ubuntu
  • Click the Eclipse Icon
  • Eclipse will start and offer a Welcome page, which you should close (if it is visible)
  • While in Eclipse, selectWindows > Preferences > Expand Java > Select Compiler
  • Create a new project by clicking onFile > New > Project
  • Make sureJavaProjectis selected, and then clickNext(at bottom)
  • Name your project with a name indicating what this assignment is about:TwoMethods

Implement a Unit test and 2 methods: boolean isEven(int) and String reverse(String)

This assignment asks you to write two methods in classTwoMethodsand test methods for both in classTwoMethodsTest. Develop (code and test) the isEven method first.

// Return true ifanIntegeris an even integer

// isEven(2) -> true

// isEven(3) -> false

public booleanisEven(int anInteger)

  • 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

importstaticorg.junit.Assert.*;

importorg.junit.Test;

publicclassTwoMethodsTest {

@Test

publicvoidtestIsEven() {

// Need a TwoMethods object to send messages to

TwoMethods myFuns =newTwoMethods();

// One test case to test the isEven method

assertTrue(myFuns.isEven(2));

}

// One more test method will be completed below
}

  • 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 isEven 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

// isEven(2) -> true

// isEven(3) -> false

public booleanisEven(int anInteger) {

returnfalse;// Change this method body

}

// One more method will be be completed below

}

  • Write the method body so isEven 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
  • After completing isEven, write a test method for reverse in class TwoMethodsTest and write method reverse in class TwoMethods.

// Return a new String that has the same characters as str in reverse order

// reverse("abcd") -> "dcba"

publicString reverse(String str)

  • When both methods are working, print both classes with File > Print > Print
  • Or if online, copy and paste both classes into an email