Ten Unrelated Methods
ControlAndArrayFun: Fifteen Methods in One Class
Collaboration Solo, with help from our book, classroom resources, section leaders, or Rick. Anyone can also share thoughts about any specification and possible algorithm. Sharing code, copying code, or looking at another's Java source code is a Violation of Academic Integrity. Complete these by yourself.
This project asks you to solve problems needing selection, repetition, and arrays. You are asked to complete the methods in the given class ControlAndArrayFun.java along with at least one @Test method for each of those methods in another class ControlAndArrayFunTest.java. Suggestion: develop one method at a time. Do the first 7 before Friday's class. Begin with a starter project:
• Download this file to a place you can easily find in the minute or so
• From Eclipse, select File > Import > General > Existing Projects into Workspace > Next
• Click the radio button to the left of Select archive file and click the Browse button to the right
• Browse to ControlAndArrayFun.zip file you just downloaded
• Click the Finish button
Here are parts of the two Java classes needed for this project with an @Test method for the first problem.
/* This class has @Test for one of the 15 methods in ControlAndArrayFun.
* Unit tests never need 100% code coverage, only the methods they are testing.
* You should write at least one @Test with assertions for the other 14.
* Programmer: YOUR NAME
*/
import static org.junit.Assert.*;
import org.junit.Test;
public class ControlAndArrayFunTest {
private ControlAndArrayFun myFun = new ControlAndArrayFun(); // Only needed once
@Test
public void testIsLeapYearTrueWhenNotEndOfCentury() {
assertTrue("Failed isLeapYear(1996)", myFun.isLeapYear(1996));
assertTrue("Failed isLeapYear(2004)", myFun.isLeapYear(2004));
assertTrue("Failed isLeapYear(2216)", myFun.isLeapYear(2216));
}
// Add more @Test methods in this unit test class . . .
}
// This class has many methods that process primitive types and Strings requiring
// ifs, loops, and arrays.
// Programmer: YOUR NAME
import java.util.Scanner; // Scanner will be needed for a few methods.
public class ControlAndArrayFun {
// 1) 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.
public boolean isLeapYear(int year) {
// TODO: Complete this method
return false;
}
// Fifteen methods stubs will be completed in this class (only one included above)
}
__________________________________________________________________________________
1) 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 (@Test methods given in the starter project).
__________________________________________________________________________________
2) 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 " (note blank spaces)
firstOf3Strings("c", "b", "a") returns "a"
firstOf3Strings("B", "B", "A") returns "A"
firstOf3Strings("ma", "Ma", "ma") returns "Ma"
firstOf3Strings("a ", "a ", "a ") returns "a "
Here is one assertion provided as an example
@Test
public void testFirstOf3Strings() {
ControlAndArrayFun myFun = new ControlAndArrayFun();
assertEquals("First", myFun.firstOf3Strings("Third", "Second", "First"));
// add more assertions . . .
}
___________________________________________________________________________________
3) 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) returns "Winter"
season(12, false) returns "Summer"
season(3, true) returns "Spring"
season(3, false) returns "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.
___________________________________________________________________________________
4) public int factorial(int n)
Given an integer argument return n factorial, which is written as n!. 5! = 5*4*3*2*1 or in general, n! = n*(n-1)*(n-2) …*2*1. Use a for loop.
factorial(1) returns 1
factorial(2) returns 2, which is 2 * 1
factorial(3) returns 6, which is 3 * 2 * 1
factorial(4) returns 24, which is 4 * 3 * 2 * 1
___________________________________________________________________________________
5) public String reverseString(String arg)
Given a String, return a reference to a new String that stores arg's characters in reverse order.
reverseString("") returns ""
reverseString("1") returns "1"
reverseString("1234") returns "4321"
reverseString("racecar") returns "racecar"
___________________________________________________________________________________
6) public double sumInScanner(Scanner stream)
Given a Scanner constructed with a String of valid double values, return the sum of those numbers. One assertion is shown as an example. Test more thoroughly.
@Test
public void sumInScanner() {
Scanner stream = new Scanner("1.1 2.0 3.3");
// When comparing doubles, use 3rd argument as the allowable error factor
assertEquals(6.4, myFun.sumInScanner(stream), 1e-12);
}
_______________________________________________________________________________
7) public int rangeInScanner(Scanner stream)
Given a Scanner constructed with a String of integers, return the range. Range is defined as the maximum integer – the minimum integer. Assume the scanner has at least one valid integer. This case and one other assertion are shown as examples. You should test more thoroughly.
@Test
public void testMaximumInScanner() {
Scanner stream0 = new Scanner("100");
assertEquals(0, myFun.rangeInScanner(stream0));
Scanner stream1 = new Scanner("1 2 7 3 5");
assertEquals(6, myFun.rangeInScanner(stream1));
}
__________________________________________________________________________________
8) public int numberOfPairs(String[] array)
Return the number of times a pair occurs in array. A pair is any two String values that are equal (case sensitive) in consecutive array elements. The array may be empty or have only one element. In both of these cases, return 0.
numberOfPairs( {"a", "b", "c" } ) returns 0
numberOfPairs( {"a", "a", "a"} ) returns 2
numberOfPairs( {"a", "a", "b", "b" } ) returns 2
numberOfPairs( { } ) returns 0
numberOfPairs( {"a"} ) returns 0
___________________________________________________________________________________
9) public boolean sumGreaterThan(double[] array, double sum)
Given a filled array of double array elements, return true if the sum of all array elements is greater than sum. The array may be empty. If so, return false. Sum may be negative.
sumGreaterThan( { 1.1, 2.2, 3.3 }, 4.0) returns true
sumGreaterThan( { 1.1, 2.2, 3.3 }, 6.6) returns false
sumGreaterThan( { }, -1.0) returns false
__________________________________________________________________________________
10) // Precondition: scanner has valid doubles
public double[] stats(Scanner scanner)
Given a Scanner of double values, return an array of capacity three that has the maximum value in the Scanner as the value in result[0], the minimum value as the second value in result[1], and the average as the third value in result[2]. The following assertions must pass:
@Test
public void testStats() {
Scanner scanner = new Scanner("90.0 80.0 70.0 68.0");
double[] result = myFun.stats(scanner);
assertEquals(3, result.length); // The capacity is always 3
assertEquals(90.0, result[0], 0.1); // The maximum is at index 0
assertEquals(68.0, result[1], 0.1); // The minimum is at index 1
assertEquals(77.0, result[2], 0.1); // The average is at index 2
}
You may assume the Scanner contains only valid double literals and there is at least one double in the argument. Do not test for empty arrays (we don't).
___________________________________________________________________________________
11) // Precondition: scanner has valid ints in the range of 0..10
public int[] frequency(Scanner scanner)
Given a Scanner constructed with a String containing a stream of integers in the range of 0..10 (like quiz scores), return an array of 11 integers where the first value (at index 0) is the number of 0s in the Scanner, the second value (at index 1) is the number of ones on the Scanner, and the 11th value (at index 10) is the number of tens in the Scanner. The following assertions must pass.
@Test
public void testFrequencyWithOneZeroTwoOnesTwoTwosTwoFiveAndNoThreesOrFours() {
Scanner scanner = new Scanner("5 0 1 2 1 5 2");
int[] result = myFun.frequency(scanner);
assertEquals(11, result.length);
assertEquals(1, result[0]); // There was 1 zero
assertEquals(2, result[1]);
assertEquals(2, result[2]);
assertEquals(0, result[3]);
assertEquals(0, result[4]);
assertEquals(2, result[5]); // There were 2 5s
for(int score = 6; score ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- strings ecology lab
- csci 515 introduction to c programming spring 2000
- handling of arrays strings and other data structures
- valdosta state university
- chapter 2 the basics of c programming
- california state university northridge
- note 2 string and file reading and writing
- ten unrelated methods
- homework 4 strings arrays and malloc
- strings in c
Related searches
- educational methods of teaching
- top ten retirement planning companies
- ten year mortgage calculator
- top ten argumentative essay topics
- top ten blood pressure medications
- top ten stocks
- top ten personal loan lenders
- top ten worst cars to buy
- methods of monitoring and evaluation
- top ten undergraduate business schools
- top ten stocks to buy
- methods of teaching pdf