Question

Write a test class to test the following methods in the java.lang.String class: ○ length ○...

Write a test class to test the following methods in the java.lang.String class:

○ length

○ charAt

○ substring

○ indexOf

Thank you in advance.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter long string: ");
    String s = sc.nextLine();

    System.out.println("Length of string: "+s.length());
    System.out.println("Char at index 0 is : "+s.charAt(0));
    System.out.println("Sub string from index 1 to 3: "+s.substring(1,3));
    System.out.println("indexOf of char i is : "+s.indexOf('i'));

  }
}

Add a comment
Know the answer?
Add Answer to:
Write a test class to test the following methods in the java.lang.String class: ○ length ○...
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
  • Write Java Programs to perform the following: 1. To create a new String object 2. To...

    Write Java Programs to perform the following: 1. To create a new String object 2. To find the length of the string 3. Concatenation operator 4. To display the content in String array 5. A complete Java Program that incorporates the following String methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length() e. compareTo(s1)) methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length()) e. compareTo(s1)) f. 51.substring(4)) g. s2.substring(2,10)) h. s1.indexOf('g')) i....

  • The String class has several methods that perform operations on the data or value associated with...

    The String class has several methods that perform operations on the data or value associated with the String. Some of those methods are trim, charAt, substring, and indexOf Which of the following statements would be INCORRECT in Java? "Gus Ikeji".trim() would be "Gus Ikeji" Given: String name="John Doe"; name.charAt(1) would be 'o' Given: String name="John Doe"; name.substring(2, 6) would be "hn D" Given: String name="John Doe"; name.indexOf("ohn") would be 1 None of the above

  • Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable...

    Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable string. This class represents a character array and provide functionalities need to do basic string processing. It contains an array of characters, representing a textual string. Overview In Java, Strings are a great device for storing arrays of characters. One limitation of Strings in Java, however (there is always at least one), is that they are immutable (ie. they cannot be changed once created)....

  • 1.This is a problem from our midterm. HOWEVER, you must use functions to solve the problem...

    1.This is a problem from our midterm. HOWEVER, you must use functions to solve the problem this time.(using Java please!) Write a program that prompts the user to enter two strings s1 and s2 and reports whether s2 is a substring of s1. The matching should be case-sensitive. The ONLY String class methods you are allowed to use is charAt(index) and length().

  • Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double...

    Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double variable that holds the rooms length(in feet). Breadth: The double variable that holds the rooms breadth(in feet). Height: Double variable that holds rooms height in feet. Color: String object that holds the color you want the room to be painted with. The class should have a constructor to initialize the fields for a particular instance (or object) of the class as given: All double...

  • Write a Python class, Circle, constructed by a radius and two methods which will compute the...

    Write a Python class, Circle, constructed by a radius and two methods which will compute the area and the perimeter of a circle. Include the constructor and other required methods to set and get class attributes. Create instances of Circle and test all the associated methods. Write a Python class, Rectangle, constructed by length and width and a method which will compute the area of a rectangle. Include the constructor and other required methods to set and get class attributes....

  • Do the following: - Write and test a function that takes an array of doubles and...

    Do the following: - Write and test a function that takes an array of doubles and returns the average of the values in the array - Write and test a function that takes an array of doubles and returns number of values in the array that are above the average of the values in the array - Write and test a function that takes an array of Strings and returns number of values in the array that start with an...

  • ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring...

    ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. int indexOf(const string& s1, const string& s2) Write a test program that reads two strings and checks whether the first string is a substring of the second string.

  • Write the definition of a class, Swimming Pool, to implement its properties and methods. Your class...

    Write the definition of a class, Swimming Pool, to implement its properties and methods. Your class should have the instance variables to store: length (in meters), width (in meters), depth (in meters), rate (in liters per minute) at which the water is filling the pool, and rate (in liters per minute) at which the water is draining from the pool. This class’s functions are expected to do the following: determine the amount of water needed to fill an empty or...

  • Write a class StringsAndThings. The class has one instance variable of type String and a constructor...

    Write a class StringsAndThings. The class has one instance variable of type String and a constructor with a parameter to initialize the instance variable. The parameter could contain any characters, including letters, digits, spaces, special characters (+, -, $, @,...), and punctuation marks. Write methods: countNonLetters - that will count how many of the characters in the string are not letters. You can use Character's isLetter method. moreVowels - which returns true if the String parameter has more vowels than...

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