In: Computer Science
The application has class Magazine which describes one Magazine object. Class LLMagazineRec desribes linked list whose nodes have data of Magazine type and includes recursive method createArrayListRec which you have to implement. Class Driver has main method that creates myList as linked lists of magazines. It should invoke recursive method from class LLMagazineRec. WRITTEN IN JAVA
1.)code for magazine class:
// Class Magazine describes magazine object that has title and
number of pages
public class Magazine
{
private int pages; //number of pages
private String name; // magazine name or title
public Magazine(int p, String n)
{
pages = p;
name = n;
}
public int getPages()
{
return pages;
}
public String getName()
{
return name;
}
public String toString()
{
return name + "\t" + pages;
}
}
2.) code for llmagazierec class:
import java.util.*;
/* Class LLMagazineRec defines linked list of magazine objects. It
includes
* recursive methods to print all magazines, to calculate sum pages
in all
* magazines, and to find longest magazine in linked list.
*/
public class LLMagazineRec
{
private Node list;
public LLMagazineRec()
{
list = null;
}
public Node getList()
{
return list;
}
public void addRear(Magazine mag)
{
Node node = new Node(mag);
if (list == null)
list = node;
else
{
Node curr = list;
while (curr.next!= null)
curr= curr.next;
curr.next = node;
}
}
// Returns ArrayList<Magazine> storing all Magazine objects
from the linked
// list. Method accepts reference to the beginning of the linked
list. It
// must be RECURSIVE, and should work for EMPTY and for NON-EMPTY
list.
public ArrayList<Magazine> createArrayListRec(Node
first)
{
//INSERT CODE HERE
}
private class Node
{
public Magazine data;
public Node next;
public Node(Magazine mag)
{
data = mag;
next = null;
}
public String toString()
{
return data.toString();
}
}
}//End LLMagazineRec
3.) code for Driver class:
/**
* Class Driver tests methods from LLMagazineRec class.
*/
public class Driver
{
public static void main(String[] args)
{
System.out.println("Creating linked list myList");
LLMagazineRec myList = new LLMagazineRec();
myList.addRear(new Magazine(35, "Golf Digest"));
myList.addRear(new Magazine(49, "Sports Illustrated"));
myList.addRear(new Magazine(101, "Time"));
myList.addRear(new Magazine(130, "Vogue"));
System.out.println("\nPrinting resulting ArrayList");
//INSERT CODE TO INVOKE THE METHOD createArrayListRec
//AND PRINT ITS RESULT BY USING FOR EACH LOOP
}
}
//create new project in netbeans or eclipse
//create class Magazine.java
public class Magazine {
private int pages; //number of pages
private String name; // magazine name or title
public Magazine(int p, String n) {
pages = p;
name = n;
}
public int getPages() {
return pages;
}
public String getName() {
return name;
}
public String toString() {
return name + "\t" + pages;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
//create class LLMagazineRec.java
import java.util.*;
/* Class LLMagazineRec defines linked list of magazine objects.
It includes
* recursive methods to print all magazines, to calculate sum pages
in all
* magazines, and to find longest magazine in linked list.
*/
public class LLMagazineRec {
private ArrayList<Magazine>final_result=new
ArrayList<Magazine>();
private Node list;
public LLMagazineRec() {
list = null;
}
public Node getList() {
return list;
}
public void addRear(Magazine mag) {
Node node = new Node(mag);
if (list == null) {
list = node;
} else {
Node curr = list;
while (curr.next != null) {
curr = curr.next;
}
curr.next = node;
}
}
// Returns ArrayList<Magazine> storing all Magazine
objects from the linked
// list. Method accepts reference to the beginning of the linked
list. It
// must be RECURSIVE, and should work for EMPTY and for NON-EMPTY
list.
public ArrayList<Magazine> createArrayListRec(Node first)
{
//INSERT CODE HERE
if (first != null) {
Magazine magazine = first.data;
final_result.add(magazine);//add magazine data to arraylist
first = first.next;
createArrayListRec(first);//this requirement , It must be
RECURSIVE. hence call method recursively
}
return final_result;
}
private class Node {
public Magazine data;
public Node next;
public Node(Magazine mag) {
data = mag;
next = null;
}
public String toString() {
return data.toString();
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------
//create Driver.java and run this class
import java.util.ArrayList;
/**
* Class Driver tests methods from LLMagazineRec class.
*/
public class Driver {
public static void main(String[] args) {
System.out.println("Creating linked list myList");
LLMagazineRec myList = new LLMagazineRec();
myList.addRear(new Magazine(35, "Golf Digest"));
myList.addRear(new Magazine(49, "Sports Illustrated"));
myList.addRear(new Magazine(101, "Time"));
myList.addRear(new Magazine(130, "Vogue"));
System.out.println("\nPrinting resulting ArrayList");
//INSERT CODE TO INVOKE THE METHOD createArrayListRec
ArrayList<Magazine> list =
myList.createArrayListRec(myList.getList());
//AND PRINT ITS RESULT BY USING FOR EACH LOOP
for (int i = 0; i < list.size(); i++) {
Magazine magazine = list.get(i);
System.out.println(magazine.toString());
}
}
}
_______________________________________OUTPUT__________________________________________