Question
USING IntelliJ, Create project named CompSciQuiz; Implement the classes identified below; Code Specification: We are solving a classic interview problem, finding the longest prefix in
USING IntelliJ,
- Create project named CompSciQuiz;
- Implement the classes identified below;
Code Specification:
We are solving a classic interview problem, finding the longest prefix in an ArrayList of Strings.
The problem statement is:
You must find the longest common prefix in an array of Strings. If there is no common prefix, return an empty string (i.e. ""). If there is a common prefix, return it as a String:
Example: in this array ["flower","flow","flight"], we would return "fl" Example: in this array ["flower","flow"], we would return "flow" Example: in this array ["dog","racecar","car"], we would return "" Example: in this array [], we would return "" as there are no strings at all
Step 1:
Below are two classesLongestCommonPrefix andLongestCommonPrefixTest. Create the corresponding java files (they must have the same name), and ensure that you can run your tests.
Step 2:
Write new test where you pass an empty ArrayList to the findLongestCommonPrefix method. This test will fail, in fact, it will through an error. You need to figure out how to fix this, hint, it should return an empty string ("") if there are no elements in the ArrayList. Include at least one assert in the test.
Step 3:
Find a way to fix the code so that the test passes.
Step 4 (Extra Credit):
Write test that would fail given the code below and correct the code so that it passes, this must include at least assert in the test.
LongestCommonPrefix.Java import java.util.ArrayList; * This class includes functionality to find the longest common prefix in an array of Strings *If there is no common prefix, return an empty string * * Example: in this array ["flower", "flow", "flight"], we would return "f1" * Example: in this array ["flower", "flow"], we would return "flow" * Example: in this array ["dog", "racecar","car"], we would return ** * Example: in this array [], we would return ** as there are no strings at all */ public class Longest CommonPrefix { public String findLongest CommonPrefix (ArrayList strings) { // get the longest prefix as just the first one String prefix strings.get(0); // loop through all of the strings for (String string strings) { // do a temp string builder to keep track of current prefix StringBuilder tempPrefix = new StringBuilder(); // loop through the length of the prefix (the longest prefix can't be longer than the first string for (int i = 0; i < prefix.length(); i++) { // make sure you aren't going out of bounds if (string.length() > 1) { // compare them if (prefix.charAt(i) == string.charAt(1)) { // add if the match tempPrefix.append(prefix.charAt(1)); } else { // break out of the loop if they don't break; } } } } // update the prefix prefix = tempPrefix.toString(); // return the prefix return prefix;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
importorgjunitjupiterapiTest importjavautilArrayList importstaticorgjunitjupiterapiAssertions public class CompSciQuiz public static String findLonges...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started