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

(1)Assume that sentence is a variable of type String that has been assigned a va

ID: 3655153 • Letter: #

Question

(1)Assume that sentence is a variable of type String that has been assigned a value. Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared, secondWord , also of type String. Write the statements needed so that the second word of the value of sentence is assigned to secondWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord . (2) Given a String variable named sentence that has been initialized, write an expression whose value is the index of the very last character in the String referred to by sentence . (3) Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the last character of the value of name . So if the value of name were "Smith" the expression's value would be "h".

Explanation / Answer

import java.util.Scanner; public class CopyString { static String sentence; static String secondWord; static String name; public static void main(String[] args) { System.out.println("Enter the sentence of atleast 2 words:"); Scanner input=new Scanner(System.in); sentence=input.nextLine(); try { String [] array = sentence.split(" ", 3); secondWord=array[1]; } catch (Exception e) { } System.out.println("Second Word is :"+secondWord); int len=sentence.length(); int index =len-1; System.out.println("Last Index is :"+index); System.out.println("Enter the name for finding last character:"); Scanner input1=new Scanner(System.in); name=input1.next(); len=name.length(); char lastCharacter=name.charAt(len-1); System.out.println("Last Character of "+name+" is :"+lastCharacter); } }