Menu

[Solved]Java Write Dupend Method Duplicates Last Item Arraylist Tester Dupend Public Class Arrayli Q37031251

java

write dupEnd method, that duplicates the last item in thearraylist.

the tester for the dupEnd is below

public class ArrayList<T>
{
private Object[] myArray; // array of list entries
private int mySize;
private static final int DEFAULT_INITIAL_CAPACITY = 25;

// public methods:
public ArrayList()
{
this(DEFAULT_INITIAL_CAPACITY);
} // end default constructor

public ArrayList(int initialCapacity)
{
mySize = 0;   
myArray = new Object[initialCapacity];
} // end constructor
  
  
public void add(Object newEntry)
{
myArray[mySize] = newEntry;
mySize++;
} // end add

/** Converts all the data in the bag into one big String
@return a very long String of objects contained in bag */
public String toString()
{   
int k;
String result = “”;
for (k=0; k<mySize; k++)
if (k<mySize-1)
result += myArray[k] + “, “; // up to item before last
else
result += myArray[k]; // the last item
return result;
}   
  
/** Add your additional methods here */
public void dupEnd()
{
//that duplicates the last item in the list

}

——————————————————————————————————————–

public class Tester
{
public static void main(String[] args)
{
System.out.println(“f”);
//******************** Problem 1 dupEnd method**********************
// write a dupEnd method that duplicates the last item inthe list
// BEFORE a-b-c-d AFTER a-b-c-d-d
// BEFORE x AFTER x-x
// BEFORE [empty] AFTER [empty]
// don’t worry about checking if the list has the capacity. Assumeit does.
  
System.out.println(“**************** Problem 1*********************n”);
ArrayList list1 = new ArrayList();
list1.add(“a”); list1.add(“b”); list1.add(“c”); list1.add(“d”);

System.out.println(“list1 = ” + list1);
       list1.dupEnd();
System.out.println(“After dupEnd list1 should contain a-b-c-d-d”);
System.out.println(“list1 = ” + list1);

ArrayList list2 = new ArrayList();
list2.add(“x”);

System.out.println(“nlist2 = ” + list2);   
       list2.dupEnd();
System.out.println(“After dupEnd list2 should contain x-x “);
System.out.println(“list2 = ” + list2);
  
ArrayList list3 = new ArrayList(); // an empty list
  
list3.dupEnd();
System.out.println(“nAfter dupEnd list3 should contain nothing”);
System.out.println(“list3 = ” + list3);
  
System.out.println(“————————-n”);
  
//*************************** END Problem 1***********************************/

Expert Answer


Answer to java write dupEnd method, that duplicates the last item in the arraylist. the tester for the dupEnd is below public clas… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *