In: Computer Science
Note: The answers to the following questions should be typed in the block of comments in the Assignemnt2.java file. Please make sure they're commented out (green). Type them neatly and make them easy to read for the graders.
Given the String object called myString with the value "Practice makes perfect!" answer the following questions.
Question #1 (1pt): Write a statement that will output
(System.out) the number of characters in the string.
Question #2 (1pt): Write a statement that output the index of the
character ‘m’.
Question #3 (1 pt): Write a statement that outputs the sentence in
uppercase letters
Question #4 (1 pt): Write a statement that uses the original
string to extract the new string "perfect" and prints it to the
screen
Question # 5 (2 pts): What do the following expressions evaluate to
in Java given int x = 3, y = 6;
a) x==y/2
b) x%2==0||y%2!=0 c) x–y<0&&!(x>=y) d)
x+6!=y||x/y<=0
Question # 6 (1 pt): What does the following statement sequence print? (page 63)
String str = “Harry”; int n = str.length(); String mystery = str.substring(0,1) + str.substring(n-1, n); System.out.println(mystery);
1) & 2) & 3) & 4) & 5)
// Screenshot of the code & output
// code to copy
Assignemnt2.java
public class Assignemnt2 {
public static void main(String[] args) {
String myString = "Practice makes perfect!";
//Q1 - the number of characters in the string
System.out.println(myString.length( ));
// Q2 - the index of the character 'm'
System.out.println(myString.indexOf('m'));
// Q3 - the sentence in uppercase letters
System.out.println(myString.toUpperCase());
// Q4 - extract the new string "perfect"
System.out.println(myString.subSequence(0,8));
}
}
5)
// Screenshot of the code & output
// code to copy
Main.java
public class Assignemnt2 {
public static void main(String[] args) {
int x = 3, y = 6;
// a statement
System.out.println(x==y/2);
// b statement
System.out.println(x%2==0||y%2!=0);
// c statement
System.out.println(x-y<0&&!(x>=y));
// d statement
System.out.println(x+6!=y||x/y<=0);
}
}
6)
// Screenshot of the code & output
// code to copy
Main.java
public class Assignemnt2 {
public static void main(String[] args)
{
String str = "Harry";
int n = str.length();
String mystery = str.substring(0,1)
+ str.substring(n-1, n);
System.out.println(mystery);
}
}