Question: Java Exercise Exercise 4 - Using Static Objects 1. Open and study class CountObjects.java. The main method in this class creates a random number of

Java Exercise

Java Exercise Exercise 4 - Using Static Objects 1. Open and study

class CountObjects.java. The main method in this class creates a random numberof objects of the class ClassC. However, variable numObjects has value -1,which you must change so that when this program is executed itprints the number of Class C objects created. 2. You can makechanges to ClassC.java and to CountObjects.java. However, the only changes that youcan make to ClasC.java are to the declaration of counter and changingthe statement in the constructor of ClassC. Do not add more code

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). 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!