Java Code package edu.wit.cs.comp1050; //TODO: document this class public class
ID: 3873814 • Letter: J
Question
Java Code
package edu.wit.cs.comp1050;
//TODO: document this class
public class PA2b {
/**
* Error to supply if input is not positive
*/
public static final String ERR_VALUES = "Number of values must be positive.";
/**
* Returns true if the supplied array has a
* sequence of k consecutive values
*
* @param values input array
* @param k sequence length for which to search
* @return true if values has a consecutive sequence of at least k
*/
public static boolean hasConsecutive(int[] values, int k) {
return false;
}
/**
* Returns the length of the longest
* consecutive sequence in the supplied
* array
*
* @param values input array
* @return length of the longest consecutive value sequence in values
*/
public static int maxConsecutive(int[] values) {
// Hint: hasConsecutive could
// be very useful here
return 0;
}
/**
* Inputs an array of numbers
* and outputs the longest consecutive
* sequence of values
*
* @param args command-line arguments, ignored
*/
public static void main(String[] args) {
// Hint: useful methods and constants here
// - maxConsecutive
// - ERR_VALUES
}
}
JUnit Test:
package edu.wit.cs.comp1050.tests;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.security.Permission;
import edu.wit.cs.comp1050.PA2b;
import junit.framework.TestCase;
public class PA2bTestCase extends TestCase {
private static final String ERR_VALUES = "Number of values must be positive.";
private static final String P_NUM = "Enter the number of values: ";
private static final String P_VAL = "Enter the values: ";
@SuppressWarnings("serial")
private static class ExitException extends SecurityException {}
private static class NoExitSecurityManager extends SecurityManager
{
@Override
public void checkPermission(Permission perm) {}
@Override
public void checkPermission(Permission perm, Object context) {}
@Override
public void checkExit(int status) { super.checkExit(status); throw new ExitException(); }
}
@Override
protected void setUp() throws Exception
{
super.setUp();
System.setSecurityManager(new NoExitSecurityManager());
}
@Override
protected void tearDown() throws Exception
{
System.setSecurityManager(null);
super.tearDown();
}
private void _test(String in, String sNum, String sValues, String msg) {
final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
final String input = String.format("%s%n%s%n", sNum, sValues);
final String expected = TestSuite.stringOutput(new String[] {
in,
msg + "%n"
}, new Object[] {});
System.setIn(new ByteArrayInputStream(input.getBytes()));
System.setOut(new PrintStream(outContent));
try {
PA2b.main(new String[] { "foo" });
} catch (ExitException e) {}
assertEquals(expected, outContent.toString());
System.setIn(null);
System.setOut(null);
}
private void _testHasConsecutive(int[] a, int k, boolean expected) {
Boolean result = null;
try {
result = PA2b.hasConsecutive(a, k);
} catch (ExitException e) {}
assertEquals(expected, (boolean) result);
}
private void _testMaxConsecutive(int[] a, int expected) {
Integer result = null;
try {
result = PA2b.maxConsecutive(a);
} catch (ExitException e) {}
assertEquals(expected, (int) result);
}
public void testHasConsecutive() {
_testHasConsecutive(new int[] {}, -1, false);
_testHasConsecutive(new int[] {}, 0, false);
_testHasConsecutive(new int[] {}, 1, false);
_testHasConsecutive(new int[] {}, 2, false);
_testHasConsecutive(new int[] {}, 3, false);
_testHasConsecutive(new int[] {}, 4, false);
_testHasConsecutive(new int[] {1}, -1, false);
_testHasConsecutive(new int[] {1}, 0, false);
_testHasConsecutive(new int[] {1}, 1, true);
_testHasConsecutive(new int[] {1}, 2, false);
_testHasConsecutive(new int[] {1}, 3, false);
_testHasConsecutive(new int[] {1}, 3, false);
_testHasConsecutive(new int[] {1, 2}, -1, false);
_testHasConsecutive(new int[] {1, 2}, 0, false);
_testHasConsecutive(new int[] {1, 2}, 1, true);
_testHasConsecutive(new int[] {1, 2}, 2, false);
_testHasConsecutive(new int[] {1, 2}, 3, false);
_testHasConsecutive(new int[] {1, 2}, 4, false);
_testHasConsecutive(new int[] {2, 2}, -1, false);
_testHasConsecutive(new int[] {2, 2}, 0, false);
_testHasConsecutive(new int[] {2, 2}, 1, true);
_testHasConsecutive(new int[] {2, 2}, 2, true);
_testHasConsecutive(new int[] {2, 2}, 3, false);
_testHasConsecutive(new int[] {2, 2}, 4, false);
_testHasConsecutive(new int[] {1, 2, 1}, -1, false);
_testHasConsecutive(new int[] {1, 2, 1}, 0, false);
_testHasConsecutive(new int[] {1, 2, 1}, 1, true);
_testHasConsecutive(new int[] {1, 2, 1}, 2, false);
_testHasConsecutive(new int[] {1, 2, 1}, 3, false);
_testHasConsecutive(new int[] {1, 2, 1}, 3, false);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, -1, false);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 0, false);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 1, true);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 2, true);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 3, true);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 4, true);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 5, false);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 6, false);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 7, false);
_testHasConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 8, false);
}
public void testMaxConsecutive() {
_testMaxConsecutive(new int[] {}, 0);
_testMaxConsecutive(new int[] {1}, 1);
_testMaxConsecutive(new int[] {1, 2}, 1);
_testMaxConsecutive(new int[] {2, 2}, 2);
_testMaxConsecutive(new int[] {1, 2, 1}, 1);
_testMaxConsecutive(new int[] {3, 3, 5, 5, 5, 5, 4}, 4);
_testMaxConsecutive(new int[] {3, 4, 5, 5, 6, 5, 5, 4, 5}, 2);
}
public void _testBad(String n) {
_test(P_NUM, n, "", ERR_VALUES);
}
public void _testGood(String n, String v, String ans) {
_test(P_NUM+P_VAL, n, v, "The maximum length of consecutive values is " + ans + ".");
}
public void testProgram() {
_testBad("-1");
_testBad("0");
_testGood("1", "1", "1");
_testGood("2", "1 2", "1");
_testGood("2", "2 2", "2");
_testGood("3", "1 2 1", "1");
_testGood("7", "3 3 5 5 5 5 4", "4");
_testGood("9", "3 4 5 5 6 5 5 4 5", "2");
}
}
Problem b (PA2b.java) You are to write a program that inputs an array of integers and then outputs the length of the longest sequence of repeating values. For example... Enter the number of values: 7 Enter the values: 3 3 5555 4 The maximum length of consecutive values is 4 Enter the number of values: 9 Enter the values: 3 4 5 5 6554 5 The maximum length of consecutive values is 2. The program will first prompt the user for integers to store an in array. This array will then be passed to a method in order to ascertain the longest repeating sequence. Before attempting the full program, you will first implement a method that checks whether a sequence of a certain length exists in an array. You should complete this method first, then proceed to the maximum length method, and then finally the main method.Explanation / Answer
Hi friend, Please find my implementation.
Please let me know in case of any issue.
//TODO: document this class
public class PA2b {
/**
* Error to supply if input is not positive
*/
public static final String ERR_VALUES = "Number of values must be positive.";
/**
* Returns true if the supplied array has a
* sequence of k consecutive values
*
* @param values input array
* @param k sequence length for which to search
* @return true if values has a consecutive sequence of at least k
*/
public static boolean hasConsecutive(int[] values, int k) {
int count = 1;
for(int i=1; i<values.length; i++) {
if(count == k)
return true;
if(values[i] == values[i-1])
count++;
else
count = 0;
}
return false;
}
/**
* Returns the length of the longest
* consecutive sequence in the supplied
* array
*
* @param values input array
* @return length of the longest consecutive value sequence in values
*/
public static int maxConsecutive(int[] values) {
//Hint: hasConsecutive could
//be very useful here
int max = 1;
for(int i=2; i<=values.length; i++) {
if(hasConsecutive(values, i))
max = i;
}
return max;
}
/**
* Inputs an array of numbers
* and outputs the longest consecutive
* sequence of values
*
* @param args command-line arguments, ignored
*/
public static void main(String[] args) {
//Hint: useful methods and constants here
//- maxConsecutive
//- ERR_VALUES
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.