Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE HELP, this is my code but i don\'t know why my code isn\'t working in the

ID: 3826870 • Letter: P

Question

PLEASE HELP, this is my code but i don't know why my code isn't working in the OUTPUT?!.. everything compiles but when i type in a number nothing happens..... Here's the question i hade to answer and with the TODO's:

Implement the methods (getData, makeReversedCopy, reverse, and makeArrayCopy) in the PA2.java class. You may rename the class. Do not modify the main method or any of the method headers. Only make changes to the method bodies.

This is what the OUTPUT suppose to look like:

But here's what mine's look like when i try to type into the OUTPUT:

import java.util.*;

public class PA2 {

public static void main(String[] args) {
  
// Part 1. read the input strings into an ArrayList
final String EOD_INDICATOR = "XXX";
Scanner inp = new Scanner(System.in);
System.out.printf("Enter data [%s indicated the end of data]: ",
EOD_INDICATOR);
ArrayList<String> data = getData(inp, EOD_INDICATOR);

// Part 2. Print the ArrayList
System.out.println("The data values are: ");
System.out.println(data);

// Part 3. Make a copy of the ArrayList with the elements in reverse
// order.
ArrayList<String> reversedData = makeReversedCopy(data);
  
// Part 4. Print the reversed copy ArrayList
System.out.println("The reversed data values are: ");
System.out.println(reversedData);

// Part 5. Reverse the elements in the reversed copy ArrayList.
reverse(reversedData);
  
// Part 6. Print the reversed copy ArrayList. The elements should
// be in the original order.
System.out.println("The reversed reversed data values are: ");
System.out.println(reversedData);

// Part 7. Make an array the contains the elements of the original
// ArrayList and in the original order.
String[] dataArray = makeArrayCopy(data);
  
// Part 8. Print the array.
System.out.println("Data Array: ");
System.out.println(Arrays.toString(dataArray));
}
  
/**
* Reads strings from the Input data source until the end of data indicator
* is read. Returns the strings read in an ArrayList. The end of data
* indicator is not included in the ArrayList.
*
* @param inp Input data source
* @param endOfDataIndicator value that indicates the end of input data.
* @return ArrayList of the values read from the input data source. The
* endOfDataIndicator is not contained in the ArrayList.
*/
private static ArrayList<String> getData( Scanner inp,
String endOfDataIndicator ) {
  
// TODO: implement method body. Do not change the method header.
ArrayList<String> data=new ArrayList<String>();
String line="";
//read a string and add to arraylist until the user enters XXX
while(!line.equals(endOfDataIndicator)){
line=inp.nextLine();
data.add(line);}
//remove XXX from the arraylist
data.remove(data.size()-1);
return data;
}
  
/**
* Make a copy of the input ArratList with the elements in reversed order
*
* @param list The ArrayList of Strings
* @return copy of the input ArrayList with the elements in reversed order.
*/
private static ArrayList<String> makeReversedCopy(ArrayList<String> list) {
  
// TODO: implement method body. Do not change the method header.
ArrayList<String>reverse=new ArrayList<String>();
for(int i=list.size()-1;i>=0;i--){
reverse.add(list.get(i));
}
return reverse;
}
  
/**
* Reverse the elements of an ArrayList of strings.
*
* @param list ArrayList of strings. The elements of this ArrayList
* are put in reverse order.
*/
  
/*
* here the parameter is passed by value so that reverse of arraylist elements can not
* be seen out side this method.
* for that the returned arraylist should be returned or arraylist(list) should be passed by reference.
*/
private static void reverse(ArrayList<String> list) {
  
ArrayList<String> reverse=makeReversedCopy(list);
list=reverse;
// TODO: implement method body. Do not change the method header.
}
  
private static String[] makeArrayCopy(ArrayList<String> list) {
  
String[] copy=new String[list.size()];
for(int i=0;i<list.size();i++){
copy[i]=list.get(i);
}
// TODO: implement method body. Do not change the method header.
return copy;
}
}

Here is a sample of the output produced by the program. Enter data [xxx indicated the end of data): orange pear apple peach read yellow green blue purple brown orange hello happy XXX The data values are orange, pear, apple, peach, read yellow green, blue, purple brown, orange, hello, happyl The reversed data values are Chappy hello orange brown purple, blue green yellow read peach apple pear orange) The reversed reversed data values are orange, pear, apple, peach, read yellow green, blue, purple brown, orange, hello, happyl Data Array: orange, pear, apple, peach, read, yellow, green, blue, purple brown, orange, hello, happyl total time 2 minutes 7 seconds BUILD SUCCESSFUL

Explanation / Answer

package myProject;

import java.util.*;
public class PA2 {
public static void main(String[] args) {
  
// Part 1. read the input strings into an ArrayList
final String EOD_INDICATOR = "XXX";
Scanner inp = new Scanner(System.in);
System.out.printf("Enter data [%s indicated the end of data]: ", EOD_INDICATOR);
ArrayList<String> data = getData(inp, EOD_INDICATOR);

// Part 2. Print the ArrayList
System.out.println("The data values are: ");
System.out.println(data);
// Part 3. Make a copy of the ArrayList with the elements in reverse
// order.
ArrayList<String> reversedData = makeReversedCopy(data);
  
// Part 4. Print the reversed copy ArrayList
System.out.println("The reversed data values are: ");
System.out.println(reversedData);

// Part 5. Reverse the elements in the reversed copy ArrayList.
reverse(reversedData);
  
// Part 6. Print the reversed copy ArrayList. The elements should
// be in the original order.
System.out.println("The reversed reversed data values are: ");
System.out.println(reversedData);
// Part 7. Make an array the contains the elements of the original
// ArrayList and in the original order.
String[] dataArray = makeArrayCopy(data);
  
// Part 8. Print the array.
System.out.println("Data Array: ");
System.out.println(Arrays.toString(dataArray));
}
  
/**
* Reads strings from the Input data source until the end of data indicator
* is read. Returns the strings read in an ArrayList. The end of data
* indicator is not included in the ArrayList.
*
* @param inp Input data source
* @param endOfDataIndicator value that indicates the end of input data.
* @return ArrayList of the values read from the input data source. The
* endOfDataIndicator is not contained in the ArrayList.
*/
private static ArrayList<String> getData( Scanner inp, String endOfDataIndicator )
{
  
   //TODO: implement method body. Do not change the method header.
   ArrayList<String> myData = new ArrayList<String>();
   String enteredData = "";
   //Read a string and add to arraylist until the user enters XXX or xxx
   do
   {
       enteredData = inp.nextLine();
       myData.add(enteredData);
   }while(!enteredData.equalsIgnoreCase(endOfDataIndicator));
          //Remove XXX from the arraylist
          myData.remove(myData.size()-1);
          return myData;
   }//End of method
  
/**
* Make a copy of the input ArratList with the elements in reversed order
*
* @param list The ArrayList of Strings
* @return copy of the input ArrayList with the elements in reversed order.
*/
private static ArrayList<String> makeReversedCopy(ArrayList<String> list)
{
  
   // TODO: implement method body. Do not change the method header.
ArrayList<String> reversedCopy = new ArrayList<String>();
//Loops from the length minus one position of list to the zero position of list and adds it to the reversedCopy
for(int i = list.size() - 1; i >= 0; reversedCopy.add(list.get(i)), i--)
   ; //Do nothing statement
return reversedCopy;
}
  
/**
* Reverse the elements of an ArrayList of strings.
*
* @param list ArrayList of strings. The elements of this ArrayList
* are put in reverse order.
*/
  
/*
* here the parameter is passed by value so that reverse of arraylist elements can not
* be seen out side this method.
* for that the returned arraylist should be returned or arraylist(list) should be passed by reference.
*/
private static void reverse(ArrayList<String> list)
{
   // TODO: implement method body. Do not change the method header.
   ArrayList<String> reverseList = makeReversedCopy(list);
   //Clears the previous list
   list.clear();
   //Loops from zero to length of the reverseList and adds it to the list
   for(int i = 0; i < reverseList.size(); list.add(reverseList.get(i)), i++)
       ;//Do nothing statement
}//End of method
  
private static String[] makeArrayCopy(ArrayList<String> list)
{
   // TODO: implement method body. Do not change the method header.
String[] duplicateCopy; //= new String[list.size()];
int counter;
for(duplicateCopy = new String[list.size()], counter = 0; counter < list.size(); duplicateCopy[counter]=list.get(counter), counter++)
   ;
return duplicateCopy;
}
}

Output:

Enter data [XXX indicated the end of data]: orange pear apple peach read yellow green blue purple brown orange hello happy xxx
The data values are:
[orange, pear, apple, peach, read, yellow, green, blue, purple, brown, orange, hello, happy]
The reversed data values are:
[happy, hello, orange, brown, purple, blue, green, yellow, read, peach, apple, pear, orange]
The reversed reversed data values are:
[orange, pear, apple, peach, read, yellow, green, blue, purple, brown, orange, hello, happy]
Data Array:
[orange, pear, apple, peach, read, yellow, green, blue, purple, brown, orange, hello, happy]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote