Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, lets define a Printer class.</p> <pre><code>package com.ggl.modeltest; public class Printer { private String companyName; private String printerName; public Printer(String companyName, String printerName) { this.companyName = companyName; this.printerName = printerName; } public String getCompanyName() { return companyName; } public String getPrinterName() { return printerName; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((companyName == null) ? 0 : companyName.hashCode()); result = prime * result + ((printerName == null) ? 0 : printerName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Printer other = (Printer) obj; if (companyName == null) { if (other.companyName != null) return false; } else if (!companyName.equals(other.companyName)) return false; if (printerName == null) { if (other.printerName != null) return false; } else if (!printerName.equals(other.printerName)) return false; return true; } public Printer copy() { return new Printer(companyName, printerName); } } </code></pre> <p>This class has a constructor to define the values, and two getter methods to retrieve the values. Once you create an instance of this class, you can't change it. </p> <p>These types of non modifiable classes make debugging simpler.</p> <p>The hashCode and equals methods look pretty scary. I had Eclipse generate those methods for me. The reason that we override these methods is that we're going to use them in the <code>Computer</code> class.</p> <p>So now, we define the <code>Computer</code> class.</p> <pre><code>package com.ggl.modeltest; import java.util.ArrayList; import java.util.List; public class Computer { private List&lt;Printer&gt; printerNames; private String computerName; public Computer(String computerName) { this.computerName = computerName; this.printerNames = new ArrayList&lt;Printer&gt;(); } public void addPrinter(Printer printerName) { this.printerNames.add(printerName); } public void removePrinter(Printer printerName) { for (int i = printerNames.size() - 1; i &gt;= 0; i--) { if (printerNames.get(i).equals(printerName)) { printerNames.remove(i); } } } public List&lt;Printer&gt; getPrinterNames() { return printerNames; } public String getComputerName() { return computerName; } } </code></pre> <p>The constructor takes the name of the computer. Once an instance is constructed, there's no way to change the name of the computer.</p> <p>Printers are added to and removed from a computer. Two methods have been defined to add a printer to the computer, and remove a printer from a computer.</p> <p>The remove method removes all instances of the printer from the <code>List</code>, in case more than one was entered.</p> <p>The method that gets the printer list returns an instance of the <code>List</code>. The calling program can change the contents of this List. If you want to make sure that the calling program can't change the contents of the list, you make a deep copy of the <code>List</code>.</p> <p>To make a deep copy, you write a copy method for the <code>Printer</code> class.</p> <pre><code>public Printer copy() { return new Printer(companyName, printerName); } </code></pre> <p>Since strings are unchangeable, we don't have to make a copy of the strings. If we had changeable values, we would have to make a copy of them as well. That's why this is called a deep copy. It can be difficult to get everything copied correctly the first time. That's what testing is for.</p> <p>Then you call this copy method in a loop in the <code>getPrinterNames</code> method of the <code>Computer</code> class.</p> <pre><code>public List&lt;Printer&gt; getPrinterNames() { List&lt;Printer&gt; list = new ArrayList&lt;Printer&gt;(); for (Printer printer : printerNames) { list.add(printer.copy()); } return list; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload