Question

Please help with Java array list:

In this lab, you will implement two basic heuristics for updating a list of Intergers based on a sequence of requests to elem

import java.util.ArrayList;

public class ListUpdate {

/**
* Does a linear search through the ArrayList list, returning the index of the first occurrence of
* valToFind.
*
* Starting from index 0, check each element in list and return the index of the first element
* that matches valToFind.
*
* @param valToFind The int value to search for.
* @param list The ArrayList of Integers to search in.
* @return The index of the first occurrence of valToFind in list. If list is null, or valToFind
* is not found, return -1.
*/
public static int linearSearch(int valToFind, ArrayList<Integer> list) {
return -99;
}

/**
* Performs the Move to Front heuristic on the element located at index in list.
*
* Moves the element at index in list to index 0.
*
* If the list is null, or the index is out of bounds, the method should end gracefully without
* changing list.
*
* @param list The ArrayList to update.
* @param index The element in the ArrayList to move to the front.
*/
public static void moveToFront(ArrayList<Integer> list, int index) {
}

/**
* Performs the Transpose heuristic on the element located at index in list.
*
* Moves the element at index to index - 1. That is, it moves the item at index one spot closer
* to index 0.
*
* If the list is null, or the index is out of bounds, the method should end gracefully without
* changing list.
*
* @param list The ArrayList to update.
* @param index The element in the ArrayList to move to the front.
*/
public static void transpose(ArrayList<Integer> list, int index) {
}

/**
* Runs either the Move to Front or the Transpose heuristic on a given list over a sequence of
* requests, calculating the cost for accessing the items.
*
* For each element in requests:
* - Find its index in list. Let i be the index of the request in list.
* - The cost to access an item at index i (0 based) is i + 1. Add this cost to a running
* total.
* - Apply the move to front heuristic (doMTF is true) or the Transpose heuristic (doMTF is
* false) to list to the element at index i.
*
* Don't duplicate code! Use your linearSearch, moveToFront, and Transpose methods!
*
* @param requests The sequence of request to process.
* @param list The list to update with each request.
* @param doMTF A flag that is true when the heuristic to use is Move to Front and false when it
* is Transpose.
* @return The total access cost for the all the elements of requests.
*/
public static int runListUpdate(ArrayList<Integer> requests,
ArrayList<Integer> list,
boolean doMTF){
return -99;
}
  
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.ArrayList; import java.util.Arrays; public class List Update { * Does a linear search through the ArrayList

* @param list The ArrayList to update. * @param index The element in the ArrayList to move to the front. */ public static voi

Sample output:

// test the methods

public static void main(String[] args)
    {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        ArrayList<Integer> valuesAccessed = new ArrayList<>(Arrays.asList(2,4));
        int totalCost = ListUpdate.runListUpdate(valuesAccessed,list,true);
        System.out.println("list = " + list);
        System.out.println("totalCost = " + totalCost);
    }

Console X <terminated List Update [Java Application) C:\Program Files list = [4, 2, 1, 3, 5] totalCost = 6

public static void main(String[] args)
    {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        ArrayList<Integer> valuesAccessed = new ArrayList<>(Arrays.asList(2,4));
        int totalCost = ListUpdate.runListUpdate(valuesAccessed,list,false);
        System.out.println("list = " + list);
        System.out.println("totalCost = " + totalCost);
    }

Console X <terminated ListUpdate [Java Application) CA Program Files list = [2, 1, 4, 3, 5] totalCost = 6

Code to copy:

import java.util.ArrayList;
import java.util.Arrays;

public class ListUpdate
{
  
    /**
     * Does a linear search through the ArrayList list, returning the index of the first occurrence of
     * valToFind.
     * <p>
     * Starting from index 0, check each element in list and return the index of the first element
     * that matches valToFind.
     *
     * @param valToFind The int value to search for.
     * @param list      The ArrayList of Integers to search in.
     * @return The index of the first occurrence of valToFind in list. If list is null, or valToFind
     * is not found, return -1.
     */
    public static int linearSearch(int valToFind, ArrayList<Integer> list)
    {
       if(list==null)
           return -1;
       // do linear search
       for(int i=0;i<list.size();i++)
       {
           if(list.get(i)==valToFind)
               return i;
       }
       // if value does not found, return -1
        return -1;
    }

    /**
     * Performs the Move to Front heuristic on the element located at index in list.
     * <p>
     * Moves the element at index in list to index 0.
     * <p>
     * If the list is null, or the index is out of bounds, the method should end gracefully without
     * changing list.
     *
     * @param list The ArrayList to update.
     * @param index The element in the ArrayList to move to the front.
     */
    public static void moveToFront(ArrayList<Integer> list, int index)
    {
       // Method ends gracefully without changing list.
        if (list == null || index < 0 || index > list.size()-1) return;
        // Otherwise
        // find the value at index
        int valueToMoveToFront = list.get(index);
        // Remove element from the index position
        list.remove(index);
        // Add temporary element at index 0. This method will move other elements to right.      
        list.add(0, valueToMoveToFront);
    }
    /**
     * Performs the Transpose heuristic on the element located at index in list.
     * <p>
     * Moves the element at index to index - 1. That is, it moves the item at index one spot closer
     * to index 0.
     * <p>
     * If the list is null, or the index is out of bounds, the method should end gracefully without
     * changing list.
     *
     * @param list The ArrayList to update.
     * @param index The element in the ArrayList to move to the front.
     */
    public static void transpose(ArrayList<Integer> list, int index)
    {
       // Method ends gracefully without changing list.
        if (list == null || index < 0 || index > list.size()-1) return;
        // Otherwise
        // find the value at index
        int valueToMove = list.get(index);
        // Remove element from the index position
        list.remove(index);
        // Add temporary valueToMove at index 0. This method will move other elements to right.
        list.add(index-1, valueToMove);
    }

    /**
     * Runs either the Move to Front or the Transpose heuristic on a given list over a sequence of
     * requests, calculating the cost for accessing the items.
     * <p>
     * For each element in requests:
     * - Find its index in list. Let i be the index of the request in list.
     * - The cost to access an item at index i (0 based) is i + 1. Add this cost to a running
     * total.
     * - Apply the move to front heuristic (doMTF is true) or the Transpose heuristic (doMTF is
     * false) to list to the element at index i.
     * <p>
     * Don't duplicate code! Use your linearSearch, moveToFront, and Transpose methods!
     *
     * @param requests The sequence of request to process.
     * @param list     The list to update with each request.
     * @param doMTF    A flag that is true when the heuristic to use is Move to Front and false when it
     *                 is Transpose.
     * @return The total access cost for the all the elements of requests.
     */
    public static int runListUpdate(ArrayList<Integer> requests,ArrayList<Integer> list,boolean doMTF)
    {
       int totalCost = 0;
       // for each number in the requests
        for(int request: requests)
        {
           // Find the current number index in the list
            int i = ListUpdate.linearSearch(request,list);
            // Add the cost(i+1) of accessing an item at index i, to a running total
            totalCost = totalCost + i + 1;
            // if doMTF is true, apply the move to front heuristic
            if(doMTF)
            {
                ListUpdate.moveToFront(list,i);
            }
            // otherwise, Apply the Transpose heuristic
            else
            {
                ListUpdate.transpose(list,i);
            }          
        }
        // return the total access cost for the all the elements of requests.
        return totalCost;
    }
}

Add a comment
Know the answer?
Add Answer to:
Please help with Java array list: import java.util.ArrayList; public class ListUpdate { /** * Does a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT