Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Homework 4 Java language Q1. Answer the following questions: (4x 4 = 16 points) 1. Consider the following string: String hannah = Did Hannah see
Homework 4 Java language Q1. Answer the following questions: (4x 4 = 16 points) 1. Consider the following string: String hannah = "Did Hannah see bees? Hannah did."; a. What is the value displayed by the expression hannah.length( )? b. What is the value returned by the method call hannah.charAt(12)? c. Write an expression that refers to the letter b in hannah. 2. Consider the following string: "Was it a car or a cat I saw?" a. How long is the string returned by substring(9, 12)? b. What is the string returned? 3. Show two ways to concatenate the following: String hi = "Hi,"; String mom = "mom."; 4. Given: String str = "This is tutorialspoint"; What is the value returned by the method call a. str.indexOf('t', 14); b. str.charAt(8); (7x3 = 21 points) Q2. Answer the following questions: Given: char value1 ='9', value2 = 'Y', value3 ='', value4 = '*'; 1. Write an expression that checks and prints if value is digit or not? output: 2. Write an expression that checks and prints if value3 is digit or not? output: 3. Write an expression that checks and prints if value2 is a letter or not? output: 4. Write an expression that checks and prints if valuel is digit or letter? output: 5. Write an expression that checks and prints if value4 is digit or letter? output: 6. Write an expression that checks and prints if value2 is lower-case letter or not? output: 7. Write an expression to change value2 to lower-case letter? output: Q2. For each of the following program, show the output. (4 x 8 = 32 points) 1. String s1 = "pankaj"; String s2 = "PANKAJ"; System.out.println(s1 = s2); 2. String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2); 3. String string1 = "Geeksforgeeks", string2 = "Practice", string3 = "geeks", string 4 = "Geeks"; System.out.println(string3 + " is equal to " + string4 + ":" + string3.equals( string4)); System.out.println(string3 + " is equal to " + string4 + + string3.equalsignoreCase string4)); System.out.println(string1 + " compared to " + string2 + ":" + string1.CompareTo( string2)); System.out.println(string2 +" compared to " + string1 +" + string2.CompareTo( string1)); 4. char c='a', c2='1'; int a=c, b=c2, x= 33; System.out.println("The ASCII code of "+ C + "is" + a); System.out.println("The ASCII code of "+c2 + "is" +b); System.out.printf("%d is the ASCII code of %c ",x,x); Q3. Write a program to input the sentence "I love candies", then display it with the first and the last occurrence of the letter 'e'. (17 points) Q4.Complete the missing statements in the following program, then show the output: (7*2=14 pt) public class JavaMathExample { public static void main(String[] args) { double x = 28, y = 4, a = 2.15, b=3.51; System.out.println( _); //the maximum of x & y System.out.println( _); // return the square root of y System.out.println( .)//returns x power of y System.out.println( _); // return a rounded up to its nearest integer System.out.println _); // return a rounded down to its nearest integer System.out.println( _); // return b rounded to its nearest integer System.out.println _); }} // random no in (y,x] Output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started