Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Syllabus Calendar Misc. Computer Science 2 CPSC 1302 Java Strings Exercises 1. Do the following in Java: A. Declare a string object named compiler and

image text in transcribed

Syllabus Calendar Misc. Computer Science 2 CPSC 1302 Java Strings Exercises 1. Do the following in Java: A. Declare a string object named compiler and assign "javac" to it B. Declare a string object named sourceCode and assign "StringsExercises.java" to it C. Declare and display a string object named cmdLine and assign it compiler and sourceCode concatenated together so that it displays: javac StringsExercises.java D. Write a method that counts the number of lines in a string object. For example, for the following String: "Two roads diverged in a yellow wood And sorry I could not travel both And be one traveler long I stood And looked down one as far as I could to where it bent in the undergrowth Then took the other as just as fair And having perhaps the better claim Because it was grassy and wanted wear Though as for that the passing there Had worn them really about the same And both that morning equally lay In leaves no step had trodden black oh I kept the first for another day yet knowing how way leads on to way I doubted if I should ever come back I shall be telling this with a sigh Somewhere ages and ages hence Two roads diverged in a wood and I I took the one less traveled by And that has made all the difference " the method would return 23 Optional: Display the lengths of each line 2. What does the following display System.out.println( compiler.substring( 0, 4 ) ); System.out.println( sourceCode.substring( 2, 6)); System.out.println( sourceCode.substring( 17 ) ); 3. What does the following display (and why)? System.out.println( "I have " + 5 + " fingers on one hand, and " + 5 + " fingers on one hand, so I a total of have " + 5 + 5 + " fingers !"); 4. What does the following display (and why)? System.out.println("\"java\" is found at index: " + cmdLine.indexOf( "java" ) ); System.out.println("\"java\" is found at index: " + cmdLine.indexOf( "java", 5 ) ); System.out.println("\"java\" is found last at index: " + cmdLine.lastIndexOf("java")); 5. Discuss with someone else what is the most important difference between a string object and a stringBuffer or StringBuilder object. Optional: Complete the above steps with a stringBuffer or StringBuilder object 6. Complete the following: A. Declare a string object named name and assign it your name B. Declare a string object named nameLowerCase and assign it a lowercase version of your name using a string method C. What will the following display? System.out.println("name == nameLowerCase: " + (name == nameLowerCase)); System.out.println("name.equals( nameLowerCase ): " + (name.equals( nameLowerCase ))); System.out.println("name.compareTo ( nameLowerCase ): " + (name.compareTo( nameLowerCase ))); Copyright Hyrum D. Carroll Last Modified: 03/05/2021 15:11:13

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions