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
Special considerations must be given to systems using liquid hydrogen for fuel because of: A. Liquid hydrogen's low density B. L
vitfil [10]

Answer:

D.All of the above

Explanation:

Properties of hydrogen:

1.It is lighter than air.It has density about 0.089 g/L.

2.Hydrogen  rapidly change from liquid state to gas,so special protection is required to protect it.

3.It is highly flammable gas.

4.Liquid form of hydrogen exits at -432 F .This is very low temperature so special protection requires to keep it in liquid form.          

6 0
3 years ago
Help me Pls
cupoosta [38]

Answer:

you got this

Explanation:

try this relax your brain

6 0
3 years ago
Hãy xác định điện áp ngõ ra Về và các dòng điện chạy
Vitek1552 [10]

Answer:

furi has a good reputation for any other business in a few hours of this period of time as per it and the company is not a good place to start and it will have a lot of experience and you have to make a good recovery and you can do something that you can get a lot to be able and you have a lot to

3 0
3 years ago
The way most recursive functions are written, they seem to be circular at first glance, defining the solution of a problem in te
EastWind [94]

Question Continuation

int factorial(int n) {

if(n == 0)

return 1;

else

return n * factorial(n - 1);

}

Provide a brief explanation why this recursive function works.

Show all steps involved in calculating factorial(3) using the function defined.

Answer:

1. Brief explanation why this recursive function works.

First, the recursive method factorial is defined.

This is the means through with the machine identifies the method.

The method is defined as integer, the machine will regard it as integer.

When the factorial is called from anywhere that has access to it, which in this case is within the factorial class itself. This means you can call it from the main method, or you can call it from the factorial method itself. It's just a function call that, well, happens to call itself.

2. Steps to calculate factorial(3)

1 First, 3 is assigned to n.

2. At line 2, the machine checks if n equals 0

3. If yes, the machine prints 1

4. Else; it does the following from bottom to top

factorial(3):

return 3*factorial(2);

return 2*factorial(1):

return 1;

Which gives 3 * 2 * 1 = 6

5. Then it prints 6, which is the result of 3!

6 0
3 years ago
A car is alone at a red light. When the lights turn green, it's starts at constant acceleration after traveling 100. M, it's is
Leto [7]

The acceleration of the car is 1.125 m/s².

Explanation:

Acceleration refers to the change in velocity of an object per unit time.

There are three basic formulae linking initial velocity ("u"), final velocity ("v"), distance ("s"), time ("t") and acceleration ("a"). They are-

  1. v²=u²+2as
  2. s =u*t+¹/₂(a*t²)
  3. v =u+a*t

All the variable carry the meaning as explained above using formula no 1

Since the car starts from rest (initial velocity "u"=0)

Putting "u"=0 in the formula 1

we get v²=2*a*s

Thus acceleration "a" can be written as a=v²/2s

Putting the value of "v" as 15 m/s and s= 100 m (given in the question)

a=15*15 /(2*100)

a=1.125 m/s²

Thus the acceleration of the car is 1.125 m/s²

3 0
3 years ago
Other questions:
  • A hypodermic syringe is used to apply a vaccine. If the plunger is moved forward at the steady rate of 20 mm/s and if vaccine le
    14·1 answer
  • A cylindrical specimen of a brass alloy having a length of 60 mm (2.36 in.) must elongate only 10.8 mm (0.425 in.) when a tensil
    9·1 answer
  • What is poop made out of
    13·2 answers
  • 4. (3 pts) Sketch cylinder/cylinder head configurations to show the differences between PFI and GDI gasoline injection systems.
    5·2 answers
  • A metal crystallizes with a face-centered cubic lattice. The edge of the unit cell is 408 pm. Calculate the number of atoms in t
    13·1 answer
  • An automotive fuel cell consumes fuel at a rate of 28m3/h and delivers 80kW of power to the wheels. If the hydrogen fuel has a h
    15·1 answer
  • What method is most likely to be used to measure the
    15·1 answer
  • Linear dimensioning commands are used to dimension _____.
    11·1 answer
  • What is the equation for photosynthesis​
    12·2 answers
  • Two basic types of mechanical fuel injector systems?​
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!