Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 XFiles The following class maintains a collection of strings all begining with

ID: 3733339 • Letter: 1

Question

1 XFiles The following class maintains a collection of strings all begining with 'X' class XFiles { public LinkedList files; public void add(String s) I if (s.charAt(0)==X.) { files .add (s); public void remove (String s) I files .remove (s); public List getWords ) return files; Fix the above code, so it won't throw any exceptions [without using try/catch] Find two ways to break the invariant for this class. 2 Black box testing The following class tracks how many passengers are on a bus. public class Bus public Bus (int capacity); int getCurrent (); // how many on the bus right now int getAverageCount (); // average passengers between stops void stop (int on, int off); I/ Passenger changeover at a bus stop /passengers get off before any new get on 1. What would you test? ie what situations and inputs would you test. You don't need to specify expected output.

Explanation / Answer

import java.util.LinkedList;
import java.util.List;

public class XFiles {
public LinkedList<String> files;

//create a default constructor and create new instance of files so that null pointer exception should not come.
public XFiles()
{
   files=new LinkedList<>();
}

public void add (String s)
{
   //null handling
   if(s!=null && !s.isEmpty() &&s.charAt(0)=='X')
   {
       files.add(s);
   }
}

public void remove(String s)
{
   if(s!=null && !s.isEmpty() && files.size()>0)
   {
       files.remove(s);
   }
}

public List<String> getWords()
{
   return files;
}
}

2.We need to test capcity , current count , average count.

Different combinations for on and off. (check with on >off and off>on or both equal)

test for negative values .

3.

import java.util.ArrayList;
import java.util.Collections;

public class DistinctCounter {

   ArrayList<String> strings=new ArrayList<>();
  
   public void add (String s)
   {
       if(!strings.contains(s))
       {
           strings.add(s);
       }
   }
   public int getDistinctCount()
   {
       return strings.size();
   }
  
   public String[] getStrings()
   {
       Collections.sort(strings);
       return (String[]) strings.toArray();
   }
}

import static org.junit.Assert.assertNotNull;

import org.junit.Assert;
import org.junit.Test;

public class TestDistinctCounter {

   DistinctCounter dc=new DistinctCounter();
   @Test
   public void testAdd() {
       dc.add("X");
       Assert.assertEquals("Size must be 1",1, dc.strings.size());
      
       dc.add("X");
       Assert.assertEquals("Size must be 1",1, dc.strings.size());
      
       dc.add("Hello");
       Assert.assertEquals("Size must be 2",2, dc.strings.size());
   }

   @Test
   public void testGetDistinctCount() {
       dc.add("X");
       dc.add("X");
       dc.add("Hello");
       Assert.assertEquals("Distinct Count",2, dc.getDistinctCount());
   }

   @Test
   public void testGetStrings() {
       assertNotNull(dc.strings);
   }

}

Please implement last one by yourself.