Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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
Step: 1
java import javautilArrayList public class Neighbours pr...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started