Question: Java Exercise Exercise 2 - Comparing Content of Objects 1. Add a public method equals(ClassA otherClass) to class classA that returns the value true if
Java Exercise









Exercise 2 - Comparing Content of Objects 1. Add a public method equals(ClassA otherClass) to class classA that returns the value true if (i) this object and otherClass have the same value in variable numltems and (ii) both arritems arrays are null or both store the same values and in the same order in the first numitems entries; the method returns the value false otherwise. Do not try to access values from variable arrltems if arritems is null. 2. Add a public method equals(DataTypes otherType) to class DataTypes that returns the value true if (i) all primitive instance variables in this object and other Type have the same values and (ii) variables varA in this object and otherType are equal according to method equals of class ClassA. 3. Run the java class TestEquals. Fix your code if it fails any of the 4 tests. DataTypes.java TestDataType... J TestEquals.java ClassA.java X J ClassB.java ClassC.java CountObject... 1 ublic class ClassA { 2 private final int SIZE_ARRAY = 5; 3 public int numItems; 4 public int[] arrItems; 5 60 public ClassA(int n, int[] arr) { 7 numItems = n; 8 arrItems = arr; 9 } 10 110 public ClassA() { 12 numItems = 0; 13 arrItems = new int[SIZE_ARRAY]; 14 } 15 } 16 J DataTypes.java J TestDataType... TestEquals.java ClassA.java ClassB.java X J ClassC.java CountObject... 1| 2 public class Class { 3 4 private int counter = 0; 5 6e public Class() { 7 counter = 1; 8 } 9 100 public void incCounter() { 11 ++counter; 12 } 13 140 public int getCounter() { 15 return counter; 16 } 17 180 public static void main(String[] args) { 19 int i; 20 21 i = counter; 22 incCounter(); 23 System.out.println("i = + i + counter = 24 } 25 26 } 27 + counter); 3 DataTypes.java TestDataType... TestEquals.java J ClassA.java J ClassB.java ClassC.java X CountObject... 1| 2 public class ClassC { 3 4 private int counter = 0; 5 60 public Class() { 7 counter = 1; 8 } 9 100 public void incCounter() { 11 ++counter; 12 } 13 140 public int getCounter() { 15 return counter; 16 } 17 18 } 19 TestEquals.java 5 6 ClassA.java ClassB.java J ClassC.java CountObject... X DataTypes.java TestDataType... 1 2 public class CountObjects { 3 40 public static void main(String[] args) { int numObjects; Class C = null; 7 for (int i = 0; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
