Lab 05 - String Comparisons

pdf

School

James Madison University *

*We aren’t endorsed by this school

Course

327

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

2

Uploaded by ProfessorSummer15257

Report
Lab 5 – String Comparisons 1. Write a program called PrintStrings.java that prompts the user to enter two words, reads the user input, compares the words to each other, and displays them in lexicographic order (ABC order). Here is a sample run (blue is user input, black is program output): Please enter two words: silver red In lexicographic order: red silver 2. Copy the code below into a program called, StringEqual.java . Please note that if you are copying the code, you will need to retype the quotes (“). Compile and execute the code. How does the program compare the two strings? Modify the program so that it displays The Strings str2 and str3 are the same. if str2 and str3 really are equal (contain the same characters) when they are compared. (Hint: You will need to use the information discussed in Chapter 4 of the textbook on comparing Strings.) public class StringEqual{ public static void main(String[] args){ String str1 = “abcd”; String str2 = “abcdefg”; String str3 = str1 + “efg”; System.out.println(“str2 = “ + str2); System.out.println(“str3 = “ + str3); if (str2 == str3){ System.out.println(“The Strings str2 and str3 are the same.”); } else { System.out.println(“The Strings str2 & str3 are not the same.”); } } } 3. Now you are going to modify the program provided called MiddleString.java that prompts the user to enter three strings. You will adjust it to compare the String objects lexicographically, and print the middle---valued String. For example, if the three strings were "abcd", "wxyz", and "pqrs", the program would print "pqrs". Before you begin to code, do the following: make a list of all possible ordering possibilities for a given str1, str2, and str3 that would thoroughly test the code for the MiddleString.java program. You can use the provided test case plan to help you organize your tests Write an algorithm that will outline your code for MiddleString.java. Each algorithm step should be in close proximity to the relevant line(s) of code. Enter your test data as comments in the header section of the file. (Hint: Make sure that when you are listing out every possible ordering possibility through the program, you are consistent. This way you will easily find each possibility of ordering the three strings. This is important so that you can always find the middle value.)
Here is a sample run (blue is user input, black is program output): Please enter three Strings abd eft bde bde 4. Complete the program, compile and test your code by running it with the input data that tests every path through your code. 5. Upload PrintStrings.java, StringEqual.java, MiddleString.java, AND your test cases (into the Lab 5 submission link in Gradescope and make sure it passes all test cases. Make sure your test case document is uploaded as a PDF (you can save a word doc as a pdf or print the word document to pdf). Rubric Topic Points PrintStrings.java passes all test cases 20 PrintStrings.java has a robust solution 5 StringEqual.java passes test cases 5 StringEqual.java has only one line change 5 MiddleString.java passes all test cases 45 Programming Style (e.g. indentation, variable names, etc.) (MiddleString.java only) 5 MiddleString.java has algorithm outlined and other relevant documentation (e.g. test data in comments, header, etc.) 10 Test cases for MiddleString.java submitted as pdf and tests all edge cases 5 Total 100
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help