Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need help writing out java methods for the following.... Returns a string obta

ID: 3675599 • Letter: I

Question

i need help writing out java methods for the following....

Returns a string obtained by alternating characters from two given strings, starting with the first character of the first string. If one of the strings is longer, the extra characters are appended to the end of the result. Runs of the same character are kept together. For example

given "abc" and "xyz", returns "axbycz"

given "abcde" and "xyz", returns "axbyczde"

given "abcde" and "abc", returns "aabbccde"

given "abbcde" and "xxxyzzzz", returns "axxxbbyczzzzde"

Given a string, prints n lines of output as illustrated below, where n is the length of the string. As an example, given the string "banana", the output should be:

Returns the length of the longest consecutive run of the same character in a string s. For example, for "baaanaannnnaa", the method returns 4, since there is a run of four 'n' characters. For "banana" the method returns 1.

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.

Explanation / Answer

/**The method interleaveWithRuns that takes two strings s and t as input arguments
   * and combines the interleaving characters and append any extra
   * characters at end of the string and retuns the string vlaue*/
   private static String interleaveWithRuns(String s, String t)
   {
       String result="";
      
       int size=s.length();
      
       //check the minimum string length
       if(s.length()>t.length())
           size=t.length();
      
       //Run for loop to interleave the character one from s and one from t
       for (int i = 0; i < size; i++)
       {
           result+=String.valueOf(s.charAt(i))+String.valueOf(t.charAt(i));
       }
      
       //Append extra to the end of the result
       if(s.length()>t.length())
           result+=s.substring(size, s.length());
       else
           result+=t.substring(size, t.length());
      
       return result;
      
   }
  

------------------------------------------------------------------------------------------------------------------------
//Test program for interleaveWithRuns(String
/**java program InterleaveWithRunsTester that test
* the method InterleaveWithRuns */
//InterleaveWithRunsTester.java
public class InterleaveWithRunsTester
{
   public static void main(String[] args)
   {
  
       String s="abc";
       String t="xyz";
      
       System.out.println(interleaveWithRuns(s,t));
      
   }

  
   /**The method interleaveWithRuns that takes two strings s and t as input arguments
   * and combines the interleaving characters and append any extra
   * characters at end of the string and retuns the string vlaue*/
   private static String interleaveWithRuns(String s, String t)
   {
       String result="";
      
       int size=s.length();
      
       //check the minimum string length
       if(s.length()>t.length())
           size=t.length();
      
       //Run for loop to interleave the character one from s and one from t
       for (int i = 0; i < size; i++)
       {
           result+=String.valueOf(s.charAt(i))+String.valueOf(t.charAt(i));
       }
      
       //Append extra to the end of the result
       if(s.length()>t.length())
           result+=s.substring(size, s.length());
       else
           result+=t.substring(size, t.length());
      
       return result;
      
   }
  
  
}

Sample Output:
axbycz

---------------------------------------------------------------------------------------------------------------------------------------------------------
/**The meethod triangleWord that takes a string as input argument
* and prints a word in triangle format*/

public static void triangleWord (java.lang.String s)
   {
     
       for (int i=s.length()-1; i >= 0; --i)
        {          
           //Create a spaceCount string with the spaces half with string replace
           //with empty
            String spaceCount = new String(new char[i/2]).replace("", " ");
            //print the string
            System.out.println(spaceCount + s.substring(i));
        }
   }


//Test program for triangleWord
//TriangleWordTester.java
/**java program triangleWord that test
* the method triangleWord */
//InterleaveWithRunsTester.java
public class TriangleWordTester
{
   public static void main(String[] args)
   {
  
      
       String s="banana";
      
       triangleWord(s);
      
   }

   /**The meethod triangleWord that takes a string as input argument
   * and prints a word in triangle format*/
   public static void triangleWord (java.lang.String s)
   {
     
       for (int i=s.length()-1; i >= 0; --i)
        {          
           //Create a spaceCount string with the spaces half with string replace
           //with empty
            String spaceCount = new String(new char[i/2]).replace("", " ");
            //print the string
            System.out.println(spaceCount + s.substring(i));
        }
   }
  
}

Sample Output:

a
na
ana
nana
anana
banana


----------------------------------------------------------------------------------------------------------------------------------------------------
// Method : int longestRun(String
/**The method that taks string s and returns the maximum
   * count of the letter ,'n' in the string*/
   private static int longestRun(String s)
   {
       //Set count and max to 1
       int count   = 1;
       int max     = 1;      
       for(int i = 1; i < s.length(); i++)
       {
           //Set count by incrementing the count by 1 and
           //by checking terneary operaor otherwise set to 0
           count = (s.charAt(i) == 'n') ? (count + 1) : 0;
           if (count > max)           
               max = count;
          
       }
       //return max count
       return max;
   }

/**java program longestRun that test
* the method longestRun that prints the maximum occurence
* of letter n count */

//LongestRunTester.java
public class LongestRunTester
{
   public static void main(String[] args)
   {
       String s="baaanaannnnaa";
       System.out.println(longestRun(s));
   }

   /**The method that taks string s and returns the maximum
   * count of the letter ,'n' in the string*/
   private static int longestRun(String s)
   {
       //Set count and max to 1
       int count   = 1;
       int max     = 1;      
       for(int i = 1; i < s.length(); i++)
       {
           //Set count by incrementing the count by 1 and
           //by checking terneary operaor otherwise set to 0
           count = (s.charAt(i) == 'n') ? (count + 1) : 0;
           if (count > max)           
               max = count;
          
       }
       //return max count
       return max;
   }
}

Sampel output:
4

-------------------------------------------------------------------


/**The java program ArithematicSequanceTester that tests
* the method isArithmetic that returns true if the digits
* in the text is in the arithematic mean otherwise returns false */
//ArithematicSequanceTester.java
public class ArithematicSequanceTester
{
   public static void main(String[] args)
   {
       String text="2,4,7";
              
              
       System.out.println(isArithmetic(text));
   }
      
   public static boolean isArithmetic(String text)
   {
       String digits[]=text.split(",");
      
       int diff=Integer.parseInt(String.valueOf(digits[1]))
       -Integer.parseInt(String.valueOf(digits[0]));
      
      
       for (int i = 1; i < digits.length; i++)
       {
           if((Integer.parseInt(String.valueOf(digits[i]))
                   -Integer.parseInt(String.valueOf(digits[i-1])))!=diff)
                   return false;
                   else
                       continue;
       }
       return true;
      
      
   }
}


sampel output

false