Ten Unrelated Methods



Project: Four Methods in One File, Four Test Methods Given in Another File

Collaboration: Solo. This is a Programming Project, not a Lab. Complete this project by yourself with optional help from section leaders. 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. Begin early.

This project asks you to write five methods related to primitive types and Strings objects. You are asked to write five methods--one at a time--in class FiveMethods. You also must write at least one test method for each of these five unrelated method class FiveMethodsTest. First, begin a new Java Project in Eclipse named FiveMethods.

1) public String antiMatter(String matter)

Everyone knows that interplanetary space travel systems are fueled by letting matter and anti-matter mix. With this in mind, code a method that will take a String with the name of some thing or idea. Return a String with "Anti-" prepended to it. This method will tell us what to mix with that thing or idea so we can create a reaction to fuel our ships! Don't forget the dash!

matterAntiMatter ("Shoes") → "Anti-shoes"

matterAntiMatter ("Tennis Racket") → "Anti-Tennis Racket"

matterAntiMatter ("LOL") → "Anti-LOL"

Here are the beginnings of the two required classes:

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

// methods that do things with Strings and a few of the primitive types.

//

// Programmer: YOUR NAME

//

import static org.junit.Assert.*;

import org.junit.Test;

public class FiveMethodsTest {

@Test

public void testAntiMatter() {

FiveMethods myFuns = new FiveMethods();

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

assertEquals("Anti-Shoes", myFuns.antiMatter("Shoes"));

assertEquals("Anti-Tennis Racket", myFuns.antiMatter("Tennis Racket"));

assertEquals("Anti-LOL", myFuns.antiMatter("LOL"));

}

}

Here is the beginning of the second class containing the methods that are being tested.

// This class has five unrelated methods to provide practice for implementing

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

//

// Programmer: YOUR NAME

//

public class FiveMethods {

public String antiMatter(String matter) {

// TODO: Change this method so it meets the specs

return "Needs work";

}

}

2) public double triangleArea(double base, double height)

Given the base and height of a triangle, let triangleArea return the area of the triangle. Assume positive input since negative lengths do not make sense.

triangleArea (5.0, 5.0) → 12.5

// Put this in FiveMethodsTest.java

@Test

public void testTriangleArea() {

FiveMethods fm = new FiveMethods();

assertEquals(12.5, fm.triangleArea(5.0, 5.0), 1e-12);

// TODO: Add two more assertions

}

[pic]

// Put this in FiveMethods.java

public double triangleArea(double base, double height) {

// TODO: Complete this method

return 0.0;

}

3) public String removeEnds(String str)

Complete method removeEnds so it returns a substring of the supplied String that does not have the characters at either end. The String argument always has at least two characters.

removeEnds ("Marker") → "arke"

// Put this in FiveMethodsTest.java

@Test

public void testRemoveEnds() {

FiveMethods fm = new FiveMethods();

assertEquals("arke", fm.removeEnds("Marker"));

// TODO: Add two more assertions

}

[pic]

  // Put this in FiveMethods.java

// Precondition: str.length() >= 2

public String removeEnds(String str) {

// TODO: Complete this method

return "Needs work";

}

4) public String splitString(String str)

This method takes in a string of length 1 or greater, and returns a string with a space added into the middle of the string. If the string's length is an odd number, the second half of the string will be the longer half.

splitString ("Wildcat") → "Wil dcat"

splitString ("ab") → "a b"

@Test

public void testSplitString() {

FiveMethods fm = new FiveMethods();

assertEquals("Wil dcat", fm.splitString("Wildcat"));

// TODO: Add two more assertions

}

[pic]

// Put this in FiveMethods.java

// Precondition: str.length() >= 2

public String splitString(String str)

// TODO: Complete this method

return "Needs work";

}

5) public String halfAndHalf(String str)

Complete method halfAndHalf to 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.

halfAndHalf("x2y4abcd") → "abcdX2Y4"

halfAndHalf("AbcDef") → "defABC"

halfAndHalf("Hello") → "lloHE"

halfAndHalf("U of A") → "f aU O"

If there is an odd number of letters, there should be one more lower case letters at the beginning of the result. Assume the String arguments length >= 2.

@Test

public void testHalfAndHalf(){

FiveMethods fm = new FiveMethods();

assertEquals("defABC", fm.halfAndHalf("AbcDef"));

// TODO: Add at least three more assertions

}

[pic]

// Put this in FiveMethods.java

// Precondition: str.length() >= 2

public String halfAndHalf(String str) {

// TODO: Complete this method

return "Needs work";

}

Grading Criteria (100pts)

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

____+100 Web-Cat correctness and code coverage.

• Your code must compile using the specified names

• Rick's tests must pass

• You must have tested all of your methods with assertions in test methods

• You must execute all statements in all methods at least once

• All of your assertions must pass on WebCat

• The final WebCat submission will be the one that is graded unless you inform us

• WebCat employs a multiplication feature that means 90% and 90% results in 0.81 * 80 or 64.8/80 points. This is only 81% rather than 90%.

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

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

Google Online Preview   Download