Question

I am still new to Java Language, so please comment out lines so I can understand...

I am still new to Java Language, so please comment out lines so I can understand easily! thank you so much

The RecursionP2 class will have only one class level variables: an ArrayList of Integers that will store a data set on which you will be performing different operations using recursive function. All these functions can also be done using iteration, however, you are restricted to only use recursion for full credit. Your program might be randomly checked and any non-recursive approach will be marked down.

The following non-static public methods have to be declared in your class:

  • Constructor (1-arg)

    • The constructor will accept in an 1-D array of type int of unknown length and add every element to the class level ArrayList.
  • reverseList(ArrayList)

    • The method will accept in an ArrayList of type Integer and return a new ArrayList of type Integer that has all the elements in the reverse order. You are only allowed to use recursion for this method.
  • reverseList()

    • The method will use the class level ArrayList and return a new ArrayList of type Integer that has all the elements in the reverse order. You are only allowed to use recursion for this method.
    • Hint: This method is a special case of reverseList(ArrayList) and you are allowed to use that method if it helps.
  • toOddList(ArrayList )

    • The method will use the class level ArrayList as a parameter and return a new ArrayList of type Integer that contains all the odd indexed numbered elements of the class level ArrayList. You are only allowed to use recursion for this method.
  • toOddList()

    • The method will accept in an ArrayList of type Integer and return a new ArrayList of type Integer that contains all the odd indexed numbered elements of the class level ArrayList.
    • Hint: This method is a special case of toOddList(ArrayList) and you are allowed to use that method if it helps.
  • toEvenRevList(ArrayList )

    • The method will accept in an ArrayList of type Integer as a parameter and return a new ArrayList of type Integer that contains all the even indexed numbered elements of the class level ArrayList in reverse order. You are only allowed to use recursion for this method.
    • You can use the reverseList() method as a helper method.
  • toEvenRevList()

    • The method will use the class level ArrayList and return a new ArrayList of type Integer that contains all the even indexed numbered elements of the class level ArrayList in reverse order. You are only allowed to use recursion for this method.
    • Hint: You can use toEvenRevList(ArrayList) or reverseList(ArrayList) as a helper method.
  • retPenultimate(ArrayList )

    • The method will accept in an ArrayList of type Integer and return an int which is the last element of the ArrayList.If the list is empty/null, it should return -1. As usual, you are only allowed to use recursion for this method and the use of reverseList() is prohibited.
  • getList()

    • The method would return the class level ArrayList. The return type is ArrayList.

Example

Original: [2, 4, 6, 8, 10]
reverseList(): [10, 8, 6, 4, 2]
toOddList(): [4, 8]
toEvenRevList(): [10, 6, 2]
retPenultimate(): 10 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// do comment if any problem arises

// code

import java.lang.annotation.RetentionPolicy;

import java.util.*;

class RecursionP2 {

    // class variable

    ArrayList<Integer> array_list;

    // constructor

    RecursionP2(ArrayList<Integer> list) {

        // create arralist

        array_list = new ArrayList<>();

        // copy given list to arraylist

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

            array_list.add(list.get(i));

    }

    // reverse given list

    public ArrayList<Integer> reverseList(ArrayList<Integer> list) {

        if (list.size() == 0)

            return new ArrayList<>();

        // call reverselist

        ArrayList<Integer> temp = reverseList(new ArrayList<>(list.subList(1, list.size())));

        // append 1st element to temp

        temp.add(list.get(0));

        return temp;

    }

    // this function returns reversed list in current object

    public ArrayList<Integer> reverseList() {

        return reverseList(this.array_list);

    }

    // temporary helper variable

    boolean i = false;

    public ArrayList<Integer> toOddList(ArrayList<Integer> list) {

        if (list.size() == 0) {

            i = false;

            return new ArrayList<>();

        }

        i = !i;

        boolean j = i;

        ArrayList<Integer> temp = toOddList(new ArrayList<>(list.subList(1, list.size())));

        if (!j) {

            ArrayList<Integer> temp1 = new ArrayList<>();

            temp1.add(list.get(0));

            // copy temp to temp1

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

                temp1.add(temp.get(i));

            // insert first element

            return temp1;

        }

        return temp;

    }

    public ArrayList<Integer> toOddList() {

        return toOddList(this.array_list);

    }

    // returns even list

    public ArrayList<Integer> toEvenRevList(ArrayList<Integer> list) {

        if (list.size() == 0) {

            i = false;

            return new ArrayList<>();

        }

        i = !i;

        boolean j = i;

        ArrayList<Integer> temp = toEvenRevList(new ArrayList<>(list.subList(1, list.size())));

        if (j) {

            temp.add(list.get(0));

        }

        return temp;

    }

    public ArrayList<Integer> toEvenRevList() {

        return toEvenRevList(array_list);

    }

    // returns last element

    public int retPenultimate(ArrayList<Integer> list) {

        if (list.size() == 1)

            return list.get(0);

        return retPenultimate(new ArrayList<>(list.subList(1, list.size())));

    }

    public ArrayList<Integer> getList() {

        return array_list;

    }

    public static void main(String[] args) {

        // testing above class

        RecursionP2 testing = new RecursionP2(new ArrayList<Integer>(Arrays.asList(2, 4, 6, 8, 10)));

        System.out.println("Original: " + (testing.getList()));

        System.out.println("reverseList(): " + (testing.reverseList()));

        System.out.println("toOddList(): " + (testing.toOddList()));

        System.out.println("toEvenRevList(): " + (testing.toEvenRevList()));

        System.out.println(

                "revPenultimate(): " + testing.retPenultimate(new ArrayList<Integer>(Arrays.asList(2, 4, 6, 8, 10))));

    }

}

Output:

Add a comment
Know the answer?
Add Answer to:
I am still new to Java Language, so please comment out lines so I can understand...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Do It With Java. Make it copy paste friendly so i can run and test...

    Please Do It With Java. Make it copy paste friendly so i can run and test it. Thnak You. Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates...

    java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates the sum of all Integer values in a given ArrayList. For example, if ArrayList<Integer> list contains {1, 2, 3}, a call to sumList(list) should return 1+2+3, which is 6 as an Integer. Return null if the list is empty or null. Think about the base case. When should the method terminate/end? Under what condition should your method stop making recursive calls and return your...

  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

  • C# QUESTION Requirements Your task is to write a program that will print out all the s of the com...

    C# QUESTION Requirements Your task is to write a program that will print out all the s of the command line arguments to a program For example, given the arguments 'gaz, wx,and 'edc, your program should output wsxaazea Your implementation is expected to use Heap's algorithm according to the following pseudocode: procedure generatelk: integer, A: array of any): if k 1 then output A) else // Generate permutations with kth unaltered //Initially k length(A) // Generate permutations for kth swapped...

  • Java language Any use of java.util.LinkedList is prohibited Objective: The goal of this assignment is to...

    Java language Any use of java.util.LinkedList is prohibited Objective: The goal of this assignment is to practice recursion. ignment: The assignment requires writing recursive methods for some linked list operations. The use of loops in the recursive methods is strictly prohibited in this assignment. That is, you cannot use for, while, and do-while in the recursive methods you will write. You can only use a loop when you will initiate values for a linked list in the main method. You...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of...

    iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist vii.remove all the elements at any odd index of the original arraylist (not the cloned one) viii.print out the original arraylist ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order) xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT