In: Computer Science
Use this code to answer the following questions:
Object obj;
String str;
Song sng = new Song("Rockstar", 5);
obj = sng; // #7
sng = str; // #8
str = (String) obj; // #9
sng = obj; // #10
1a) Write the start of the class declaration for a node in a linked list (give the name of the class and the instance variables). The name of the node should be SpecialNode. The data in each SpecialNode will include both a String and a Song object.
b) Using the SpecialNode class you created in question #11, write a constructor forSpecialNode that has three parameters and initializes all the SpecialNode instance variables.
c) Write a line of code to instantiate a SpecialNode object to initialize the SpecialNodeinstance variables. Use the constructor you wrote in the previous question.
Song rockstar = new Song("Rockstar", 5);
SpecialNode sn = ___________________________________ ;
1a) Write the start of the class declaration for a node in a linked list (give the name of the class and the instance variables). The name of the node should be SpecialNode. The data in each SpecialNode will include both a String and a Song object.
Ans:
Class SpecialNode {
String str;
Object obj;
obj sng;
}
b) Using the SpecialNode class you created in question #11, write a constructor forSpecialNode that has three parameters and initializes all the SpecialNode instance variables.
Ans: SpecialNode(string,object,song) {
str = string;
obj = object;
sng = song;
}
c) Write a line of code to instantiate a SpecialNode object to initialize the SpecialNodeinstance variables. Use the constructor you wrote in the previous question.
Song rockstar = new Song("Rockstar", 5);
SpecialNode sn = new SpecialNode("Rockstar",6,rockstar);
//Here, we took 3 parameters, so Rockstar is a string, 6 is an object, and rockstar is the Song which is already defined in the question (Song rockstar = new Song("Rockstar", 5))
Note: If you have any related doubts, queries, feel free to ask by commenting down below.
And if my answer suffice your requirements, then kindly upvote.
Happy Learning