Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please solve exercise 2. question 1 is to be added to the ClassA file. Question 2 Is to be added to the DataTypes file pu

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Please solve exercise 2. question 1 is to be added to the ClassA file. Question 2 Is to be added to the DataTypes file
image text in transcribed
pu File: Exercise 2 - Comparing Content of Objects 1. Add a public method equals(ClassA otherClass) to class class that returns the value true if this object and otherClass have the same value in variable numitems and (H) both arritems arrays are null or both store the same values and in the same order in the (default package 6 70 > ClassA.java 8 > D DataTypes.ja 9 > TestDataType 10 > TestEquals je 11 12 13 14 15 316 17 18 19 pul Arr LAB 2 CS1027 Computer Science Fundamentals II first numitems entries the method returns the value false otherwise. Do not try to access values from variable arritems if arritems is null 2. Add a public method equals(DataTypes other type) to class DataTypes that returns the value true) al primitive instance variables in this object and other Type have the same values and ) viables var in this object and otherType are equal according to method equals of class Class 2. Run the java class TestEquals, Fox your code if it fails any of the 4 tests. FOO ClassA.java public class Class { private final int SIZE_ARRAY = 5; public int numitems; public int[] arritems; public Class(int n, int[] arr) { numItems = n; arritems = arr; 3 JO public Class() { numItems = 8; arrItems = new int[SIZE_ARRAY); > 3 I owl.uwo.ce DataTypes.java public class DataTypes { public int intVar: public double doublevar; public char charvar: public boolean boolvar; public ClassA varA; public DataTypes() { a2 you can add data2 is true ise"): ers for questior public DataTypes(int newIntVar, double newDoublevar, char newcharvar, boolean newboolvar, ClassA newarA) { intvar = newIntVar; doublevar = newDoublevar; charvar = newCharvar: I boo War = newBoolVar; varA = newarA; Hlass classA tha alue in variable values and int TestEquals.java public class TestEquals { public static void main(String[] args) { int[] arr1 = { 1, 2, 3 }; int[] arr2 = { 3, 2, 1 }; intl arr3 = { 1, 2, 3 }; DataTypes datal = new DataTypes (2, 2.3, 'a', false, new Class(); DataTypes data2 = new DataTypes (2, 2.3, 'a', false, new Class); DataTypes data3 = new DataTypes (2, 2.3, 'a', false, new ClassA(3, arri)); DataTypes data4 = new DataTypes (2, 2.3, 'a', false, new ClassA(3, arr2)); DataTypes data5 = new DataTypes (2, 2.3, 'f', true, new ClassA(3, arr2)); DataTypes data6 = new DataTypes (2, 2.3, 'a', false, new ClassA(3, arr3)); if (data1.equals(data)) System.out.println("Test 1 passed"); else System.out.println("Test 1 failed"); if (data3.equals(data)) System.out.println("Test 2 passed"); else System.out.println("Test 2 failed"); if (!data3.equals(data)) System.out.println("Test 3 passed"); else System.out.println("Test 3 failed"); if (!data4.equals(data5)) System.out.println("Test 4 passed"); else System.out.println("Test 4 failed"); } I } 1. Open and study ClassBjava. This class has 3 compi the little red circles with a white cross in them to the le - * GX US * Test 1 failed Test 2 failed Test passed Test 4 used Clasa DataTypes 1 public class TestDataTypes TutDataType X TX 2 35 public static void main(String args) { Lab/e/ou 4 int il testArray = { 2, 4, 6); // This statement creates an array of size ) storing values, and 5 DataTypes datai - new DataTypes(2, 3.3, 's', true, new ClassA!), testarray}); DataTypes data = new DataTypes 2, 3.3. 'st, true, new Class 3, testarra) 1/ Test datal data 9 if(datal data) 12 System.out.println("datat data is true); 11 else System.out.println("datal data is talse"); 12 1/ Test datal. Intrar data2.intvar if(datal.intvar - data2.intvar) System.out.println("datal. Intvar - data2.intvar is true"); 17 else 18 System.out.println("datal.intvar - data intVar is false"); 19 20 21 if(data.charvar - data2.charvar) Test dtal.charvar o data2.charvar System.out.println("datal.charVardata2.charVar is true); 23 else System.out.println("datal.charvar data2.charvar is false"); // Test datal.boolvar data2.boolvar 27 if(datal.boolvar w data2.boolvar) System.out.println("datan.boolvar - data2.boolvar is true"); 29 else System.out.println("data1.boolvar - data.boolvar is false"); 031 Object dta: 32 // Test datal.doublevar=dta2.doublevar 33 if(datal.doublevar data2.doublevar) System.out.println("datai.doublevardta2.doublevar is true"); 35 else System.out.println("datal.doublevardta2.doublevar is false"); 77 Test datal. var data2.vara 38 if(datal.var - data2.vara) system.out.println("datal. var data2.varA is true); 48 else System.out.println("datal.vara data2.vara is false" 1/ Test datal.vara.notensdata2.vara.nutes if(datai.vara.nuItens data2.var.unitens) System.out.println("datal.vara.nuItens - data2.vara.nuriteas is true); else System.out.println("datal.varA.nunitens data.vara.nutes is false"); // Test datal varA arritem data2.varA ar Items 18 if(datal.vara.arritens data2.var. arritens) System.out.println("data1.vara. arrites data2.varkarritens is true); se else System.out.println("datai.var. arriten we data.vara.arritess is false")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Working with other project stakeholders for support.

Answered: 1 week ago

Question

Knowledge of project management (PMI) teachings

Answered: 1 week ago