Question: /* * Lab 07a - Exploring the String class * * * (c) */ public class Lab07a { public static void main( String[] args )
/* * Lab 07a - Exploring the String class * * * (c) */ public class Lab07a { public static void main( String[] args ) { String magicWord = "Priori Incantatem"; String anotherMagicWord = "Priori Incantatem"; String hamlet = "To be, or not to be--that is the question: " + "Whether 'tis nobler in the mind to suffer The slings and arrows of " + "outrageous fortune Or to take arms against a sea of troubles " + "And by opposing end them.";
/********************************************************************************************************* * Complete the following statements/code segements to produce the required output *********************************************************************************************************/ /********************************************************************************************************* * STATEMENT ONE -- Verify String references by comparing hashcodes for each String. * * 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. * * 1. Complete the condition for the 'if' statement. * 2. Add (to each printf statement) the necessary items to replace the two decimal value holders in * each statement. *********************************************************************************************************/ if( ....... ) System.out.printf( "%n'magicWord' and 'anotherMagicWord' refer to the SAME OBJECT!%nThere are two " + "references, but ONLY ONE String object.%n" + "The hashcode of 'magicWord': %d. The hashcode of 'anotherMagicWord': %d%n", ......., ....... ); else System.out.printf( "%n'magicWord' and 'anotherMagicWord' are two completely SEPARATE OBJECTS.%n" + "The hashcode of 'magicWord': %d. The hashcode of 'anotherMagicWord': %d%n", ......., ....... ); /********************************************************************************************************* * STATEMENT TWO -- count how many characters are in the String 'hamlet'. * * Add the necessary code to replace the decimal placeholder in the printf statement below. *********************************************************************************************************/ System.out.printf( "%nString 'hamlet' is %d characters long%n", ....... ); /********************************************************************************************************* * STATEMENT THREE -- does the String 'magicWord' contain the characters (in sequence) "cantar"? * * Add the appropriate condition for the if statement. The condition should check the String 'magicWord' * for the characters 'canta'. *********************************************************************************************************/ if ( ....... ) System.out.printf( "%nThe char sequence 'cantar' DOES exist in the String 'magicWord'.%n" ); else System.out.printf( "%nThe char sequence 'cantar' DOES NOT exist in the String 'magicWord'.%n" ); /********************************************************************************************************* * STATEMENT FOUR -- write a loop to count the occurrences of the char/letter 'o' in the String 'hamlet' * * 1. Add the condition for the while loop on line 85. The loop should continue until the entire String * 'hamlet' has been examined. * 2. Complete the assignment statement on line 87. Use a method of String class that will examine a * String object for a specific character, beginning at a specific point in the String objec, * and return an integer representing the index into the String at which the character was located. * 3. Add the condition for the if statement on line 88. What value is returned from the method in * step 2 if the character is NOT found in the String? *********************************************************************************************************/ char target ='o'; // the character we will search for int length = hamlet.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 'e' was found in this search 'cycle'
while( ....... ) { foundAt = .......; if ( ....... ) { startNxtSrch = foundAt +1; count += 1; } // end positive branch else startNxtSrch = length; } // end loop System.out.printf( "%nThere are %d occurrences of '%c' in hamlet.%n", count, target );
} // end main } // end Lab07a
/* * Lab 07b - Working with Wrapper Classes * * * (c) */ public class Lab07b { public static void main( String[] args ) { /* * MAKE NO CHANGES TO THE DECLARED VARIABLES. * DO NOT ADD VARIABLE DECLARATIONS */ String integerA = "6053"; String integerB = "2085"; String integerC = "5023"; String doubleA = "1365.52"; String doubleB = "152.89"; String doubleC = "98541.235"; /* * Complete the following statements to produce the required results. * * Use methods of wrapper classes Integer and Double to complete the work. * */ // Output the sum of integerA, integerB, and integerC. System.out.printf( "The sum of integerA, integerB, and integerC is: %d%n", .......... ); // Output the results of integerC - integerA. System.out.printf( "The difference of integerC less integerA is: %d%n", .......... ); // Output the results of integerA / integerB. NOTE: the output MUST include the DECIMAL PORTION of the result. // You may need to refer to work done in IS2033 as needed.... System.out.printf( "The result of dividing integerA by integerB is: %g%n%n", .......... ); // Output the sum of doubleA, doubleB, and doubleC. System.out.printf( "The sum of doubleA,doubleB, and doubleC is: %f%n", .......... ); // Output the results of doubleB - doubleC. System.out.printf( "The difference of doubleB less doubleC is: %f%n", .......... ); // Output the results of doubleB / doubleA. NOTE: the output MUST include the DECIMAL PORTION of the result. System.out.printf( "The result of dividing doubleB by doubleA is: %f%n", .......... ); } // end main } // end Lab07b
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
