Question: IN JAVA PLEASE AND THANKS :) SKELETON CODE: package mini1;import java.util.Scanner;/** * Utility class with some loop practice problems. */public class LoopaholicsAnonymous{ /** * Private

IN JAVA PLEASE AND THANKS :)

IN JAVA PLEASE AND THANKS :) SKELETON CODE: package mini1;import java.util.Scanner;/** *Utility class with some loop practice problems. */public class LoopaholicsAnonymous{ /** *Private constructor prevents instantiation. */ private LoopaholicsAnonymous() {} /** * Determines howmany iterations of the following operation * are required until the condition

SKELETON CODE:

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 return 0; } /** * 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 }} Overview This is a short set of practice problems involving writing loops. You will write eight methods for the class minil. LoopaholicsAnonymous. All of the methods are static, so your class will not have any instance variables (or any static variables, for that matter). There is a constructor, but it is declared private so the class cannot be instantiated. For details and examples see the online Javadoc. There is a skeleton of the class on Canvas. If you use the skeleton code, be sure you put it in a package called minil. If you are working on findEscapeCount, can you a. work by hand through the example calculation given in the Javadoc, and get the same answers? b. write a loop that has the correct loop body but always just repeats the steps, say, 5 times? c. write a condition to check that a2 + b2 operators.) c. modify your algorithm so that instead of returning the length of the longest run, it returns the starting index of the longest run? d. modify your algorithm so that instead of returning the starting index of the longest run, it returns the actual longest run as a String? If you are working on printStars, can you a. write a helper method that, given a number d, constructs and returns a String of d dashes? b. write a helper method that, given number d and a number n > d, returns a String of the correct form for one line of the output? (For example, given n= 5 and d = 3, return the 10-character string "---* *---" ) c. write an algorithm to just print out the number of dashes that should appear at the beginning of each line? (For example, if n = 5, you'd print 4, 3, 2, 1, 0, 0, 1, 2, 3, 4)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!