Skip to main content

Array List Reverse and Rotation Lab-11 George Mensom University with passed test cases

Fill in the definitions to two ArrayList utility methods in the ALUtils class which is outlined below and included in the code pack for this lab. The methods are described in more detail below.import java.util.ArrayList;
public class ALUtils{
// Creates a copy of the parameter a. Reverses the order of elements
// in the copy and returns the reversed copy. Assumes a is non-null.
public static <T ArrayList<T reverse(ArrayList<T a){
// Your definition here

}

// Creates a copy of the given ArrayList a and rotates the copy to
// the right by the given shift. Elements at high indicies wrap
// around to lower indices. Assumes parameter a is non-null and
// that shift is a non-negative number. Returns the rotated copy.
public static <T ArrayList<T rotate(ArrayList<T a, int shift){
// Your definition here

}
}In ALUtils, write a method reverse(aL) that takes any type of ArrayList and creates a reversed copy of it which is returned. The nature of this method is shown in the below demo and should produce the results indicated. You may assume that the parameter aL is non-null.public static void demo_reverse(){
ArrayList<String sa = new ArrayList<String();
String [] strings = {"A","B","C","D","E"};

// Use the a for-each to add all strings
for(String s : strings){ sa.add(s); }
// sa == [A, B, C, D, E]

ArrayList<String sb = ALUtils.reverse(sa);
System.out.println(sb);
// [E, D, C, B, A]
System.out.println(sa);
// [A, B, C, D, E] - sa is not changed


ArrayList<Integer ia = new ArrayList<Integer();
Integer [] ints = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

// Use the addAll() method add all ints from a list
ia.addAll(Arrays.asList(ints));
// ia == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

ArrayList<Integer ib = ALUtils.reverse(ia);
System.out.println(ib);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
System.out.println(ia);
// [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] - sa not changed
}In ALUtils.java write a static method rotate(aL, shift) which accepts any kind of ArrayList and creates a rotated copy of it. Rotation means to shift all elements forward in the list to a new position based on the integer parameter shift. If the elements would shift off the end of the list, they wrap around to the beginning. The concept should be familiar based on projects and is best illustrated by examples given below.aL = [ A, B, C, D, E, F] bL = [ A, B, C, D, E, F, G]
cL = rotate(aL, 2) cL = rotate(bL, 7)
cL== [ E, F, A, B, C, D] cL== [ A, B, C, D, E, F, G]

cL = rotate(aL, 7) cL = rotate(bL, 4)
cL== [ F, A, B, C, D, E] cL== [ D, E, F, G, A, B, C]

Get your solution now

Buy now

Comments

Popular posts from this blog

The Zoo Management System - entity relationship diagram & MS Access Database

Zoo Management System - Project Details: You are the employee of a big, worldwide working Zoo Management Company. Your company is responsible for the Zoo management. Your boss thinks it would be a great idea to store all data for each Zoo in a brand new self-developed ZOO Management System. Up to now, the ZOO management company has maps of each ZOO available. Your boss knows that you took a course in introduction on an ERP system, so he asks you if you could help designing such a system. Each ZOO must have the same organizational structure, which should look like this: Each Zoo has a Zoo-Address. Each Zoo has many visitors (Visitor Ticket Process (VTP). Many Zoo-Attractions belong to a Zoo. Module 1: Entity Relationship Diagram Design a ER (entity-relationship) diagram for your ZOO Management System. Use the information provided below with the entities and its attributes. Put the entities in the correct relationship to each other (organizational structure). Module 2: DB Implem...

Human Physiology by Stuart Ira Fox [PDF] (12th edition) free download

Sudoku Game Project in Java with full source code including GUI task

Introduction The purpose of the project is to try in practice the use of recursion and object-oriented program design. Please make sure to read this entire note before starting your work. Pay close attention to the sections on deadlines, deliverables, and exam rules. The problem Your task in this part of the project is to write a solver for Sudoku puzzles. Sudoku puzzles are a kind of crossword puzzles with numbers where the following two conditions have to be met: In each row or column, the nine numbers have to be from the set [1,2,3,4,5,6,7,8,9] and they must all be different.  For each of the nine non-overlapping 3x3 blocks, the nine numbers have to be from the set [1,2,3,4,5,6,7,8,9] and they ust all be different. The following two figures show a Sudoku puzzle and its solution. The input The input of your program, the Sudoku puzzles, is given in text files. More specifically, they are represented as nine lines of nine characters separated by spaces. The characters can be of two...