Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise 1 - Comparing Primitive and Non-Primitive Data Types 1. Create a new Project (select File > New > Java Project) and name it: Lab2
Exercise 1 - Comparing Primitive and Non-Primitive Data Types 1. Create a new Project (select File > New > Java Project) and name it: Lab2 2. Add to this project all provided classes. 3. Note. Eclipse might indicate that there are compilation errors in some of these classes. We will deal with these errors later. For the time being ignore any warnings that Eclipse gives about compilation errors (if any popup windows appear when you try to compile and run the code, just close them and continue with the exercises). 4. Open and read the provided java classes DataTypes.java, ClassA.java, and TestDataTypes.java. Do not modify TestDataTypes.java yet! 5. Answer question 1 in AnswersLab2.txt 6. Add code to TestDataTypes.java to perform all comparisons in question 1 of Answers Lab2.txt. If a comparison is true the code must print "true", otherwise it must print "false". For example, to compare data 1 and data2 you can add this code if (data1 == data2) System.out.println("data1 == data2 is true"); else System.out.println("data1 == data2 is false); 7. Indicate in AnswersLab2.txt how many of your answers for question 1 were incorrect. Also, answer question 3. 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 numitems 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 arritems if arritems is null. 2. Add a public method equals(DataTypes other Type) to class DataTypes that returns the value true if (i) all primitive instance variables in this object and otherType 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. Exercise 3 - Understanding Static and non-Static Objects 1. Open and study ClassB.java. This class has 3 compilation errors. Hover the mouse over the little red circles with a white cross in them to the left of the incorrect statements to read the compilation error. 2. Fix the compilation error without making counter or incCounter static. At the end the code must print the values of i and counter. Run the code to make sure it works. 'gssejo jo pylqo ue ajeejo Jojo XII of nok ssnjuoo s ! }sej og snj o ; 8 es ay woje wayi UJjoj jouueo no es aje jejunooou! pueU!S JUIH Exercise 4 - Using Static Objects 1. Open and study class CountObjects.java. The main method in this class creates a random number of objects of the class ClassC. However, variable numObjects has value -1, which you must change so that when this program is executed it prints the number of Class C objects created. 2. You can make changes to ClassC.java and to CountObjects.java. However, the only Exercise 4 Using Static Objects 1. Open and study class CountObjects.java. The main method in this class creates a random number of objects of the class ClassC. However, variable numObjects has value -1, which you must change so that when this program is executed it prints the number of Class C objects created. 2. You can make changes to ClassC.java and to CountObjects.java. However, the only changes that you can make to ClasC.java are to the declaration of counter and changing the statement in the constructor of ClassC. Do not add more code to the constructor of ClassC. The only change that you can make in class CountObjects is the statement in line 9: numObjects = -1; you can assign to numObjects the number of objects that have been created. 3. Run your code and make sure that the value that it prints is the number of objects created and not -1 (the number of these objects should be 16. Note that you cannot simply write numObjects = 16 in class CountObjects). a qepen jeyi jo kdoo juejay!p e jou pue ajqejen jejunoo awes ayi seseojou! papedo si jeyi parqo Lan eyi jnsu nok op MOH (colonjsuoo ay up juwejeis ayi abueyo no pinoys moy 'os) painbx s olonsuo ay jeyi awit Lueno aqewen jeyi jo nj i seouj p fqo Jo qunu 4unoo o Ossel u unoo jq sn "jqoid znq nt jun s ou on Fuj Add Configuration... DataTypes.java RandomNumber.java TestDataTypes.java Te A.java ClassB.java ClassC.java CountObjects.java public class ClassA { private final int SIZE_ARRAY = 5; public int numItems; public int[] arrItems; public ClassA(int n, int[] arr) { numItems = n; arrItems = arr; } public ClassA() { numItems = 0; arrItems = new int[SIZE_ARRAY]; } } Add Configuration... A.java ClassB.java ClassC.java CountObjects.java DataTypes.java RandomNumber.java TestDataTypes.java Te: public class Class { private int counter = 0; public Class() { counter = 1; } public void incCounter() { ++counter; } public int getCounter() { return counter; } public static void main(String[] args) { int i; i = counter; incCounter(); System.out.println("i = " + i + " counter = " + counter); } Add Configuration... A.java ClassB.java ClassC.java CountObjects.java DataTypes.java RandomNumber.java TestDataTypes.java Te: public class Class { private int counter = 0; public Class() { counter = 1; } public void incCounter() { ++counter; } public int getCounter() { return counter; } } Add Configuration... A.java ClassB.java ClassC.java CountObjects.java DataTypes.java RandomNumber.java TestDataTypes.java Te: public class CountObjects { public static void main(String[] args) { int numObjects; ClassCC = null; for (int i = 0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started