Question: (a) Write a Java program that consists of a definition for a public class called Token. There should be two instance variables. One should be

(a) Write a Java program that consists of a definition for a public class called Token. There should be two instance variables. One should be of type char, and be named root. The other should be a one-dimensional array of Token, and be named multi. The class should have one constructor with a formal parameter of type char named root. This constructor should use the this keyword to assign its actual parameter to the instance variable of the same name. The other constructor should take a String. This should create and assign to multi an array of Token, each element of which is constructed on a distinct char from the specified String. (Remember that s.charAt(i) will return the ith char from String s.) [20 marks]

(b) Add a method to the class named getRandom(), which returns a Token. This should behave as follows. If the object on which the method is called has a null value for its multi variable, it should return a Token constructed on the char z. If the multi variable is not null, the method should return one of the Token objects from the multi array selected at random. (Remember that Math.random() produces a random double value in the range 0.0..1.0.) [15 marks]

(c) Write an additional method for the class defined above, named nMultis. This should take a char and return an int. The behavior of the method should be as follows. If the objects multi array is null, the method should return 0. Otherwise, it should go through the array to discover how many of the Token objects have a value of the root variable identical to the methods argument. This int value should then be returned. [15 marks]

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!