Question

in Java please (a) Write a static method abbreviate( ) which is passed a String s...

in Java please

(a) Write a static method abbreviate( ) which is passed a String s and an int max. The method returns a new String which contains the content of s abbreviated to at most max characters, but where the last three characters are ellipses (i.e., the characters . . .).

For example, if s is "Too bad Spongebob’s not here to enjoy Spongebob not being here. ---Squidward." and max is 10, the method returns the 10-character String "Too bad..." If s is shorter than max characters, return the original String. If max is less than 3 and less than the length of s, return the empty String.

(b) Write a main method that calls the abbreviate method twice, and prints the result to the screen each time. The first call should abbreviate the String "How now, brown cow?" down to 10 characters. The second call should abbreviate the result of the first abbreviation down to 5 characters.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Main{

public static String abbreviate(String s, int max) {

if (s.length() <= max) {

return s;

}

return s.substring(0, max - 3) + "...";

}

public static void main(String[] args) {

System.out.println(abbreviate("How now, brown cow?", 10));

System.out.println(abbreviate("How now, brown cow?", 5));

}

}

Add a comment
Know the answer?
Add Answer to:
in Java please (a) Write a static method abbreviate( ) which is passed a String s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • programming language is JAVA: We wish to implement a method  String insert(char c, String s) that returns...

    programming language is JAVA: We wish to implement a method  String insert(char c, String s) that returns a string with c inserted in the correct position in already sorted String s. To do so we will implement insert as follows: static  String insert(char c, String s) { return insertHelper(c, "", s); } static  String insertHelper(char c, String left, String right) {} // strip leading characters from right, & append to left 'till insertion point found. Now return left + c + right. For...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • java /* Q2 (10 pts): Write a method called method that accepts an integer parameter *...

    java /* Q2 (10 pts): Write a method called method that accepts an integer parameter * * * * and returns a sum of the first n terms of the sequence. * In other words, the method should generate the following sequence: 1 + 1/2 + 1/3 + 1/4 + ... 1/n * For example, method2(2) will return 1.5 since 1+1/2 = 1.5 * method2 (15) will return 3.3182289932289937 * You may assume that the parameter n is nonnegative. */...

  • 4. Write a static method named ConcatStrings that is passed any number of parameters of   type...

    4. Write a static method named ConcatStrings that is passed any number of parameters of   type String, and returns a single string that is the concatenation of the strings passed.   (4 pts.)   5. Write a static method named ConcatObjects that is passed any number of parameters of   type Object, and returns a single string that is the concatenation of the result of the call to   toString() for each of the objects. (4 pts.)   6. Write a class named Triplet that...

  • 3. Write a method buildString which takes a 2D array of characters (char), and returns a...

    3. Write a method buildString which takes a 2D array of characters (char), and returns a string that is built from all the characters in that array. For example, the following method call should return "hello!" buildString( new char[] [ 'h', 'e',

  • Please write this in java Write one static method to print each character in the output....

    Please write this in java Write one static method to print each character in the output. There should be methods to print: H, E, L, O, (comma), W, R, D and (exclamation point). The method to print H should be called printH() and the method to print the comma should be called printComma(). Make sure to separate characters with a new line in the method (except the comma) as shown on right. Write a method called printHelloWorld() to call the...

  • Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String a...

    Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String argument. The method will return a new String by removing the logest segment that contains consequtively repeating characters. public static void main(String []args){ String s=”1111222223333311111111444455552222”; String res= removeLongestRepeatingSegment(s); will return “11112222233333444455552222” } public static String removeLongestRepeatingSegment(String s){ --------------- Q2)15p)Given a list of objects stored (in ascending order) in a sorted linked list, implement member method which performs search as efficiently as possible for an object based on a key....

  • 5. Write a static method "f(n)" in the space provide, that returns O if n is...

    5. Write a static method "f(n)" in the space provide, that returns O if n is even, and if n is odd, returns 1 or -1 according as n is greater than or less than 0. importjava.util.Scanner; public class Q_05 Your "f(n)" method: public static void main(String args[]) mana int n; Scanner input = new Scanner(System.in); System.out.print("Please enetrn: "); n=input.nextInt(); System.out.println(f(n)); "Method f(n)" 7. Write a static method "max" in the space provide, that returns the maximum value from 3...

  • Write a static method called printWithSpaces that takes a String as its parameter and prints the...

    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 =...

  • Write a recursive method,     public static Boolean isPalindrome(String s) That returns true if s is...

    Write a recursive method,     public static Boolean isPalindrome(String s) That returns true if s is a palindrome, else false.


ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT