Question: JAVA: This question deals with the object constructor. You are given the following class Rock that has two constructors (overloaded). They both update the class

JAVA:

This question deals with the object constructor. You are given the following class Rock that has two constructors (overloaded). They both update the class variable (count) whenever an object is created.

class Rock {

static int count=0;

Rock() { // This is the constructor

System.out.print("Rock ");

count++;

}

Rock(int i) {

System.out.print("Rock " + i + " ");

this();

count++;

}

}

write a client program to display the following; rnum is between 0 to 10. rnum = 3 if it (random number) is <=3. You are only allowed to use the constructors (in the Rock Class) to display the Rock objects created.

For example:

rnum = 7

Rock Rock0 Rock Rock1 Rock Rock2 Rock Rock3 Rock Rock4 Rock Rock5 Rock Rock6

number of Rock objects created = 14

rnum = 1

Rock Rock0 Rock Rock1 Rock Rock2

number of Rock objects created = 6

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!