Question: IN JAVA PLEASE AND THANKS :) Overview This is a short set of practice problems involving writing loops. You will write eight methods for the
IN JAVA PLEASE AND THANKS :)









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) Package minit Class Loopaholics Anonymous java.lang. Object mini 1. LoopaholicsAnonymous public class Loopaholics Anonymous extends java.lang.Object Method Summary All Methods Static Methods Concrete Methods Modifier and Type Method Description static int findEscapeCount(double x, double y, int maxIterations) static int findNth (java.lang.String s, char ch, int n) static java.lang. String getRun (java.lang.String s, int start) static boolean isArithmetic(java.lang. String text) longestAscendingSubstring(java.lang.String s) Determines how many iterations of the following operation are required until the condition (a*a+b*b) > 4 is reached: 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. Returns the longest substring starting at the given position in which all characters are the same. 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). Returns the longest substring of consecutive characters in a given string that are in ascending (nondecreasing) order. Prints a pattern of an rows containing dashes and stars, as illustrated below for n = 5. Determines whether the string target occurs as a subsequence of string source where "gaps" are allowed between characters of target. Separates sinto two strings, each made of alternating characters from s, except that runs of the same character are kept together. static java.lang.String static void printStars(int n) static boolean subsequencellithGaps (java.lang.String source, java.lang.String target) takeApartPreservingRuns (java.lang.String s) static java.lang. String Methods inherited from class java.lang.Object equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait Method Detail findEscapeCount public static int findEscapeCount (double x, double y, int maxIterations) Determines how many iterations of the following operation are required until the condition (a*a+b*b) > 4 is reached: new = a * a - b*b + x newB = 2 * a * b + y a = news b = news 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. Parameters: x-given x value y-given y value maxIterations - maximum number of iterations to attempt Returns: number of iterations required to get (a *a+b* b) > 4, or maxterations findNth public static int findth(java.lang.String s, char ch, int n) 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 findth("mississippi", 's', 4) returns 6 findNth("mississippi", 's', 5) returns -1 findNth("mississippi", W, 1) returns -1 Parameters: S-given string ch - given character to find n- occurrence to find Returns: index of nth occurrence, or -1 if the character does not occur n or more times getRun public static java.lang.String getRun (java.lang. String s, int start) 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. Parameters: s - given string start - index at which to start the run Returns substring of all matching characters starting at the given position isArithmetic public static boolean isArithmetic(java.lang.String text) 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. You can assume there are no spaces in the string. Parameters: text - a string of text containing numbers separated by commas Returns: true if each number differs from the previous one by the same amount subsequence With Gaps public static boolean subsequenceWithGaps (java.lang.String source, java.lang.String target) 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, contains With Gaps('hamburgers", "mug") returns true containsWith Gaps("hamburgers", "burts") returns true containsWith Gaps('hamburgers", "hamburgers") returns true contains With Gaps('hamburgers", "gum") returns false contains With Gaps('hamburgers", "hamm") returns false contains With Gaps("hamburgers","") returns true . Parameters: source - the given string in which to find the target characters target the characters to be found Returns true if the characters in target can be found as a subsequence in source, false otherwise takeApartPreservingRuns public static java.lang. String takeApartPreservingRuns(java.lang.String s) 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 baf" takeApartPreservingRuns ("aabcccddddefa") returns "aacccea bddddf" Parameters: s-any string Returns: 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 longestAscending Substring public static java.lang. String longestAscendingSubstring(java.lang.String s) 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. Parameters: s-any given string Returns longest nondecreasing substring printStars public static void printStars(int n) Prints a pattern of an 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. Parameters: n-one-half the number of rows in the output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
