TenMethods in One Class with Assertions in Another (60 pts)

CollaborationSolo: Complete this Assignment by yourself with help from section leaders only. Do not work with anyone else, do not copy any code directly, do not copy code indirectly through reading, do not give your code to anyone, do not post it on the web. Begin early, preferably today.

Early turn-in Bonus: To encourage early starts and reduce the risk of 20 minute waits, you can earn +1 bonus point for each 24 hours early turn-in. For example, a turn-in before 9:00 pm Friday results in 4 points added to this Assignment. Recall that the Assignment grade is 50% of your final grade.

What This project asks you to solve ten problems related to primitives, Strings and selection control. You are asked to write ten methods in one class named TenMethods along with at least one test method for each of those ten methods in another class named TenMethodsTest. Develop one method at a time! No more than one.

A Note about Rick's Reference TestsWebCat will use fifty@Test methods to test your ten methods. If one assertion fails, you lose 2% on your problem coverage. Here is onesuch @Test methods, which you should also have:

@Test

public void testFailedIsLeapYearWhenEndOfCentury() {
TenMethodstm = newTenMethods();

assertFalse(tm.isLeapYear(2100));

}

If the above assertion fails, WebCat will provide a hint. The hint will be the methodheading below @Test For example, if the above assertion fails, the followinghint will appear ifyou scroll down to see what exists under WebCat's heading Problem Coverage(it’s magic):

isLeapYear(2100) failed

Such hints are not intended to give you a solution, but rather to give you hints about what we were testing when your code did not meet the expected behavior in the specification of each of the ten methods that follow. It is up to you to add more tests, fix your method, and resubmit. You may turn in your project as often as you wish, but try for two or three maximum. Do not write a little code and expect WebCat to fix it or to tell you how to fix it. Strive for 100% on the first try and you will get good at software development.

The Assignment

You will need two classes. The beginning of both required classes are provided next to get you started.

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

// methods that do things with primitives, Strings and selection control.

//

// Programmer: YOUR NAME

//

importstatic org.junit.Assert.*;

import org.junit.Test;

publicclass TenMethodsTest {

@Test

publicvoid testAntiMatter() {

TenMethods tm = new TenMethods();

// Three test cases to test method antimatter(String matter)

assertEquals("anti-Shoes", tm.antiMatter("Shoes"));

assertEquals("anti-Tennis Racket", tm.antiMatter("Tennis Racket"));

assertEquals("anti-LOL", tm.antiMatter("LOL"));

}

// More test methods to be completed below.
}

And here is the beginning of the class containing the methods being tested.

/*

* This class has ten unrelated methods to provide practice for implementing

* methods that process primitives, Strings and selection control.

*

* Programmer: YOUR NAME

*/

publicclass TenMethods {

public String antiMatter(String matter) {

return"?"; // Change this method body

}

// Nine More methods to be completed below

}

The ten methodsyou should really develop one at a time!

______

1)public String antiMatter (String matter)

Complete method antiMatter that returnsthe String argument matter preceded by "anti-".

antiMatter("LOFL") →"anti-LOFL"

antiMatter("")→"anti- "

______

2) public String csLatin(String aWord)

Background: Aoccdrnig to a rscheearch at an Elingsh uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is hvinagthe frist and lsat ltteer is at the rghit pclae.

Complete method csLatin will mix up the middle characters in a 5 letter word. Assuming the letters are indexed 0-1-2-3-4, they should end up in the order 0-3-1-2-4. Your method will accept a 5-character string as an argument and return a 5-character string reordered as described. No other lengths will be allowed. No need to test for 4 or 6 length words.

Examples:

csLatin (“apple”)  "alppe"

csLatin (“order”)  " oerdr"

csLatin (“first”)  "fsirt"

______

3) public String halfAndHalf(String str)

Given a String argument, return a new string that has the upper case version of the first half of the argument at the end and the lower case version of the last half of the argument at the beginning. If there are an odd number of letters, there should be one more lower case letters at the beginning of the result. Assume the String argument's length >= 2.

halfAndHalf("x2y4abcd") "abcdX2Y4"

halfAndHalf("AbcDef")  "defABC"

halfAndHalf("Hello")  "lloHE"

______

4) public String lastNameLast(String fullName)

Given a String argument that contains a last name, a comma and a space (", "), a first name, a space, and a one letter initial, return a string that is the first name followed by one space, followed by the initial and a new dot, followed by thelast name (no commas). All parts have at least one letter.Assume that the last name is always followed by a comma as the first part of the String argument.

lastNameLast("Mercer, Rick H") "Rick H.Mercer"

lastNameLast("Zevon, Warren G”)  "Warren G. Zevon"

______

5) public String timeDifference(int earlyTime, int laterTime)

Complete timeDifference that takes two different military times and returns a string with the difference in hours and minutes, separated by ":". The int argument 0 represents midnight, 700 is 7:00 a.m., 1314 is 14 minutes past 1:00pm, and 2200 is 10 pm). Assume both arguments are correct military time representations where 1 means 00:01 and 1955 means 19:55 (7:55 at night). Also assume the first argument earlyTime is not later than the 2nd argument laterTime.

timeDifference(0, 1) "0:1"

timeDifference(100, 200) → "1:0"

timeDifference(1, 2359) → "23:58"

timeDifference(1115, 1205) → "0:50"

timeDifference(956, 1955) → "9:59"

Notes:

The argument 0 is okay. However, do not begin any integerargument with 0 (Java sees 0115 as 115 octal which is the integer 77). You may assume earlyTime <= laterTime.

You may assume both times are on the same date and that both times are valid. For example, 1099 is not a valid time because the last two digits are minutes that must be in the range of 00 through 59. 2401 is not valid because the hours must be in the range of 0 through 23 inclusive. All arguments are always non-negative.

@Test

publicvoid testTimeDifference() {

TenMethods tm = new TenMethods();

assertEquals("0:1", tm.timeDifference(2358, 2359));

// ... add many more assertions

}

______

6)publicdouble fToC(double fahrenheit)

Using the formula C = 5/9(F-32), complete method fToC that returns the Celsiusequivalent of the Fahrenheit argument.

fToC (212.0) → 100.0

fToC (-40.0) → -40.0

Remember that JUnit requires a 3rd argument as the error factor when comparing two floating point numbers. So for this one, an assertion would have three arguments like this:

@Test

publicvoid test_fToC() {

TenMethods tm = new TenMethods();

assertEquals(100.0, tm.fToC(212.0), 0.01);

}

______

7) public boolean isLeapYear(int year)

Complete method isLeapYear that returns true if the integer argument represents a leap year. A leap year is a positive integer that is evenly divisible by 4 except the last year of a century, which are those years evenly divisible by 100. These must also be divisible by 400. 2000 was a leap year and 2100 will not be even though 2100 is evenly divisible by 4. Assume the argument > 1582.

isLeapYear(2008) →true

isLeapYear(2009) →false

isLeapYear(2100) →false

______

8) public String max(String a, String b)

Given two String arguments, return a reference to the String that is not less than the other. Use String's compareTo method.Note: "A" is less than "a" and "abc" is less than "abc ".

max("c", "b") → "c"
max("B", "B") →"B"
max("ma", "Ma") →"ma"
max("91", "789034") →"91"

______

9) public String firstOf3Strings(String a, String b, String c)

Given three String arguments, return a reference to the String that is not "greater than" the other two. Use String's compareTo method.Note: "A" is less than "a" and "abc" is less than "abc ".

firstOf3Strings("c", "b", "a") →"a"
firstOf3Strings("B", "B", "A") →"A"
firstOf3Strings("ma", "Ma", "ma") →"Ma"
firstOf3Strings("a ", "a ", "a ") →"a "

Here is one assertion provided as an example

@Test

publicvoid testFirstOf3Strings() {

TenMethodstm = newTenMethods();

assertEquals("First", tm.firstOf3Strings("Third", "Second", "First"));

// add more assertions . . .

}

______

10) publicString season(int month, boolean inNorthernHemisphere)

Given an integer for the month (1 is January and 12 is December) and a Boolean argument that represents the northern hemisphere when true, return the current season in that hemisphere using the table below.

season(12, true) →"Winter"
season(12, false) →"Summer"
season(3, true) → "Spring"
season(3, false) →"Fall"

Use the following table to determine the season for each month.

month / inNorthernHemisphere / ! inNorthernHemisphere
12, 1, or 2 / "Winter" / "Summer"
3, 4, or 5 / "Spring" / "Fall"
6, 7, or 8 / "Summer" / "Winter"
9, 10, 11 / "Fall" / "Spring"

Make sure you test all branches through the nested if-else. If you don’t, the code coverage check in WebCat will deduct points from your score.

Grading Criteria (60 pts)

When you have completed all 10 methods, turn in your project to WebCat. You will be graded as follows:

___/ 50 Web-Cat correctness and code coverage (the best you can get is 50.0/60.0 until your SL grades

the 10 Style and Design points).

  • Your code must compile using the specified names
  • You must have tested all of your methods with assertions in test methods
  • You must execute all statements in all 10 methods at least once
  • All ifexpressions must be true and false at least once (an if that is always true is not needed)
  • You turned in a unit test (TenMethodsTest) that has the @Test methods
  • All of your assertions pass locally in your programming environment
  • All of your assertions also pass on WebCat (they usually do if yours pass locally)
  • All of Rick's reference tests pass

___/ 10 Style and Design:

  • 2pts You used meaningful identifiers
  • 2pts All code is in the two files named TenMethods and TenMethodsTest
  • 2pts You have your name as a comment at the top of the all files
  • 2 pts You have a comment describing the purpose of each file
  • 2pts All code is formatted consistently (use Eclipse’s Source > Format)

Notes:

  • The final WebCat submission will be the one that is graded unless you inform us of an earlier turn-inwith a higher score. We see your most recent submission, not the highest.
  • WebCat employs a multiplication feature that means 90% and 90% results in 0.81 * 50 or 40.5/50 points. This is 81%of the max problem coverage points rather than the 90% one might expect.