Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

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

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_2

Step: 3

blur-text-image_3

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

How To Build A Million Dollar Database

Authors: Michelle Bergquist

1st Edition

0615246842, 978-0615246840

More Books

Students also viewed these Databases questions