An example of using the ArrayList Class Assignment Help

Assignment Help: >> The ArrayList class >> An example of using the ArrayList Class

An example of using the ArrayList Class:

We now develop some code based on an ArrayList in order to give you an idea of the power of the class library.

We construct a data structure that contains the details of a computer, together with all the computers to which it is connected in a local network.

The first part of the class, which describes the linkage, is shown below:

class LinkingInfo
{
private String computer;
private ArrayList<String> linkedComputers;
...
}

The string computer contains the name of the computer whose details are documented by this class. The ArrayList referenced by linkedComputers contains the names of the computers that are linked to that computer.

We will assume that a number of methods are required. These are specified below.

  • String getComputerName() returns the name of the computer whose details are in this object.
  • int getNumberOfLinks() returns the number of computers that are linked to the first computer.
  • boolean isLinked(String comp1) returns true if the computer comp1 is linked to the computer documented by this object; otherwise it returns false.
  • void addComputer(String comp1) adds the name of computer comp1 to the list of linked computers. If the computer is already in the list of linked computers, then a ComputerErrorException exception is raised with the message 'Computer already in links'.
  • void removeComputer(String comp1) removes the computer named comp1 from the linked computers. If the computer is not in the list of linked computers, then a ComputerErrorException exception is raised with the message 'Computer not in links'.

We assume for now that ComputerErrorException has been defined elsewhere. Recall that raising an exception is not the preferred way of detecting predictable error situations, like those above that could give rise to the exception ComputerErrorException. Programs should check beforehand, using the method isLinked, to avoid causing an exception when adding or removing computers from the linked computers.

We shall define two constructors. The first is provided with the name of the computer for which the linking information is to be held and an initial value of the number of locations to hold linked computers. The second has a single argument, the name of the computer, and uses the default initial capacity. The code for these two constructors is shown below:

public LinkingInfo (String computerName, int capacity)
{
linkedComputers = new ArrayList<String>(capacity);
computer = computerName;
}

public LinkingInfo (String computerName)
{
linkedComputers = new ArrayList<String>();
computer = computerName;
}

We now need to develop some of the methods. The code for the first two methods is shown below. Method getComputerName is trivial as it merely returns the name of the computer.

public String getComputerName ()
{
return computer;
}

public int getNumberOfLinks ()
{
return linkedComputers.size();
}

The getNumberOfLinks method returns the number of linked computers. This uses the size method, which returns the number of items in an ArrayList. This is why we did not need an instance variable for the LinkingInfo class to record the number of computers that are linked.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your An example of using the ArrayList Class homework and assignments? Live An example of using the ArrayList Class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer An example of using the ArrayList Class homework help, java assignment help and An example of using the ArrayList Class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd