In: Computer Science
1. Given an int variable k, write an expression whose value is the k-th character in the String variable word. Assume thatword has been initialized and that the value ofk is non-negative and less than the length ofword.
Example 1: if k's value is 3 andword is "spring", the value of the expression would be 'i'.
Example 2: if k's value is 0 andword is "fall", the value of the expression would be 'f'.
2. Given an int variable k, write an expression whose value is character that immediately preceds the k-th character in the String variableword. Assume that word has been initialized and that the value of k is positive and less than the length of word.
Example 1: if k's value is 3 andword is "spring", the value of the expression would be 'r'.
Example 2: if k's value is 1 andword is "fall", the value of the expression would be 'f'.
3. Given a String variablename. that has been initialized to value whose length is 2 or more, write an expression that istrue if an only if the first two character of theString are identical.
Example 1: if name is "bob", the value of the expression would be false.
Example 2: if name is "oona", the value of the expression would be true.
4. Given a String variablename. that has been initialized to value that is not empty, write an expression that is true if an only if the first and the last characters of theString are identical.
Example 1: if name is "bob", the value of the expression would be true.
Example 2: if name is "oona", the value of the expression would be false.
5. Given a String variablename. that has been initialized to value that is not empty, write an expression that is true if an only if the the last two characters of the Stringare identical.
Example 1: if name is "Roo", the value of the expression would be true.
Example 2: if name is "Kanga", the value of the expression would be false.
1.
import java.util.*;
class chegg {
public static void main(String[] args) {
// EXPRESSION 1st
String word = "spring";
int k = 3;
char value = word.charAt(k); // SIMPLY USING THE charAt inbuilt to get the Kth value
System.out.println(value);
}
}
2.
import java.util.*;
class chegg {
public static void main(String[] args) {
// EXPRESSION 2nd
String word = "spring";
int k = 3;
char value = word.charAt(k - 1); // SIMPLY USING THE charAt inbuilt to get the K-1th value
System.out.println(value);
}
}
3.
import java.util.*;
class chegg {
public static void main(String[] args) {
// EXPRESSION 2nd
String word = "oona";
boolean result = word.charAt(0) == word.charAt(1) ? true : false;
// using the ternary operator that will return true if 0th and 1st index char are same else false
System.out.println(result);
}
}
4.
import java.util.*;
class chegg {
public static void main(String[] args) {
String word = "bob";
boolean result = word.charAt(0) == word.charAt(word.length() - 1) ? true : false;
// using the ternary operator that will return true if 0th and the last index char are same else false
System.out.println(result);
}
}
5.
import java.util.*;
class chegg {
public static void main(String[] args) {
String word = "Roo";
boolean result = word.charAt(word.length() - 2) == word.charAt(word.length() - 1) ? true : false;
// using the ternary operator that will return true if the last and the second last index char are same else false System.out.println(result);
}
}