Question: JAVA: Two integers, babies 1 and babies 2 , are read from input as the number of babies for two rabbits. headObj has the default

JAVA: Two integers, babies1 and babies2, are read from input as the number of babies for two rabbits. headObj has the default value of -1. Create a new node firstRabbit with integer babies1, and insert firstRabbit after headObj. Then, create a second node secondRabbit with integer babies2, and insert secondRabbit after firstRabbit.
Ex: If the input is 1522, then the output is:
-1
15
22
You will need to create two source files:
import java.util.Scanner;
public class RabbitLinkedList {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
RabbitNode headObj;
RabbitNode firstRabbit;
RabbitNode secondRabbit;
RabbitNode currRabbit;
int babies1;
int babies2;
babies1= scnr.nextInt();
babies2= scnr.nextInt();
headObj = new RabbitNode(-1);
/* Your code goes here */
currRabbit = headObj;
while (currRabbit != null){
currRabbit.printNodeData();
currRabbit = currRabbit.getNext();
}
}
}
public class RabbitNode {
private int babiesVal;
private RabbitNode nextNodeRef;
public RabbitNode(int babiesInit){
this.babiesVal = babiesInit;
this.nextNodeRef = null;
}
public void insertAfter(RabbitNode nodeLoc){
RabbitNode tmpNext;
tmpNext = this.nextNodeRef;
this.nextNodeRef = nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}
public RabbitNode getNext(){
return this.nextNodeRef;
}
public void printNodeData(){
System.out.println(this.babiesVal);
}
}
Given e code segments above, fill in the missing lines of code for the RabbitLinkedList file.

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!