Question

In: Computer Science

The application has class Magazine which describes one Magazine object. Class LLMagazineRec desribes linked list whose...

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   
}   
}

Solutions

Expert Solution

//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__________________________________________


Related Solutions

Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Create the logic for an application that instantiates an object of the ATM class. Prompt the...
Create the logic for an application that instantiates an object of the ATM class. Prompt the user for the ATM object data to display the user’s bank balance PROGRAMMING LOGIC AND DESIGN QUESTION JAVA LANGUAGE
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
A Bookstore Application C++ Design the class Book. Each object of the class Book can hold...
A Bookstore Application C++ Design the class Book. Each object of the class Book can hold the following information about a book: title, authors, publisher, ISBN Include the member functions to perform the various operations on the objects of Book. For example, the typical operations that can be performed on the title are to show the title, set the title. Add similar operations for the publisher, ISBN , and authors. Add the appropriate constructors and a destructor (if one is...
(a) Write a stack class that is based on a linked list. It can be just...
(a) Write a stack class that is based on a linked list. It can be just pop(), push(), and anything you need for those methods or testing. (b) Write a queue class that is based on a linked list. As above, it can be just enqueue() and dequeue(), as well as anything you need for those methods or testing. (c) Write some test cases, trying to include edge cases. Why did you choose those tests? Did you get the results...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
Find the last node of a linked list of n elements whose index is a multiple...
Find the last node of a linked list of n elements whose index is a multiple of k (counted from 0). For example, if the list is 12 → 75 → 37 → 99 → 12 → 38 → 99 → 60 ↓ and k = 4, then you should return the second 12 (with index 4). Your algorithm should take O(n) time and use O(1) extra space. Implement the following method in LinkedList.java. public T lastK(int k) LinkedList.java. public...
3. Find the last node of a linked list of n elements whose index is a...
3. Find the last node of a linked list of n elements whose index is a multiple of k (counted from 0). For example, if the list is 12 → 75 → 37 → 99 → 12 → 38 → 99 → 60 ↓ and k = 4, then it should return the second 12 (with index 4). The algorithm should take O(n) time and use O(1) extra space. Implement the following method in LinkedList.java. public T lastK(int k)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT