dimanche 20 septembre 2020

Time taken to use a method

I am trying to find the time taken to insert random integers between 0 and 200 into an integer list. I am not entirely sure how to limit the random to just between 0 and 200 and my other issue is that I get the error:

java.lang.IllegalArgumentException: bound must be positive

Experiment Controller:

public class ExperimentController
{
    
    /**
     * Constructor for objects of class ExperimentController
     */
    public static void main(String[] args)
    {
        ExperimentController EX = new ExperimentController();

        for(int i =1; i<=200;i=i+50){
          long time = EX.timeAppend(i, 0);
          System.out.println(time);
        }
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public long timeAppend(int numberOfItems, int seed){
        long startTime = System.nanoTime();
        
        IntegerList list = new IntegerList();
        Random random = new Random(seed);
        
        for(int i=0; i<numberOfItems; i++){
            int randomInt = random.nextInt(seed);            
            list.append(randomInt);   
        }
        

        long stopTime = System.nanoTime();
        long timeTotal = stopTime-startTime;
        return timeTotal;
        
    
    }   
}

Integer List:

public class IntegerList extends IntegerListADT
{
    Cell root;

    /**
     * Constructor for objects of class IntegerList
     */
    public static void main(String[] args)
    {
         Cell root;
   
    }
    
    public void append(int x){
      if(root == null){
          root = new Cell(x);
        }
        
        else{
            root.append(x);    
        }
        
    }
    
    public String toString(){
        if(root == null){
            return  root.toString();    
        }
        else{
            return root.toString();
        }
        
    }
    
    public boolean isEmpty(){
        if(root == null){
           return true;
        }
        else{
           return false;
        }
    }

}

Cell class:

public class Cell
{
    // instance variables - replace the example below with your own
    private int val;
    private Cell next;
     

    /**
     * Constructor for objects of class Cell
     */
    public Cell(int val)
    {
       
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public void append(int x)
    {
       if (next == null) {
           next = new Cell(x);
        }
        
        else{
         next.append(x);   
        }
        
    }
    
    public String toString(){
        if(next == null){
            return String.valueOf(val);
        }
        return val+ next.toString();
    }
}



Aucun commentaire:

Enregistrer un commentaire