1. Given a string of odd length, return the character in the middle of the strin
ID: 3592233 • Letter: 1
Question
1.Given a string of odd length, return the character in the middle of the string, so that "abc" yields 'b'.
middleChar("a") 'a'
middleChar("abc") 'b'
middleChar("abcde") 'c'
public char middleChar ( String str ) {
int m = str.length( )/2;
}
Given a string of odd length, return the string of length 3 from its middle, so that "Peach" yields "eac". Assume that the str length will be at least 3.
"and"
middleThree("Peach") "eac"
middleThree("Student") "ude"
public String middleThree( String str ) {
}
Relational and Logical Operations
For the code below, give the printed values for each of the println statements.
Printed value
System.out.println( 7 > 2 );
System.out.println( 7 > = 2 );
System.out.println( 7 < 2 );
System.out.println( 7 < = 2 );
System.out.println( 7 = = 2 );
System.out.println( 7 ! = 2 );
System.out.println( true && true );
System.out.println( true && false );
System.out.println( true || false );
System.out.println( false || true && false );
System.out.println( false && true || false );
System.out.println( false || true && ! false );
System.out.println( true && ! false );
Give a java expression for each of the following.
int num;
Java expression
num is less than 0
num is greater than or equal to 0 and less that 10
num is less than 0 or greater that 9
public char middleChar ( String str ) {
int m = str.length( )/2;
}
Explanation / Answer
Question 1:
Here is code:
public class Testing {
public static void main(String[] args) {
System.out.println(middleChar("a"));
System.out.println(middleChar("abc"));
System.out.println(middleChar("abcde"));
}
public static char middleChar(String str) {
int m = str.length() / 2;
return str.charAt(m); // return the char at position
}
}
Output:
a
b
c
Question 2:
Here is code:
public class Testing {
public static void main(String[] args) {
System.out.println(middleThree("Peach"));
System.out.println(middleThree("Student"));
}
private static String middleThree(String str) {
int m = str.length() / 2;
if (str.length() > 2 && str.length() % 2 == 1) {
return (str.charAt(m - 1) + "" + str.charAt(m) + "" + str.charAt(m + 1)).toString();
}
return "";
}
}
Output:
eac
ude
Printed value
System.out.println( 7 > 2 );
true
System.out.println( 7 > = 2 );
true
System.out.println( 7 < 2 );
false
System.out.println( 7 < = 2 );
false
System.out.println( 7 = = 2 );
false
System.out.println( 7 ! = 2 );
true
System.out.println( true && true );
true
System.out.println( true && false );
false
System.out.println( true || false );
true
System.out.println( false || true && false );
false
System.out.println( false && true || false );
false
System.out.println( false || true && ! false );
true
System.out.println( true && ! false );
int num;
Java expression
num is less than 0
num < 0
num is greater than or equal to 0 and less that 10
num >= 0 && num < 10
num is less than 0 or greater that 9
num < 0 || num > 9
Printed value
System.out.println( 7 > 2 );
true
System.out.println( 7 > = 2 );
true
System.out.println( 7 < 2 );
false
System.out.println( 7 < = 2 );
false
System.out.println( 7 = = 2 );
false
System.out.println( 7 ! = 2 );
true
System.out.println( true && true );
true
System.out.println( true && false );
false
System.out.println( true || false );
true
System.out.println( false || true && false );
false
System.out.println( false && true || false );
false
System.out.println( false || true && ! false );
true
System.out.println( true && ! false );
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.