YenKang / Java-Programming-Solving-Problems-with-Software

5 stars 6 forks source link

Debugging part 1 #7

Open jacquesdelta opened 3 years ago

jacquesdelta commented 3 years ago

I am stuck here anyone help

Coursera

Java Programming: Solving Problems with Software Week 2 Debugging: Part 1

Finding a Gene in DNA

Finding All Genes in DNA

Debugging Code Practice Quiz: Debugging: Part 1 12 questions Practice Quiz: Debugging: Part 2 11 questions Discussion Prompt: Debugging First Steps . Duration: 10 minutes10 min

Using the StorageResource Class

Review PRACTICE QUIZPractice Quiz • 30 MIN30 minutes Debugging: Part 1 Submit your assignment Receive grade TO PASS65% or higher Grade —

Debugging: Part 1 Practice Quiz • 30 min Debugging: Part 1 TOTAL POINTS 12 1. Question 1 Your friend is trying to solve the following problem using Java:

Write a method that finds each occurrence of “abc” in a String input (where is a single character) and prints “bc_” for each such occurrence. For example, findAbc(“abcdefabcghi”) should print:

12 bcd bcg You friend has just finished writing a solution and needs help testing it.

Create a new BlueJ project. Create a class and copy and paste the two methods (below) into it.

1234567891011121314 public void findAbc(String input) { int index = input.indexOf("abc"); while (true) { if (index == -1) { break; } String found = input.substring(index+1, index+4); System.out.println(found); index = input.indexOf("abc", index+4); }

Test method findAbc() by adding a line in the test() method:

1 findAbc("abcd"); Do this example by hand. What should the output be if the method is working correctly?

1 point Enter answer here 2. Question 2 Compile your class and run the test() method. What is the output?

1 point Enter answer here 3. Question 3 So far so good! Comment out the line findAbc("abcd") and add a new line in the test() method:

1 findAbc("abcdabc"); What should the output be if the method is working correctly?

1 point Enter answer here 4. Question 4 Compile and run your code. Which two of the following are results?

1 point

"Error" is printed.

A StringIndexOutOfBoundsException is thrown.

"bcd" is printed.

A blank line is printed.

5. Question 5 Which line of the program is causing the error?

1 point

1 int index = input.indexOf("abc");

1 if (index == -1) {

1 String found = input.substring(index+1, index+4);

1 index = input.indexOf("abc", index+4); 6. Question 6 What is the index that is out of range?

1 point Enter answer here 7. Question 7 What is the length of input for this method call?

1 point Enter answer here 8. Question 8 At the time this error is produced, what are the values of index+1 and index+4? You may want to add print statements to your code and run it again to see what these values are.

Enter your answer as numbers separated by a comma: int1, int2. For example if index+1 were 1 and index+4 were 5, you would enter:

1 1,5 1 point Enter answer here 9. Question 9 Why does the program throw an index out of bounds exception for this input?

1 point

The method substring() is trying to access index 5, but the value of the int index is 4.

The method substring() is trying to access index 8 but the String input is only 7 characters long.

The program finds the wrong index for the second occurrence of "abc".

The method findAbc() never works when the String input is longer than 6 characters.

10. Question 10 Imagine your friend wants to get help from Coursera classmates on the discussion forums.

Which one of the following would be the most helpful way to describe the problem so that others can easily help?

1 point

My method findAbc() has an index out of bounds exception.

My method findAbc() has an index out of bounds exception when I call it with input "abcdabc".

My method findAbc() has an index out of bounds exception when I call it with input "abcdabc". The exception is at the line

1 String found = input.substring(index+1, index+4);

My method findAbc() has an index out of bounds exception at the line

1 String found = input.substring(index+1, index+4); when I call it with input "abcdabc". It prints "bcd" and then throws the exception. The index out of range is 8.

11. Question 11 Which of the following are examples of input that would also throw this exception? Check all that are correct.

1 point

"woiehabchi"

"yabcyabc"

"eusabce"

"aaaaabc"

"abcbbbabcdddabc"

12. Question 12 Add an if statement, such that the while loop breaks if the index is out of bounds. Compile the code and run it again. It should now print "bcd" without throwing an exception. Try it on the examples you selected the previous question to make sure it works.

What are possible conditions for this if statement? (Choose all that are correct.)

1 point

1 index >= input.length() - 3

1 index >= input.length() - 4

1 index > input.length() - 3

1 index > input.length() - 4

zeroni-lie commented 1 year ago

did you ever get the answers to this? bc im doing it right now and im dying bro help

Vithurshajini commented 9 months ago

Question 2: bcd Question 3: bcd Question 4: "bcd" is printed, A StringIndexOutOfBoundsException is thrown Question 5: String found = input.substring(index+1, index+4); Question 6: 8 Question 7: 7 Question 8: Question 9: The method substring() is trying to access index 8 but the String input is only 7 characters long. Question 10: My method findAbc() has an index out of bounds exception at the line
String found = input.substring(index+1, index+4); Question 11: "aaaaabc", "yabcyabc", "abcbbbabcdddabc" Question 12: index >= input.length() - 3, index > input.length() - 4