Ten Unrelated Methods - University of Arizona



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

Collaboration Solo: 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 Tests WebCat will use fifty @Test methods to test your ten methods. If one assertion fails, you lose 2% on your problem coverage. Here is one such @Test methods, which you should also have:

@Test

public void testFailedIsLeapYearWhenEndOfCentury() {

TenMethods tm = new TenMethods();

assertFalse(tm.isLeapYear(2100));

}

If the above assertion fails, WebCat will provide a hint. The hint will be the method heading below @Test For example, if the above assertion fails, the following hint will appear if you 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

//

import static org.junit.Assert.*;

import org.junit.Test;

public class TenMethodsTest {

@Test

public void 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

*/

public class TenMethods {

public String antiMatter(String matter) {

return "?"; // Change this method body

}

// Nine More methods to be completed below

}

The ten methods you should really develop one at a time!

___________________________________________________________________________________

1) public String antiMatter (String matter)

Complete method antiMatter that returns the 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 hvinag the 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 the last 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:00 pm, 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 integer argument with 0 (Java sees 0115 as 115 octal which is the integer 77). You may assume earlyTime 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

public void testFirstOf3Strings() {

TenMethods tm = new TenMethods();

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

// add more assertions . . .

}

__________________________________________________________________________________

10) public String 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 if expressions 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-in with 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.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download