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

2. Write a Java program that consists of a definition for a

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

3.54 Rating (154 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

java import javautilArrayList public class Neighbours pr... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!