Question
This is Java. Hello, having troubles of where to start. Posted below is the criteria needed. Thank you in advance! Overview This is a short
This is Java. Hello, having troubles of where to start. Posted below is the criteria needed. Thank you in advance!
Overview
This is a short set of practice problems involving writing loops. You will write eight methods for the class mini1.FromLoopToNuts. 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.
You do not need arrays or ArrayLists for this assignment, though you will not be penalized for using them. None of the problems requires nested loops.
What do you mean, a private constructor?
Just put this declaration at the top of your class:
/**
* Private constructor disables instantiation.
*/ private FromLoopToNuts() { }
How do I make a string with a loop, as needed for eliminateRuns?
Start with an empty string and concatenate additional characters in each iteration.
Method Detail
threeInARow
public static int threeInARow(java.util.Random rand, int bound)
Counts how many random numbers must be generated to get three consecutive values that are the same, using the given Random object and the given bound. For example,threeInARow(new Random(42), 5) returns 7, because for the given Random object, the first seven values produced by nextInt(5) are 0, 3, 3, 4, 0, 0, 0.
Parameters:
rand - given random number generator
bound - upper bound on generated numbers
Returns:
how many numbers are generated in order to get three consecutive values that are the same
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:
tempA = a * a - b * b + x
tempB = 2 * a * b + y
a = tempA
b = tempB
where a and b are initially zero, and x and y are given parameters. For example, with x = 1 and y = 0.5, after one iteration a should be 1 and b should be 0.5. After two iterations ashould be 1.75 and b should be 1.5, at which point the method returns with value 2 since (1.75)2 + (1.5)2 = 5.3, which is greater than 4. If the condition (a * a + b * b) > 4 is not reached within maxIterations, the method just 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 maxIterations
countMatches
public static int countMatches(java.lang.String s, java.lang.String t)
Counts the number of positions in a pair of strings that have matching characters. The strings need not be the same length. For example, countMatches("abcde", "xbydcazzz") returns 2.
Parameters:
s - any string
t - any string
Returns:
number of positions in which the characters match in the two strings
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. Assume that the string does not contain whitespace before or after the comma.
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
eliminateRuns
public static java.lang.String eliminateRuns(java.lang.String s)
Returns a string with runs of consecutive characters removed. For example, eliminateRuns("abbbccbbd") returns the string "abcbd".
Parameters:
s - given string (possibly empty)
Returns:
string similar to s but with runs removed
differByOneSwap
public static boolean differByOneSwap(java.lang.String s, java.lang.String t)
Determines whether s and t differ by having exactly one pair of distinct adjacent characters reversed. This method is case sensitive. For example,
- differByOneSwap("banana", "banaan") is true
- differByOneSwap("banana", "banana") is false
- differByOneSwap("banana", "bnaaan") is false
- differByOneSwap("apple", "apple") is false
- differByOneSwap("abc", "ba") is false
Parameters:
s - given string
t - given string
Returns:
true if s and t are the same except for one pair of adjacent characters that are switched
countSubstrings
public static int countSubstrings(java.lang.String t, java.lang.String s)
Counts the number of times that one string occurs as a substring in another, without allowing the occurrences to overlap. For example:
- countSubstrings("aa", "aaaaa") returns 2
- countSubstrings("aa", "ababab") returns 0
Parameters:
t - string we are looking for ("target")
s - string in which we are looking ("source")
Returns:
number of times t occurs in s as a substring
countSubstringsWithOverlap
public static int countSubstringsWithOverlap(java.lang.String t, java.lang.String s)
Counts the number of times that one string occurs as a substring in another, allowing the occurrences to overlap. For example:
- countSubstringsWithOverlap("aa", "aaaaa") returns 4
- countSubstringsWithOverlap("aba", "ababab", true) returns 2
Parameters:
t - string we are looking for ("target")
s - string in which we are looking ("source")
Returns:
number of times t occurs in s as a substring, allowing overlap
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