2. Write a Java program that consists of a definition for a public class called Neighbours....
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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] 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]
Expert Answer:
Answer rating: 100% (QA)
java import javautilArrayList public class Neighbours pr... View the full answer
Related Book For
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch
Posted Date:
Students also viewed these programming questions
-
Predictive text entry systems are familiar on touch screens and mobile phones. This question asks you to consider how the same principles might be used in a programming editor for creating Java code....
-
Let r and s be solutions to the quadratic equation x 2 b x + c = 0. For n N, define d0 = 0 d1 = r s dn = b dn1 c dn2 (n 2) Prove that dn = r n s n for all n N. [4 marks] (b) Recall that a commutative...
-
Host A has a file of 49.5Kbytes to send to host C and host B has a file of 70.5Kbytes to send to host D, both using TCP. TCP connection 1 from A to C and TCP connection 2 from B to D share one...
-
How would your answer to Question 66 change if management determined that there was only a "50/50" chance any portion of the $1,000,000 DPADwould be sustained on audit?
-
The journal Networks periodically publishes studies on the reliability of flow networks. For example, Networks (September 2007) provided applications in mobile ad hoc and sensor networks. Consider a...
-
How is the IT infrastructure different from the IT architecture?
-
High Frequency manufactures and sells MP-3 players. Information on last years operations (sales and production of the 2010 model) follows. Sales price per unit ....... $70 Costs per unit Direct...
-
These financial statement items are for Sheridan Company at year-end, July 31, 2022. Salaries and wages payable $1,500 Notes payable (due after 2023) $1,600 Salaries and wages expense 52,000 Cash...
-
The nominal accounts of San Antonio Clinic for the year ended December 2022 are listed below in alphabetical order: Depreciation expense Supplies Interest expense Contributions (from philanthropic...
-
A pump must pump 1500gpm against a total head of 40 ft. What horsepower is required for this work? (round to the nearest tenth)
-
If $2200 grows to $2376 in 4 years in a simple interest account, what was the interest rate?
-
Solve for the value of the complex number in rectangular form. tan ((11))
-
The doctor ordered Zovirax 15 mg/kg. The patient weighs 121 lbs. The available supply is 100 mg/2 mL. How many mL will the nurse administer per dose?
-
Let L be an observable. Show that (AL) ((L-r)) for any rR, with equality when a T= = (L).
-
Propose an algorithm that accepts a sequence of labeled training examples and outputs a hypothesis h H that is consistent with all the training examples, if one exists. If there are no consistent...
-
What are the two methods used to translate financial statements and how does the functional currency play a role in determining which method is used?
-
Repeat the previous programming project, but read the input data from a file and send the output to another file. If you have covered binary files, use binary files; otherwise, use text files. Read...
-
Write a fragment of code that will compute the sum of the first n positive odd integers. For example, if n is 5, you should compute 1 + 3 + 5 + 7 + 9.
-
Write a program similar to the one in Listing 10.10 that can write an arbitrary number of Species objects to a binary file. (Species appears in Listing 5.19 of Chapter 5.) Read the file name and the...
-
An undamped single-degree-of-freedom system consists of a mass \(5 \mathrm{~kg}\) and a spring of stiffness \(2000 \mathrm{~N} / \mathrm{m}\). Find the response of the system using Eq. (2.21) when...
-
An undamped single-degree-of-freedom system consists of a mass \(10 \mathrm{~kg}\) and a spring of stiffness \(1000 \mathrm{~N} / \mathrm{m}\). Determine the response of the system using Eq. (2.21)...
-
Find the response of the system described in Problem 2.60 using Eq. (2.23). Data From Problem 2.60:- An undamped single-degree-of-freedom system consists of a mass \(10 \mathrm{~kg}\) and a spring of...
Study smarter with the SolutionInn App