1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
gtnhenbr [62]
4 years ago
5

In this homework problem we’ll begin completing an implementation of SimpleList that uses a linked list of Item objects internal

ly. We’ve provided some starter code in a class called SimpleLinkedList. However, the get and set methods rely on a helper function called getItem to work properly, which is not properly implemented yet. You should create a class called YourSimpleLinkedList that inherits from SimpleLinkedList and provide a correct implementation of getItem. getItem should return a reference to the Item at that position in the list, or null if no such item exists. You may want to look at the implementation of size in SimpleLinkedList for a reminder of how to walk a linked list using a for loop. You will also need to provide a constructor for YourSimpleLinkedList that takes an array of Object references and simply passes them to a call to super to properly initialize SimpleLinkedList. A portion of the implementation of SimpleLinkedList follows:
Engineering
1 answer:
balu736 [363]4 years ago
5 0

The solution contains multiple java files. The initial linked list is 10 20 30 40 . The modified list is 20 30 40

<u>Explanation</u>

//Define the interface SimpleList;

public interface SimpleList

{

   public Object get (int index);

   public void set (int index, Object element);

   public void add (int index, Object element);

   public Object remove(int index);

   public int size();

}

SimpleLinkedList.java:

//Define the class SimpleLinkedList that implements the SimpleList.

public class SimpleLinkedList implements SimpleList

{

   //Define the Item class.

   class Item

   {

       Object value;

       Item next;          

       Item (Object setValue, Item setNext)

       {

           value = setValue;

           next = setNext;

       }

   }      

   protected Item start;

   protected int currentSize;      

   //Define the default constructor.

   public SimpleLinkedList()

   {

       currentSize = 0;

       start = null;

   }      

   //Define the parameterized constructor.

   SimpleLinkedList(Object[] values)

   {

       currentSize = 0;          

       //Start the loop and call the add method.

       for(int i = 0; i<values.length;i++)

       {

           add(i, values[i]);

       }

   }      

   //Define the method to return the object

   // at the given index.

   atOverride

   public Object get (int index)

   {

       //Call the getItem() method and return it's value.

       return getItem(index).value;

   }      

   //Define the set() method.

   atOverride

   public void set (int index, Object element)

   {

       if (index < 0 || index >= currentSize)

       {

           return;

       }

       int currentIndex = 0;          

       for (Item current = start; current != null; current = current.next)

       {

           if (currentIndex == index)

           {

               current.value = element;

               break;

           }

           currentIndex++;

       }

   }    

   //Define the getItem() method.

   protected Item getItem(int index)

   {

       if (index < 0 || index >= currentSize)

       {

           return null;

       }      

       int currentIndex = 0;        

       for (Item current = start; current != null; current = current.next)

       {

           if (currentIndex == index)

           {

               return current;

           }

           currentIndex++;

       }

       return null;

   }      

   //Define the add() method.

   atOverride

   public void add (int index, Object toAdd)

   {

       if (index == 0)

       {

           start = new Item (toAdd, start);

           currentSize++;

           return;

       }        

       Item previousItem = getItem(index - 1);        

       if(previousItem == null)

       {

           return;

       }          

       Item newItem = new Item(toAdd, previousItem.next);

       previousItem.next = newItem;

       currentSize++;

   }      

   //Define the method to return the size of the linked list.

   atOverride

   public int size ()

   {

       return currentSize;

   }      

   //Define the method to remove an index from the linked list.

   atOverride

   public Object remove (int index) {

       // TODO Auto-generated method stub

       return null;

   }      

}

YourSimpleLinkedList.java:

//Define the YourSimpleLinkedList class.

public class YourSimpleLinkedList extends SimpleLinkedList

{

   //Define the default constructor.s

   public YourSimpleLinkedList()

   {

       super();

   }      

   //Define the parameterized constructor.

   YourSimpleLinkedList(Object[] values)

   {

       super(values);

   }

   //Define the method to remove an index.

   at Override

   public Object remove(int index)

   {

       //Return null if the index is out of range.

       if (index < 0 || index >= currentSize)

       {

           return null;

       }        

       //Set the start Item if the first value

       // is to be removed.

       if (index == 0)

       {

           //Store the value of the node to be removed.

           Object temp = start.value;

           

           //Set the start Item.

           start = start.next;

           

           //Update the size of the linked list and return

           // the deleted value.

           currentSize--;

           return temp;

       }        

       //Initialize the required variables.

       int currentIndex = 0;

       Item current = start;

       Item prev = start;        

       //Start the loop to traverse the list.

       while (current != null)

       {

           //Check the index value.

           if (currentIndex == index)

           {

               //Store the value to be deleted.

               Object temp = current.value;                

               //Set the next pointer.

               prev.next = current.next;                  

               //Update the size of the linked list and return

               // the deleted value.

               currentSize--;

               return temp;

           }            

           //Update the values.

           currentIndex++;

           prev = current;

           current = current.next;

       }

       return null;

   }

}

Main.java:

public class Main

{

   public static void main(String[] args)

   {

       //Create a list of objects.

       Object [] values = {10, 20, 30, 40};          

       //Create an object of the YourSimpleLinkedList class.

       YourSimpleLinkedList myList = new YourSimpleLinkedList(values);        

       //Display the initial list.

       System.out.print("The initial linked list is ");

       for (int i=0;i<myList.size();i++)

       {

           System.out.print(myList.get(i) + " ");

       }        

       //Remove an index from the list.

       myList.remove(0);        

       //Display the modified list.

       System.out.println();

       System.out.print("The modified list is ");

       for (int i=0;i<myList.size();i++)

       {

           System.out.print(myList.get(i) + " ");

       }

   }  

}

You might be interested in
How many points do you need to define for the rectangle
Rudik [331]

Answer:

Two points

Explanation:

3 0
3 years ago
“In a List of Positive Integers, Set MINIMUM to 1. For each number X in the list L, compare it to MINIMUM. If X is smaller, set
artcher [175]

Answer:

no i cant answer it sorry

7 0
3 years ago
Design an op amp circuit to average the input of six sensors used to measure temperature in restaurant griddles for a large fast
Marina CMI [18]

Answer:

See the attached file for the design.

Explanation:

Find attached for the explanation.

3 0
4 years ago
An important concept to aid understanding of electromagnetics is electrical length. Electrical _________ is a unitless measure t
kondor19780726 [428]

Answer: Electrical length is a unitless measure that refers to the length of a wire or device at a certain frquency. It is defined as the ratio of the physical length of the device to the wavelength of the signal frequency.

Explanation:

In nature, the maximum speed achievable for any interaction or perturbation along a medium (like a wire) can't be higher than the speed of light, otherwise it would be violating Einstein's Relativity Theory.

So, in a given circuit, the voltage and current aren't set up instantaneously, and a finite time exists since a battery is connected to a circuit till a stable current  traverses a load resistor connected to it.

Now, the electrical length, defines how important in is this delay, in the calculation of the voltage through the resistor, for instance.

We can write the electrical length (L.E.) as follows:

L.E. = L / (v/f) = L / λ

As we can see, the effect depends not only on the circuit dimensions, but the frequency of the signal as well.

If L.E. is much smaller than λ, this means that we can neglect the effects of the delay, and we can use the circuit theory, namely KVL, KCL and Ohm's law to analyze the circuit, assuming that the current establishes instantaneously.

Otherwise, the current and voltage concepts are not valid anymore, as we need to think in terms of propagation of electromagnetic waves, like in transmission lines and antennas.

Roughly, if L.E. ≤ 20 λ, we say that we are still in the realm of the circuit theory.

3 0
3 years ago
PLEASE HURRY!!!
Marizza181 [45]

Answer:

A

Explanation:

I just took the test. Plus I'm built different ;]

5 0
3 years ago
Other questions:
  • Main technologies used in atms vending machines game consoles and microwave ovens
    6·1 answer
  • Consider a single crystal of some hypothetical metal that has the BCC crystal structure and is oriented such that a tensile stre
    10·1 answer
  • The 10 foot wide circle quarter gate AB is articulated at A. Determine the contact force between the gate and the smooth surface
    10·1 answer
  • Problem 2. The length of a side of the square block is 4 in. Under the application of the load V, the top edge of the block disp
    6·1 answer
  • In C++ the declaration of floating point variables starts with the type name float or double, followed by the name of the variab
    15·1 answer
  • Which of the following describes a polar orbit?
    7·1 answer
  • Find the value of P(-1.5≤Z≤2)
    12·1 answer
  • PLEASE ANSWEAR FAST!!! <br> What does it mean if E˂1
    13·1 answer
  • Pls help me it’s due today
    8·1 answer
  • Hey any one ride dirtbikes here
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!