Question: 2. Write a Java program that consists of a definition for a public class called Neighbours. This should have one constructor, which takes a String[]

2. Write a Java program that consists of a definition for a public class called Neighbours. This should have one constructor, which takes a String[] as its parameter. The constructor should use a loop to copy the strings of the supplied array into an ArrayList of Strings, which should be a class variable. A method called getNeighbours should also be written. This takes a String and an int parameter, and identifies the 'neighbours' of the specified String in the ArrayList. The returned value is a string. If the int parameter is 1, the returned String is just the two nearest neighbours concatenated together. If the int parameter is 2, the returned String should be the four nearest neighbours concatenated together in the order they appear in the ArrayList), and so on. If requested neighbours do not exist, the method should return the empty string". To make writing getNeighbours easier, you should also write a method called 'get' which returns the relevant element of the ArrayList when given a valid index, and the empty string otherwise. This method should take a single int parameter specifying the index in question. [50 marks]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
