Valdosta State University



CS 1302 – HW 1 This homework has one problem with 5 methods that deal with strings.PreliminaryDo not do this assignment until you have completed Lab 1 where you learned to use Eclipse. Your homework must be completed exactly as prescribed. This means method names must be spelled correctly, the return type must be as specified, and the order of parameters should follow the order they were mentioned in the problem.Do this now:Open eclipse in your cs1302_code workspace (or create a new workspace and open Eclipse there)Create a Java Project in Eclipse with the name: hw1_lastnameRight-click the src folder:Choose: New, Class Change the Package to “prob1”Supply the class Name “StringExamples”Select option to generate main.Choose: FinishDownload and unzip, hw1_testcases.zip. Drag the three files there (TestEngine.java, TestSuite.java, expectedResults.txt) into the prob1 folder.Read the comments at the top of TestEngine.java and TestSuite.java.Read the problem below and write your code.Problem 1 – 100 pointsFor this problem you will write 5 (unrelated) static methods inside a single class. Follow the steps below.Methods 1 & 2 (25 points total)Passwords must be at least 6 characters long and have no spaces. Any other characters are permitted. A Level 1 password must meet at least 2 of the following conditions below and a Level 2 password must meet all 3 conditions.at least one lower case letterat least one upper case letteras least one digitIn the StringExamples class, write the two static methods described below. boolean isLevel1(String password) – returns true if password is a Level 1 password and false otherwise.boolean isLevel2(String password) – returns true if password is a Level 2 password and false otherwise.Hint: you should write helper methods for each of the (bulleted) conditions. Then, the required methods will be easy to write. For help with this see Appendix A.For this assignment, I have provided you with test cases. I test extremely thoroughly. To gain insight into how I derived the test cases, see Appendix B. I strongly recommend that you read this several time and really think about it. For future assignments, I will not always give you the test cases I use. Thus, for those assignments you will need to develop and test your code thoroughly so that you will have a better chance of getting a good grade when I run my test cases. Follow these steps:Download hw01_testcases.zip from the Schedule.Unzip and drag the three files (TestEngine.java, TestSuite.java, expectedResults.txt) to your prob1 folder.Open TestSuite.java and expand the class in the Package Explorer. There you will see the following methods:Test MethodTest Coveragetest01 (and helper01)isLevel1test02 (and helper02)isLevel2test03 (and helper03)reverseWordstest04 (and helper04)getUniqueCharsAndDigitstest05getSmallestSalaryStringtest06getSmallestSalaryStringComment out test03 onward.Make a copy of expectedResults.txt. For the directions that follow, I’ll call the copy: expectedResults-copy.txt. Open expectedResults.txt. Find the line that begins with: “# Test 3…” and delete from there to the end of the file.Open TestEngine.java and run. The results are shown in the console with clear indication of what is correct and what is not. Method 3 (25 points)Write a static method, reverseWords. This method accepts a string which is composed of words separated by spaces. It returns a string with the words reversed. You can assume a single space separates words. You cannot use String.split. For example:Input String: The red king has the ringreverseWords(): ring the has king red TheHints: You could loop through the characters backwards, checking to find the spaces which tells you where the words are.Or, you could use lastIndexOf(ch,from) to identify the words (and then substring to extract them). In either case, you will probably need two variables, a left and right to keep track of where a word begins and ends.Test this method.Open TestSuite.java and uncomment test03 and helper03 methods.Delete expectedResults.txtCopy expectedResults-copy.txt and give it the name: expectedResults.txt.Open expectedResults.txt. Find the line that begins with: “# Test 4…” and delete from there to the end of the file.Open TestEngine.java and run. The results are shown in the console with clear indication of what is correct and what is not.Method 4 (25 points)Write a static method, getUniqueCharsAndDigits that accepts an array of strings and returns a single string that contains the unique alphabetic characters found in all the strings in the array, in the order they occur, followed by the unique digits found in all the strings in the array in the order they occur. You should ignore any characters that are neither alphabetic nor a digit. For example: InputOutput[“&”][][“7”][“7”][“a”, “1ab”, “23” “2c”]“abc123”[“rbr”, “a9b”, “9b *24”]“rba924”["ab1cba2", "34ad5#e", "%*fga1236"]“abcdefg123456”Test this method.Open TestSuite.java and uncomment test04 and helper04 methods.Delete expectedResults.txtCopy expectedResults-copy.txt and give it the name: expectedResults.txt.Open expectedResults.txt. Find the line that begins with: “# Test 5…” and delete from there to the end of the file.Open TestEngine.java and run. The results are shown in the console with clear indication of what is correct and what is not.Methods 5 (25 points)Write a static method, getSmallestSalaryString that accepts: an array of strings, names; an array of ints, ages; and an array of doubles, salaries. The size of each array is the same and the corresponding elements in each array are related. In the example below, the first element in names is “Keisha” and her age is the first entry in ages (22) and her salary is the first element in salaries (68,992.92). For example:String[] names = {"Keisha", "Jed", "Jaylen"};int[] ages = {22, 33, 44};double[] salaries = {68992.92430, 48339.23423, 121042.04328};The method should find the smallest salary and use that to get the corresponding name and age. The method should return a string that shows a message exactly in this format (must use String.format):Smallest salary:$48,339.23, Name:Jed, age:22Test this method.Open TestSuite.java and uncomment test05 and test06 methods.Delete expectedResults.txtCopy expectedResults-copy.txt and give it the name: expectedResults.txt.Open TestEngine.java and run. The results are shown in the console with clear indication of what is correct and what is not.SubmissionHere, I will give explicit directions about how to submit your work. It follows directly Lab 1, Stage 5. In the future I will just say, “zip your package folders into a file with the name: hw1_lastname.zip.Prepare for archiving – Do the followingMake sure all your files are saved in Eclipse.Although not necessary, I recommend closing Eclipse. Do that now.Archiving your work for submission – Do the following:right2032000In Windows Explorer, display the contents of the src folder for your hw1 project folder. This folder should contain one folder (package) for every problem in the homework. For this homework there is only 1, prob1.Select the prob1 folder as shown in the figure on the right (in general, select all the prob folders).Right-click the selected folders and choose: Send to, Compressed (zipped) folder.Provide the name:hw1_lastNameThis will create a zip file with name: hw1_lastName.zip.Test your zip file – Do the following:Copy your zip file from above to some new location (anywhere, but not in your workspace). Right-click the file and choose: Extract All… and then choose: OK.Verify that the proper folder (prob1) and files are present.Upload your zip file to Blazeview to the proper dropbox (in this case, HW 1)Appendix ASuppose you are writing a method and it seems even just at little a bit complicated. A good solution is to break the problem into smaller pieces and use helper methods to solve the smaller pieces. Then the original method you are writing will just call the helper methods. You should do this in a way that makes your original method more readable, so that as you read the code it is self-explanatory. Another thing to keep in mind is that we want methods to have high cohesion. Cohesion refers to the strength of relatedness of the code in a method. One way to achieve this is to make sure a method does one thing. Sometimes this can make your code less efficient. In this problem it is definitely less efficient, but the difference is trivial. If passwords were 1,000,000 characters long it might make a difference. Below, I show the way I would write the two required methods. Notice that the helper methods (isLongEnough, atLeastOneUpper, etc) do the detailed work while the main method, isLevel1 reads in a natural way that clearly expresses the logic.public static boolean isLevel1(String pwd){int howMany = 0;if(isLongEnough(pwd) && hasNoSpaces(pwd)) {if(atLeastOneUpper(pwd)) howMany++;if(atLeastOneLower(pwd)) howMany++;if(atLeastOneDigit(pwd)) howMany++;if(howMany >= 2)return true;}return false;}public static boolean isLevel2(String pwd) {if(isLongEnough(pwd) && hasNoSpaces(pwd) && atLeastOneUpper(pwd) && atLeastOneLower(pwd) && atLeastOneDigit(pwd))return true;return false;}Appendix BConsider the characteristics of a password that effect isLevel1 for Problem 1: length, number of spaces, number of upper case characters, number of lower, and number of digits. In software testing an equivalence class breaks the values of a characteristic into ranges where the method should treat things the same. For example for any length less than 6 the method should return false; it doesn’t matter whether the test case is “abc” or “abcde”. Thus, the equivalence class for length is: Len = {<6, >=6}. Similarly, it doesn’t matter how many upper case characters there, we just need at least 1 to contribute to the count. Thus, the equivalence class is: numUpper={0, >=1). The complete set of equivalence classes for this problem is:CharacteristicEquivalence Classlen{<6, ≥6} numSpaces{0, ≥1} numUpper{0, ≥1} numLower{0, ≥1} numDigits{0, ≥1} We see that there are 5 factors with 2 choices in each one. Thus, there are 25=32 possible tests using this approach. However, we can simplify things a bit. For example we only need one test case where the length is less than 6 because the method should immediately report false. The same is true with the number of spaces. Thus, the first two tests are:Test SpecificationTest CaseExpected ResultLen<6“abc”FalseTest SpecificationTest CaseExpected ResultnumSpaces >= 1“abc 123”FalseNow, this leaves three factors: numUpper, numLower, numDigits. Since there are two choices for each, that gives 23=8 more tests we should do:Test SpecificationTest NumnumUppernumLowernumDigitsTest CaseExpected Result3000“&&&&##”false400≥1“&&&&45”false50≥10“&&aaaa”false60≥1≥1“aaa333”true7≥100“BBBBBB”false8≥10≥1“2222BB”true9≥1≥10“cccBcc”true10≥1≥1≥1“BBcc22”trueThis approach does not guarantee that your method is correct. However, it is better than making tests up ad hoc, just whatever comes to mind. For example, another characteristic we might have wanted to test would be the location of the upper case character (and lower and digits) in the password. For example, does the method work with an uppercase letter in the first position? In the middle? In the end? That is why I have randomized the locations a bit in the test cases above.As you can see, testing can be tedious. I encourage you to start approaching testing with at least a bit more rigor than ad hoc. For almost all homework assignments I have test scripts that will test your code very thoroughly with a number of test cases. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download