Question: Problem 6: Practice with static methods, part I 16 points total; pair-optional This is the only problem of the assignment that you may complete with
Problem 6: Practice with static methods, part I
16 points total; pair-optional
This is the only problem of the assignment that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured.
This problem asks you to write a series of static methods.
Getting started
If you havent already done so, create a folder named ps2 for your work on this assignment.
Download the following file: Methods6.java
Make sure to put the file in your ps2 folder. If your browser doesnt allow you to specify where the file should be saved, try right-clicking on the link above and choosing Save as... or Save link as..., which should produce a dialog box that allows you to choose the correct folder for the file.
In VS Code, select the File->Open Folder or File->Open menu option, and use the resulting dialog box to find and open the folder that you created for this assignment. (Note: You must open the folder; it is not sufficient to simply open the file.)
The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the name of the Methods6.java file that you downloaded in step 2.
Click on the name Methods6.java, which will open an editor window for that file. Add the methods described below to the class in that file.
Make sure that you follow the important guidelines given above at the start of Part II. In addition:
All of your method headers should begin with the keyword public.
You do not need to handle bad inputs inputs with a value that doesnt correspond to the description of the inputs provided in the problem.
Here are the methods you should implement:
a static method called printVertical that takes a String as its parameter and prints the characters of the string vertically with one character per line. For example, a call of printVertical("method") should produce the following output:
m e t h o d
We have given you this example method in the starter file.
Testing your methods To help you test your methods, weve provided a main method in Methods6.java that you can use to make appropriate test calls, and weve included a sample test call for printVertical.
You can see if the test calls produce the correct output by running the program. One way to do so in VS Code is to right-click on the name Methods6.java in the Explorer Pane and choose Run.
a static method called printEveryOther that takes a String as its parameter and prints every other character of the string i.e., the first character, the third character, etc. For example, a call of printEveryOther("method") should produce the following output:
mto
This method should not return a value.
Hint: Use printVertical as a model.
To test your method, add one or more test calls to the main method, and run the program to see if you get the expected results.
a static method called longerLen that takes two String objects as parameters and returns the length of the longer of the two strings. If the two strings have the same length, you should return that length. For example:
a call of longerLen("bye", "hello") should return 5, because 5 is the length of "hello", which is the longer of the two strings.
a call of longerLen("goodbye", "hi") should return 7, because 7 is the length of "goodbye", which is the longer of the two strings.
This method should not do any printing; rather, it should determine the longer length and return it.
Hint: Because this method returns a value and doesnt print anything, youll need to perform the necessary printing yourself when you test it. For example, you could add these lines to the main method:
int len = longerLen("bye", "hello"); System.out.println("the longer length is: " + len); a static method called secondIndex that takes as its two parameters a string s and a character c and returns the index of the second occurrence of c in s (if any). If s does not have at least two occurrences of c, the method should return -1.
For example:
a call of secondIndex("banana", 'a') should return 3, because the second occurrence of 'a' in "banana" occurs at position 3 in the string
a call of secondIndex("banana", 'n') should return 4, because the second occurrence of 'n' in "banana" occurs at position 4 in the string.
a call of secondIndex("banana", 'b') should return -1, because there is only one 'b' in "banana".
a call of secondIndex("banana", 'x') should return -1, because there is no 'x' in "banana".
This method should not do any printing; rather, it should return the appropriate integer.
Additional testing Once you have completed all of these methods, you should run a separate test program that weve created. This is an important step as it will help to identify any issues in your methods that might prevent us from grading them.
Heres how to use the test program:
Download the following file: Test6.java
Put it in your ps2 folder with your Methods6.java file.
As needed, open your ps2 folder using the File->Open Folder or File->Open menu option in VS Code. The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the names of both the Methods6.java and Test6.java files.
Right click on Test6.java and choose Run to compile and run the program. If the IDE reports errors in Test6.java, that probably means that at least one of your methods does not have the correct header i.e., either the name, the return type, or the parameters are incorrect. Make whatever changes are needed to your methods to get Test6.java to compile and run.
Once Test6.java does compile and run, you should check the results that it produces; the correct results are given in the descriptions of the methods above.
Feel free to add additional test cases to the ones that weve provided.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
