Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class MyClass { /* * This method returns an array that contains the sorted contents of arr * It repeatedly finds the smallest char

image text in transcribed

public class MyClass { /* * This method returns an array that contains the sorted contents of arr * It repeatedly finds the smallest char in arr, copies it to the new array, and then replaces * that char in arr with a 0 to mark that it has been taken already. Therefore it skips over * any 0 values that it sees. * This algorithm has a bug! (Hint: 0 could be a valid value for a char!) */ public static char[] sorter(char[] arr) { char[] result = new char[arr.length]; boolean anythingFound; int count = 0; do { int smallestIndex = -1; anythingFound = false; //Find the smallest remaining char, skipping already removed chars for(int i=0; i

-----------------

import junit.framework.TestCase; import static org.junit.Assert.*;

public class TestClass extends TestCase{ public void setUp() { } public void testSorter() { char[] arrayToSort = new char[]{'a', 'b', 'c', 'd'}; char[] expected = new char[]{'a', 'b', 'c', 'd'}; assertArrayEquals(expected, MyClass.sorter(arrayToSort)); arrayToSort = new char[]{}; expected = new char[]{}; assertArrayEquals(expected, MyClass.sorter(arrayToSort)); arrayToSort = new char[]{'w', 'e', 'r', 's', 'b', 'd', 'a'}; expected = new char[]{'a', 'b', 'd', 'e', 'r', 's', 'w'}; assertArrayEquals(expected, MyClass.sorter(arrayToSort)); arrayToSort = new char[]{'3', '2', '2', '0'}; expected = new char[]{'0', '2', '2', '3'}; assertArrayEquals(expected, MyClass.sorter(arrayToSort)); } }

---------------------------------

please solve the program above by using java language.

Introduction This week's lab will focus on debugging and testing. In particular, it focuses on introducing new bugs, and insufficient testing. It is based on the sorting function in this week's homework assignment. The lab will be easier if you have looked at and understood the sorting method already. What you need to do Import the project into Eclipse. You have the same sorting method from the homework, and the same unit tests. As described in the homework, there is a bug related to O-value chars. We will ignore this bug for now, and instead focus on introducing a new bug! Here is your task: 1) Introduce a new bug into the method. This bug should be one that is rare enough that the existing tests DO NOT catch it. So, after you introduce this bug, the given test method should still pass. This will take some creativity! 2) Write another test method that DOES catch your new bug. This new test should fail, but it should also make sense! If you remove your bug, the new test should pass. 3) Debug your new test. Demonstrate to the TA how you can use the debugger to show where the execution goes wrong, as if you didn't already know what the bug is. Note: you may ONLY use alphanumeric chars in your tests for this lab. You may use other char values in your homework, however. Creating the bug In order for the sorting method to work correctly, it has to take the array of chars passed in, and return an array with the same char values, but in sorted order. Currently, this method does do that for the 4 sample inputs given. After you introduce a bug, the method will sometimes produce the wrong output, but it may still give the correct output other times. A method does not have to fail every time for it to count as buggy! In fact, we are asking that your new bug not cause the given test to fail. Your bug might only cause the program to fail when the array is very large, or when it contains a certain value, or even only for a certain specific sequence of characters. The point is that, whatever case causes the error, the existing tests don't produce that case, and your new test does. If you are having trouble coming up with a bug, ask for help! Introduction This week's lab will focus on debugging and testing. In particular, it focuses on introducing new bugs, and insufficient testing. It is based on the sorting function in this week's homework assignment. The lab will be easier if you have looked at and understood the sorting method already. What you need to do Import the project into Eclipse. You have the same sorting method from the homework, and the same unit tests. As described in the homework, there is a bug related to O-value chars. We will ignore this bug for now, and instead focus on introducing a new bug! Here is your task: 1) Introduce a new bug into the method. This bug should be one that is rare enough that the existing tests DO NOT catch it. So, after you introduce this bug, the given test method should still pass. This will take some creativity! 2) Write another test method that DOES catch your new bug. This new test should fail, but it should also make sense! If you remove your bug, the new test should pass. 3) Debug your new test. Demonstrate to the TA how you can use the debugger to show where the execution goes wrong, as if you didn't already know what the bug is. Note: you may ONLY use alphanumeric chars in your tests for this lab. You may use other char values in your homework, however. Creating the bug In order for the sorting method to work correctly, it has to take the array of chars passed in, and return an array with the same char values, but in sorted order. Currently, this method does do that for the 4 sample inputs given. After you introduce a bug, the method will sometimes produce the wrong output, but it may still give the correct output other times. A method does not have to fail every time for it to count as buggy! In fact, we are asking that your new bug not cause the given test to fail. Your bug might only cause the program to fail when the array is very large, or when it contains a certain value, or even only for a certain specific sequence of characters. The point is that, whatever case causes the error, the existing tests don't produce that case, and your new test does. If you are having trouble coming up with a bug, ask for help

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

In Figure 9 .3, by how much is the market overproducing cars?

Answered: 1 week ago