Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string), findFirstP(finds first p in a string)) in java language.
public class simpleLoops
{
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println(longestRun("aabbbccd"));
System.out.println("Expected 3");
System.out.println(longestRun("aaa"));
System.out.println("Expected 3");
System.out.println(longestRun("aabbbb"));
System.out.println("Expected 4");
int count = countP("Mississippi");
System.out.println(count);
int result = findLastP("Mississippi");
System.out.println(result);
result = findFirstP("stop");
System.out.println(result);
result = findFirstP("xxxyyyzzz");
System.out.println(result);
}
/**
* Returns the number of P's in a string.
* @param s
* the string to examine
* @return
* number of P's in s
*/
private static int countP(String s)
{
int count = 0;
int i = 0;
while (i < s.length())
{
if (isLetterP(s.charAt(i)))
{
count += 1;
}
i += 1;
}
return count;
}
/**
* Returns the index of the last P in a string, or -1 if the
* string contains no P's.
* @param s
* the string to examine
* @return
* index of the last P, or -1
*/
private static int findLastP(String s)
{
int i = 0;
while (i < s.length())
{
// start at the end of the string
int index = s.length() - i - 1;
if (isLetterP(s.charAt(index)))
{
return index;
}
index = index - 1;
}
// didn't find a P
return -1;
}
/**
* Returns the index of the first P in a string, or -1 if the
* string contains no P's.
* @param s
* the string to examine
* @return
* index of the first vowel, or -1
*/
private static int findFirstP(String s)
{
int i = 0;
while (i <= s.length())
{
if (isLetterP(s.charAt(i)))
{
return i;
}
i = i + 1;
}
// didn't find a P
return -1;
}
public static int longestRun(String s)
{
int count = 1;
int max = 1;
// start with the first character, see how long a run there
is
char current = s.charAt(0);
for (int i = 1; i < s.length() - 1; i += 1)
{
char c = s.charAt(i);
if (c == current)
{
// matches the 'current' character, add 1
count += 1;
}
else
{
// that was the end of the run; if it was a longer run, make that
the max
if (count > max)
{
count = max;
}
// start counting a new run of a different character
current = c;
}
}
// this should be the length of the longest run we found
return max;
}
/**
* Returns true if the given character is the letter "P"
(lowercase
* or uppercase), false otherwise
* the character to check
* @return
* true if the given character is a "P", false otherwise
*/
private static boolean isLetterP(char ch)
{
return ch == 'p' || ch == 'P';
}
}
ANSWER:-
CODE:-
public class SimpleLoops
{
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println(longestRun("aabbbccd"));
System.out.println("Expected
3");
System.out.println(longestRun("aaa"));
System.out.println("Expected
3");
System.out.println(longestRun("aabbbb"));
System.out.println("Expected
4");
int count =
countP("Mississippi");
System.out.println(count);
int result =
findLastP("Mississippi");
System.out.println(result);
result =
findFirstP("stop");
System.out.println(result);
result =
findFirstP("xxxyyyzzz");
System.out.println(result);
}
/**
* Returns the number of P's in a string.
* @param s
* the string to examine
* @return
* number of P's in s
*/
private static int countP(String s)
{
int count = 0;
int i = 0;
while (i < s.length())
{
if
(isLetterP(s.charAt(i)))
{
count += 1;
}
i += 1;
}
return count;
}
/**
* Returns the index of the last P in a string, or -1
if the
* string contains no P's.
* @param s
* the string to examine
* @return
* index of the last P, or -1
*/
private static int findLastP(String s)
{
int index = s.length() - 1;
while (index >= 0)
{
// start at the
end of the string
if
(isLetterP(s.charAt(index)))
{
return index;
}
index = index -
1;
}
// didn't find a P
return -1;
}
/**
* Returns the index of the first P in a string, or -1
if the
* string contains no P's.
* @param s
* the string to examine
* @return
* index of the first vowel, or -1
*/
private static int findFirstP(String s)
{
int i = 0;
while (i < s.length())
{
if
(isLetterP(s.charAt(i)))
{
return i;
}
i = i + 1;
}
// didn't find a P
return -1;
}
public static int longestRun(String s)
{
int count = 1;
int max = 1;
// start with the first character,
see how long a run there is
char current = s.charAt(0);
for (int i = 1; i < s.length();
i++)
{
char c =
s.charAt(i);
if (c ==
current)
{
// matches the 'current' character, add 1
count += 1;
}
else
{
// that was the end of the run; if it was a
longer run, make that the max
if (count > max)
{
max = count;
}
count = 1;
// start counting a new run of a different
character
current = c;
}
}
if(count > max)
max =
count;
// this should be the length of the
longest run we found
return max;
}
/**
* Returns true if the given character is the letter
"P" (lowercase
* or uppercase), false otherwise
* the character to check
* @return
* true if the given character is a "P", false
otherwise
*/
private static boolean isLetterP(char ch)
{
return ch == 'p' || ch ==
'P';
}
}
CHANGES:-
IN longestRun method,
1.max = count; -> you need to assign count to max. But you assigned max to count;
2. count = 1; -> you need to reset count to 1 when old run ends.
findLastP method:-
1. int index = s.length()-1;
you just need to start from end of the string. no need of i variable.
NOTE:- If you have any doubts,please comment below.Please give positive rating.THUMBS UP.
THANK YOU!!!!
OUTPUT:-

Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string),...
Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example: > Methods.printWithSpaces("method") m e t h o d You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically public static void printVertical(String s) { for (int i = 0; i < s.length(); i++) { char c =...
JAVA:
Run length encoding is a simple form of data compression. It
replaces long sequences of a repeated value with one occurrence of
the value and a count of how many times to repeat it. This works
reasonably well when there are lots of long repeats such as in
black and white images. To avoid having to represent non-repeated
runs with a count of 1 and the value, a special value is often used
to indicate a run and everything...
Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /** * Takes in a string containing a first, middle and last name in that order. * For example Amith Mamidi Reddy to A. M. Reddy. * If there are not three words in the string then the method will return null * @param name in...
Consider the encryption code Encryption Code true false - stringContains is an encryption algorithm that can be decrpyted true false - Running reverseString twice will have no effect true false - Running incLetters(s,4) then incLetters(s,-4) will have no effect true false - using maxDigit as an encryption algorighm, it can be decrypted USE CODE BELOW: package encryption; import java.util.Scanner; public class Encryption { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); String password, encryptedPassword, salt; int increment;...
Given the following code: public static void foo3(String s) { if (s.length() >0) { System.out.print(s.charAt(s.length() -1)); foo3(s.substring(0, s.length() -1)); } } What is the output of: foo3(“”); 2, You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n) //line 9 { if (n = = 0) return 1; else System.out.println(n* foo(n-1) ); } //line 15 At compile time, you get the following error: Text.java: 15: missing return statement } ...
Objectives
Problem solving using arrays and ArrayLists. Abstraction.
Overview
The diagram below illustrates a banner constructed from
block-letters of size 7. Each block-letter is
composed of 7 horizontal line-segments of width 7 (spaces
included):
SOLID
as in block-letters F, I, U, M, A, S and the
blurb RThere are six distinct line-segment
types:
TRIPLE
as in block-letter M
DOUBLE
as in block-letters U, M, A
LEFT_DOT
as in block-letters F, S
CENTER_DOT as
in block-letter...
10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println ()
10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...
I need this program converted to c++ Please help if you can import java.util.*; // Add two arrays of the same size (size) // Each array is a representation of a natural number // The returned array will have the size of (size + 1) elements public class Fibonacci { private static String arrToString(int[] arr) { String s = ""; for (int i = 0; i < arr.length; i++) { s = s + arr[i]; }...
Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...
The code below accepts and evaluates an integer expression with the following operators: +, _, *, and /. Your task is to modify it to include the % remainder operator that has the same precedence as * and /. No need to rewrite the entire program, just insert the needed statements. import java.util.Stack; public class EvaluateExpression { public static void main(String[] args) { // Check number of arguments passed if (args.length != 1) { System.out.println( "Usage:...