Menu

Write Java Program Consists Definition Public Class Called Token Two Instance Variables On Q43816472

(a) Write a Java program that consists of a definition for apublic class called Token. There should be two instance variables.One should be of type char, and be named root. The other should bea one-dimensional array of Token, and be named multi. The classshould have one constructor with a formal parameter of type charnamed root. This constructor should use the “this” keyword toassign its actual parameter to the instance variable of the samename. The other constructor should take a String. This shouldcreate and assign to multi an array of Token, each element of whichis constructed on a distinct char from the specified String.(Remember that s.charAt(i) will return the i’th char from Strings.) [20 marks]

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

(c) Write an additional method for the class defined above,named nMultis. This should take a char and return an int. Thebehavior of the method should be as follows. If the object’s multiarray is null, the method should return 0. Otherwise, it should gothrough the array to discover how many of the Token objects have avalue of the root variable identical to the method’s argument. Thisint value should then be returned. [15 marks]

Expert Answer


Answer to (a) Write a Java program that consists of a definition for a public class called Token. There should be two instance var…

OR