Question
JAVA only please ***************************PROVIDED CODE****************************** // Lab 11 - Working with String class methods public class Lab11 { public static void main( String[] args )
JAVA only please
***************************PROVIDED CODE******************************
// Lab 11 - Working with String class methods
public class Lab11 { public static void main( String[] args ) { String firstStr = "IS2041InterObjOrientedProgrLab11"; String secondStr = "IS2041InterObjOrientedProgrLab11"; String thirdStr = "Java How to Program: Late Objects Version, Eleventh Edition " + "by Paul and Harvey Deitel."; /* * The hashcode of an object is a correlate for its storage address. If two references * refer to the EXACT SAME OBJECT, their hashcodes will be identical. Generally, if * the hashcodes of two objects are *different* those objects are discrete and separate objects. */ int hashOfFirstStr = firstStr.hashCode( ); // STATEMENT ONE -- Verify String references by comparing hashcodes for each String. if( ....... ) System.out.printf( "%n'firstStr' and 'secondStr' refer to the SAME OBJECT!%nThere are two references, " + "but ONLY ONE String object.%n" + "The hashcode of 'firstStr': %d. The hashcode of 'secondStr': %d%n", ......., ....... ); else System.out.printf( "%n'firstStr' and 'secondStr' are two completely SEPARATE OBJECTS.%n" + "The hashcode of 'firstStr': %d. The hashcode of 'secondStr': %d%n", ......., ....... ); // STATEMENT TWO -- count how many characters are in the String 'firstStr'. System.out.printf( "%nString 'firstStr' is %d characters long%n", ....... ); // STATEMENT THREE -- does the String 'firstStr' contain the characters (in sequence) "Program"? if ( ....... ) System.out.printf( "%nThat char sequence 'Program' DOES exist in the String 'firstStr'.%n" ); else System.out.printf( "%nThe char sequence 'Program' DOES NOT exist in the String 'firstStr'.%n" ); // STATEMENT FOUR -- write a loop to count the occurrences of the char/letter 'a' in the String 'thirdStr' char target ='i'; // the character we will search for int length = thirdStr.length( ); // length of String object; for use in loop control int count = 0; // counter -- number of occurrences found int startNxtSrch = 0; // where to start next search 'cycle'; an INDEX into the String int foundAt = 0; // index @ which 'i' was found in this search 'cycle'
while( ....... ) { foundAt = .......; if ( ....... ) { startNxtSrch = foundAt +1; count += 1; } else startNxtSrch = length; } System.out.printf( "%nThere are %d occurrences of '%c' in thirdStr.%n", count, target );
} }
*********************END CODE********************
Purpose: Working with String class methods General Information In this lab assignment, you need to use methods of API class String to complete four statements/code segments described below in Requirements section. Provided Resources 1. 2. 3. Lab 11 Instructions, including requirements The code shell (Lab11.java) Sample output Requirements Do not change the provided variable declarations. You may, if you wish, add declarations, but you may not change those already existing in the shell. Add a condition for the 'if statement on line 24. The condition should return true when the two String objects, firstStr and secondStr can be assumed to be a single object, with a single storage location. It should return false if those String objects are separate objects, with discrete storage locations .Complete the two printf statements by adding the necessary values to replace the two decimal placeholders in each statement. Complete the printf statement by replacing the dots with a value containing the count of characters in the String firstStr. Statement Three Complete the if condition using a String method that will search the String firstStr for the given word and return a boolean value indicating whether that word exists in the String or notStep 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