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...
-
What was IBM's accounting effective tax rate for 2012? What items caused the company's accounting effective tax rate to differ from the "hypothetical" tax rate of 35%? What was the company's cash...
-
Last month you assumed the position of manager for a large car dealership. The distinguishing feature of this dealership is its "no hassle" pricing strategy; prices (usually well below the sticker...
-
Does Fairmont have any contactor personnel whose have terminated but are being paid through payroll after termination (e.g., ghost employees)?
-
Gordon is the only limited partner in Bushmill Ventures, a limited partnership whose general partners are Daniels and McKenna. Gordon contributed $10,000 for his limited partnership interest and...
-
Explain briefly why countries trade If two countries, Chile and Agentina start to trade, what will determine what each country will exportor import? Explain why countries do not produce everything...
-
Go to the Internet and find one example of how an HR professional can use analytics to improve the HR department's training program and its succession and career planning efforts.
-
Would a projects NPV for a typical firm be higher or lower if the firm used bonus depreciation rather than straight-line depreciation? Explain.
-
What role do incremental cash flows play in a replacement analysis?
-
Digital Software, Inc., prefers to hire Asian Americans, because, according to its personnel director, theyre smarter and work harder than others. Showing a preference for one group over another is...
-
Why might there be two different component costs for common equity? Which one is generally relevant, and for what type of firm is the second one likely to be relevant?
-
Would you expect an abandonment option to increase or decrease a projects expected NPV and risk (as measured by the coefficient of variation)? Explain.
-
robability that it is either a spa 1 -) Evaluate - dx 1+ sin x tan x sec? x dx 1- tan2 x Evaluate Find two numbers whose su Ify= (x+sinx) (x- co
-
This problem continues the Draper Consulting, Inc., situation from Problem 12-45 of Chapter 12. In October, Draper has the following transactions related to its common shares: Oct 1 Draper...
-
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...
-
Please reflect on and explain the role and usefulness of the concept of SD in relation to the protection of the environment.
-
Has the concept of SD achieved the balance between all three pillars: environmental protection; economic development; and social issues?
-
What, if any, is the normative content of the concept of SD?
Study smarter with the SolutionInn App