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

This is for intro Java, please use basics (if/else, char/num types, loops, class

ID: 3912070 • Letter: T

Question

This is for intro Java, please use basics (if/else, char/num types, loops, classes, objects, GUIs, methods)

Given the String "Java versus C++ Programming". Using String API

a. Print the 5th index and 17th index of this string. Use String API method for this class.

Indexing starts at 0, so 5th index is 0 1 2 3 4 5th index.. Similarly for index 17.

b. Print sub-string of this string at position 5. Use String API method for this class. BTW Many get this incorrect - do not just use any API, Read all API on substring in String class and then ese substring method which uses starting position ONLY. I am trying to learn and understand API

Explanation / Answer

You may declare the string either by:

a. You may use the charAt() method and pass the index directly to access it.

System.out.println( s.charAt(5) ); // Prints character at index 5, printing v

System.out.println( s.charAt(17) ); // Prints character at index 17, printing r

b. To print a substring from any position use the substring() method. Passing one argument would print the subtring starting at it till the end of the string. A second argument however specifies the ending position as well.

System.out.println( s.substring(5) ); // This would print, versus C++ Programming

Full Program:

public class Solution {

    public static void main(String[] args) {
        String s = new String("Java versus C++ Programming");
        System.out.println( s.charAt(5) ); // Prints character at index 5
        System.out.println( s.charAt(17) ); // Prints character at index 17
        System.out.println( s.substring(5) ); // Prints the substring
    }
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote