Question
I need help fixing my java code from line 60 down. package mini1; import java.util.Scanner; /** * Utility class with some loop practice problems. */
I need help fixing my java code from line 60 down.
package mini1;
import java.util.Scanner;
/** * Utility class with some loop practice problems. */ public class LoopaholicsAnonymous {
/** * Private constructor prevents instantiation. */ private LoopaholicsAnonymous() { }
/** * Determines how many iterations of the following operation are required until * the condition (a * a + b * b) > 4 is reached: * *
* newA = a * a - b * b + x * newB = 2 * a * b + y * a = newA * b = newB *
* * where a and b are initially zero. For example, given x = -1 and y = 1, there * would be three iterations: *
- *
- initially: a = 0, b = 0 *
- 1: a = -1, b = 1 *
- 2: a = -1, b = -1 *
- 3: a = -1, b = 3 *
*
* If the condition (a * a + b * b) > 4 is not reached within * maxIterations, the method returns maxIterations. * * @param x given x value * @param y given y value * @param maxIterations maximum number of iterations to attempt * @return number of iterations required to get (a * a + b * b) > 4, or * maxIterations */ public static int findEscapeCount(double x, double y, int maxIterations) { // TODO double a = 0; double b = 0; int totalMaxIterations = maxIterations;
while (a * a + b * b <= 4 && maxIterations > 0) { double newA = a * a - b * b + x; double newB = 2 * a * b + y; a = newA; b = newB; maxIterations -= 1; }
return totalMaxIterations - maxIterations; }
/** * Returns the index for the nth occurrence of the given character in the given * string, or -1 if the character does not occur n or more times. This method is * case sensitive. Examples: *
- *
- findNth("mississippi", 's', 1) returns 2 *
- findNth("mississippi", 's', 4) returns 6 *
- findNth("mississippi", 's', 5) returns -1 *
- findNth("mississippi", 'w', 1) returns -1 *
* * @param s given string * @param ch given character to find * @param n occurrence to find * @return index of nth occurrence, or -1 if the character does not occur n or * more times */ public static int findNth(String s, char ch, int n) { // TODO return 0; }
/** * Returns the longest substring starting at the given position in which all * characters are the same. For example, *
- *
- getRun("bbbbbbcbc", 2) returns "bbbb" *
- getRun("abcd", 1) returns "b" *
* Returns an empty string if the given start index is out of range. * * @param s given string * @param start index at which to start the run * @return substring of all matching characters starting at the given position */ public static String getRun(String s, int start) { // TODO return null; }
/** * Given a string of text containing numbers separated by commas, returns true * if the numbers form an arithmetic sequence (a sequence in which each * value differs from the previous one by a fixed amount). For example, *
- *
- given "2, 4, 6, 8", the method returns true *
- given "-2, 5, 12, 19, 26", returns true *
- given "2, 4, 7", returns false *
- given "1, 2, 23skidoo", returns false *
* The method should return true for any string containing two or fewer numbers * and false for any invalid string. Note that there may or may not be spaces * after each comma. * * @param text a string of text containing numbers separated by commas * @return true if each number differs from the previous one by the same amount */ public static boolean isArithmetic(String text) { // TODO return false; }
/** * Determines whether the string target occurs as a subsequence of * string source where "gaps" are allowed between characters of * target. That is, the characters in target occur in * source in their given order but do not have to be adjacent. * (Pictured another way, this method returns true if target could * be obtained from source by removing some of the letters of * source.) This method is case sensitive. For example, *
- *
- containsWithGaps("hamburgers", "mug") returns true *
- containsWithGaps("hamburgers", "burrs") returns true *
- containsWithGaps("hamburgers", "hamburgers") returns true *
- containsWithGaps("hamburgers", "gum") returns false *
- containsWithGaps("hamburgers", "hamm") returns false *
- containsWithGaps("hamburgers", "") returns true *
* * @param source the given string in which to find the target characters * @param target the characters to be found * @return true if the characters in target can be found as a * subsequence in source, false otherwise */ public static boolean subsequenceWithGaps(String source, String target) { // TODO return false; }
/** * Separates s into two strings, each made of alternating characters from s, * except that runs of the same character are kept together. The two strings are * concatenated with a space between them to make a single returned string. If * the given string is empty, the returned string is a single space. For * example, *
- *
- takeApartPreservingRuns("abcdefa") returns "acea bdf" *
- takeApartPreservingRuns("aabcccddddefa") returns "aaccccea * bddddf" *
* * @param s any string * @return pair of strings obtained by taking alternating characters from s, * keeping runs of the same character together, concatenated with one * space between them into a single string */ public static String takeApartPreservingRuns(String s) { // TODO return null; }
/** * Returns the longest substring of consecutive characters in a given string * that are in ascending (nondecreasing) order. For example, *
- *
- longestAscendingSubstring("xyzabcdzyx") returns "abcdz" *
- longestAscendingSubstring("dcba") returns "d" *
- longestAscendingSubstring("bbbbaaaaa") returns "aaaaa" *
* If there are multiple runs of the same maximal length, the methods returns * the leftmost one. * * @param s any given string * @return longest nondecreasing substring */ public static String longestAscendingSubstring(String s) { // TODO return null; }
/** * Prints a pattern of 2n rows containing dashes and stars, as illustrated below * for n = 5. Note that the first row starts with exactly n - 1 spaces and the * middle rows have no spaces. * *
* *----**---- *---* *--- *--* *-- *-* *- ** * ** * *-* *- *--* *-- *---* *--- *----**---- *
* * @param n one-half the number of rows in the output */ public static void printStars(int n) { // TODO }
}
Here are the Errors I'm getting
PROBLEM: findNth("mississippi", 's', 1) should return 2. findNth("mississippi", 's', 1) should return 2. expected:<2> but was:<0>
PROBLEM: findNth("mississippi", 's', 4) should return 6. findNth("mississippi", 's', 4) should return 6. expected:<6> but was:<0>
PROBLEM: findNth("mississippi", 's', 5) should return -1. findNth("mississippi", 's', 5) should return -1. expected:<-1> but was:<0>
PROBLEM: findNth("mississippi", 'w', 1) should return -1. findNth("mississippi", 'w', 1) should return -1. expected:<-1> but was:<0>
PROBLEM: findNth("m", 'w', 5) should return -1. findNth("m", 'w', 5) should return -1. expected:<-1> but was:<0>
PROBLEM: findNth("computer science", 'e', 3) should return 15. findNth("computer science", 'e', 3) should return 15. expected:<15> but was:<0>
PROBLEM: findNth("missiSSippi", 's', 3) should return -1. findNth("missiSSippi", 's', 3) should return -1. expected:<-1> but was:<0>
PROBLEM: getRun("bbbbbbcbc", 2) should return "bbbb". expected:
PROBLEM: getRun("abcd", 1) should return "b". expected: but was:
PROBLEM: getRun("xxxxyyyy", 5) should return "yyy". expected:
PROBLEM: getRun("abcd", 4) should return an empty string. Expected empty string. expected:<> but was:
PROBLEM: Test for empty string: For isArithmetic(""), result should be true. expected:
PROBLEM: Test for a string containing one number: For isArithmetic("12"), result should be true. expected:
PROBLEM: Test for a string containing two numbers: For isArithmetic("12,13"), result should be true. expected:
PROBLEM: Test an arithmetic sequence from 1000000 to 1000000000 in increments of 500000. expected:
PROBLEM: Test for an arithmetic sequence:For isArithmetic("-2,5,12,19,26"), result should be true. expected:
PROBLEM: For target: "mug" and source: "hamburgers" result should be true expected:
PROBLEM: For target: "burrs" and source: "hamburgers" result should be true expected:
PROBLEM: For target: "hamburgers" and source: "hamburgers" result should be true expected:
PROBLEM: For target: "" and source: "hamburgers" result should be true expected:
PROBLEM: longestAscendingSubstring("xyzabcdzyx") should return "abcdz". expected:
PROBLEM: longestAscendingSubstring("dcba") should return "d". expected:
PROBLEM: longestAscendingSubstring("bbbbaaaaa") should return "aaaaa". expected:
PROBLEM: longestAscendingSubstring("acfpqxz") should return "acfpqxz". expected:
PROBLEM: longestAscendingSubstring on an empty string should return empty string. Expected empty string. expected:<> but was:
PROBLEM: longestAscendingSubstring("qxzgacf") should return "qxz". expected:
PROBLEM: takeApartPreservingRuns("abcdef") should return "ace bdf" expected:
PROBLEM: takeApartPreservingRuns("abcdefg") should return "aceg bdf" expected:
PROBLEM: takeApartPreservingRuns("aaabcdefff") should return "aaace bdfff" expected:
PROBLEM: takeApartPreservingRuns("axxxxbbbbyccz") should return "abbbbcc xxxxyz" expected:
PROBLEM: takeApartPreservingRuns("") should return string with just one space. expected:< > but was:
PROBLEM: Checking printStars(5). org.opentest4j.AssertionFailedError: Testing printStars(5), Output has only 0 lines, first missing line is "----**----".
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started