Question

THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...

THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN

Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program.

import java.awt.Rectangle;
import java.util.ArrayList;

public class ArrayListReverser {

        public static void main(String[] args) {
                 ArrayList<Object> myList = new ArrayList<Object>();
                 myList.add(new Rectangle(1,1));
                 myList.add(new Rectangle(2,2));         
                 myList.add(new Rectangle(3,3));
                 myList.add(new Rectangle(4,4));
                 for (Object o:myList)
                         System.out.println(o);
                 reverse(myList);
                 System.out.println("\nThe order should now be reversed:");
                 for (Object o:myList)
                         System.out.println(o);
        }

        //your method here

}

Homework Answers

Answer #1

===============================================

import java.awt.Rectangle;
import java.util.ArrayList;

public class ArrayListReverser {

   public static void main(String[] args) {
       ArrayList<Object> myList = new ArrayList<Object>();
       myList.add(new Rectangle(1, 1));
       myList.add(new Rectangle(2, 2));
       myList.add(new Rectangle(3, 3));
       myList.add(new Rectangle(4, 4));
       for (Object o : myList)
           System.out.println(o);
       reverse(myList);
       System.out.println("\nThe order should now be reversed:");
       for (Object o : myList)
           System.out.println(o);
   }
  
   //Method to reverse ArrayList
   public static void reverse(ArrayList<Object> inputArrayList) {
      
       //Check if size is greater than 1
       if(inputArrayList.size()>1) {

          
           //Get current value
           Object currentValue = inputArrayList.remove(0);
          
           //Calling recursively
           reverse(inputArrayList);
          
           //Finally add the current element at each stack to end of the list
           inputArrayList.add(currentValue);
       }
   }

}

Know the answer?
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
Similar Questions
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
Create the ArrayList program example in listing 13.1, Battlepoint. Describe the code (in your WORD document)...
Create the ArrayList program example in listing 13.1, Battlepoint. Describe the code (in your WORD document) in the 'if' statement if (targets.indexOf(shot) > -1) { ADD a NEW ArrayList of points called 'misses' ADD code to add a point to the 'misses' list on a miss (if a SHOT does NOT hit a TARGET) ADD code to place an 'M' in the final output map for all shots that were MISSES. ADD code to place an H on the target...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
Write a Java program that Reads baseball data in from a comma delimited file. Each line...
Write a Java program that Reads baseball data in from a comma delimited file. Each line of the file contains a name followed by a list of symbols indicating the result of each at bat: 1 for single, 2 for double, 3 for triple, 4 for home run, o for out, w for walk, s for sacrifice Statistics are computed and printed for each player. EXTRA CREDIT (+10 points); compute each player's slugging percentage https://www.wikihow.com/Calculate-Slugging-Percentage Be sure to avoid a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT