Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are totally stuck you might consider starting over from scratch and go your way again with smaller yet safer steps.</p> <p>Start with a simple test:</p> <pre><code>@Test public void testConstruct() { MyNewList&lt;Integer&gt; myNewList = new MyNewList&lt;Integer&gt;(); assertNotNull(myNewList); assertEquals(0, myNewList.size()); } </code></pre> <p>now write <strong>just enough</strong> code in <code>MyNewList</code> to pass the test - do <strong>not</strong> think of complicated structure, the most simple solution will suffice for now - even <code>return 0;</code> for <code>size()</code> is okay.</p> <p>If your code finally passes the test you can write another one:</p> <pre><code>@Test public void testAdd() { MyNewList&lt;Integer&gt; myNewList = new MyNewList&lt;Integer&gt;(); myNewList.add(5); assertEquals(1, myNewList.size()); } </code></pre> <p>Again implement just enough code to pass <strong>both</strong> tests. Once you succeed alter the test to check if your solution also works with other parameters:</p> <pre><code>@Test public void testAdd() { MyNewList&lt;Integer&gt; myNewList = new MyNewList&lt;Integer&gt;(); myNewList.add(5); assertEquals(1, myNewList.size()); myNewList.add(7); assertEquals(2, myNewList.size()); } </code></pre> <p>Now you can trust that your <code>add()</code> method and decide what to do next: Take a short rest and clean up your code or organize the internal structure of you class, or march on to the next destination of your journey?</p> <p>When you finally go on you have to solve the next puzzle:</p> <pre><code>@Test public void testRemoveAll() { MyNewList&lt;Integer&gt; myNewList = new MyNewList&lt;Integer&gt;(); myNewList.add(5); myNewList.add(5); myNewList.add(7); myNewList.add(3); assertEquals(4, myNewList.size()); myNewList.removeAll(3); assertEquals(3, myNewList.size()); } </code></pre> <p>Now again write just enough code to pass <strong>all</strong> tests. For now you may even implement your method this way:</p> <pre><code>public void removeAll(T element) { size--; } </code></pre> <p>This would be could since it completely fulfils the requirements the above test describes. Now let us confront you method to a tougher check and alter the test:</p> <pre><code>@Test public void testRemoveAll() { MyNewList&lt;Integer&gt; myNewList = new MyNewList&lt;Integer&gt;(); myNewList.add(5); myNewList.add(5); myNewList.add(7); myNewList.add(3); assertEquals(4, myNewList.size()); myNewList.removeAll(3); assertEquals(3, myNewList.size()); myNewList.removeAll(5); assertEquals(1, myNewList.size()); } </code></pre> <p>If you chose a trivial solution earlier, you might be "in trouble" now ;) So think about your <code>removeAll()</code> method again and extend its code until it passes the tests again. Once you have this done you can rest assured that you code works.</p> <p>If you change something in your class run your tests again to see if something is broken.</p> <p>If requirements toward the way your methods have to work change - change your tests to reflect the new requirements and run them again.</p> <p>At a first glance this might seem very tedious but once you have a solid fondation of tests set up you can always say: "My code <strong>does</strong> work exactly how it should"</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload