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

%3Cp%3EPractice%20Question%201%3C%2Fp%3E%0A%3Cp%3E%3Cbr%20%2F%3E%3C%2Fp%3E%0A%3C

ID: 3552717 • Letter: #

Question

%3Cp%3EPractice%20Question%201%3C%2Fp%3E%0A%3Cp%3E%3Cbr%20%2F%3E%3C%2Fp%3E%0A%3Cp%3Ewrite%20a%20small%20method%20with%20the%20following%20signature%3A%3C%2Fp%3E%0A%3Cp%3E%3Cstrong%3Echar%20returnThirdChar%20(%20String%20word)%20%3C%2Fstrong%3E%3C%2Fp%3E%0A%3Cp%3E%26nbsp%3BThe%20method%20should%20take%20a%20string%20as%20a%20parameter%20and%20then%0Areturn%20the%203rd%20character%20from%20that%20string%20%2C%20it%20should%20also%20check%0Awether%20the%20length%20of%20the%20String%20is%20less%20than%20three%20and%20if%20so%20it%0Ashould%20print%20an%20error%20message%2C%20and%20return%20a%20'0'%20instead%20of%20the%0Athird%20character.%26nbsp%3B%3C%2Fp%3E%0A%3Cp%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%3EHint%3A%20Use%20Java%20Documentation%20to%20find%20out%20the%20names%20of%20the%0Amethods%20that%20you%20would%20be%20using%20and%20how%20to%20use%20it.%3C%2Fp%3E%0A

Explanation / Answer

/* package whatever; // don't place package name! */



/* Name of the class has to be "Main" only if the class is public. */

class Test

{

public static void main (String[] args)

{

// your code goes here

String s = "statistics";

returnThirdChar(s);

}

private char returnThirdChar(String s){

if(s.length()<3){

System.out.println("Error. String length less than 3");

return '0';

}else

return s.charAt(2);

}

}