Question: Create tests for the main method of: ArrayToArrayList.java class (I will copy and paste at the bottom) Tests should take advantage of assert Once you
Create tests for the main method of: ArrayToArrayList.java class (I will copy and paste at the bottom)
Tests should take advantage of assert
Once you have written one or two test methods, run your JUnit test case. There are two ways to do this.
One way is to click the Run button in the top toolbar (it looks like a green "Play" symbol). A menu will drop down; choose to run the class as a JUnit Test.
The other way is to right-click your JUnit test case class and choose Run As JUnit Test.
A new pane will appear showing the test results for each method. You should see a green bar if all of the tests passed, or a red bar if any of the tests failed. If any test fail, you can view the details about the failure by clicking on the failed test's name/icon and looking at the details in the pane below.
Note: Most people think that getting a red failure bar is bad. It's not! It is good; it means that you have found a potential bug to be fixed. Finding and fixing bugs is a good thing. Making a red bar become a green bar (by fixing the code and then re-running the test program) can be very rewarding.
import java.util.*; public class ArrayToArrayList { public static void main(String args[]) { int indexC, indexE; // integer to hold index value int i; // variable for for loop // array containing city names String cities[] = {"Montreal", "Saskatoon", "New York", "Toronto", "Sudbury", "London", "Paris", "Delhi", "Vancouver", "Winnipeg"}; ArrayList list = new ArrayList(); // storing array into an arraylist for (i = 0; i < cities.length; i++) { list.add(cities[i]); } System.out.println("Current list of cities:"); System.out.println(list); // create scanner object for keyboard input Scanner kb = new Scanner(System.in); System.out.print(" Enter index number of city to replace with Calgary: "); indexC = kb.nextInt(); list.set(indexC, "Calgary"); System.out.println(" Current list with replaced city with Calgary:"); System.out.println(list); System.out.print(" Now, what index number do you want to add Edmonton in? "); indexE = kb.nextInt(); list.add(indexE, "Edmonton"); System.out.println(" Current list with added city Edmonton:"); System.out.println(list); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
