Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(IN JAVA) Use Doubly Li nked List to implement the following program: The progra

ID: 3831218 • Letter: #

Question

(IN JAVA)

Use Doubly Linked List to implement the following program: The program already uses Linked List and sorts by artistID, then artID

I need to just implement Doubly Linked list to do the same thing.

Modify the main class attached below "p7DoublyLinkedList" to sort, but using [Doubly Linked List] instead of Linked List.

Main class below uses Linked List Approach, but we are required to use Doubly Linked List, so implement Doubly Linked List to achieve the same output after sorting.

Sample Output:

Artist ID Artist Name Art ID Art Title Appraised Value

1 Acconci   
1038 Spring Flowers 800

1050 Cattle Ranch 10000

1103 Trail End 8000


2 Budd
1042 Coffee on the Trail 7544

1086 Untitled (desert landscape) 18000

1101 The Red Door 10000


3 Carpenter   
1010 Untitled (land with adobe) 800

1013 Superstitions 78000

1019 The White Heart 9300

1021 Bead Wall 14000

1022 The Cowboy 4200

1025 Profile of a Woman 625

1034 Beaver Pole Jumble 28000

1058 The Gathering 250

1063 Asleep in the Garden 110000

1080 The Dust Behind 18000

1095 The Spirit 20000

1104 Untitled 1800


4 Dill
1070 Beginnings 27500

1079 Carrying the Mail 8000

1111 Untitled (man and crucifix) 3200


5 Edwards   
1020 Untitled (Man holding coat) 3000

1036 Blackhawk 25500

1093 Antelopes 12500


6 Fleming   
1004 Seeking Shelter 52000

1012 Man on Horseback 8000

1016 Untitled 6000

1017 Brittlecone 1300

1033 Untitled (Woman abstract) 2500

1053 Blue Eyed Indian 40000

1056 Cavalry Is Coming 1900

1066 Untitled (still life) 19500

1075 Western Boots and Spurs 6000

1077 Bull Riding 5200

1083 Untitled 2500

1085 Traces 20000

1097 Untitled (Sea) 2800

1108 Untitled Mural 400

1110 Three Sisters 6500


7 Garber
1024 Spirit and Nature 592

1026 Untitled (couple) 4000

1032 Rising Sun 2000

1037 Floating World 2350

1049 Buttercup with Red Lip 400

1054 Snake Charmer 4500

1061 Untitled Mural 3520

1091 Stone Palette 11500

1114 Storytelling at the Campfire 18000


8 Higgins   
1018 Mountain Scene 2500

1057 Untitled 4500

1060 Story Sticks 650

1064 Spirit Columns 7000

1067 Owl in Flight 7000

1068 Moonlight 9750

1071 Ride the Rapids 300

1074 Storm on the Rise 8000

1109 Friends 16000


9 Ibe   
1001 Red Rock Mountain 18000

1002 Offerings 10000

1044 Mexican Fiesta 14000

1055 Starlit Evening 9500

1069 Renaissance 5500

1084 Crossing the Platt River 2200

1098 Sweet Project 592


10 Kollasch
1028 Tired Cowboy 4700

1047 Medicine Man 2500

1048 Comfy Chair 800

1072 Funnel 4500

1089 Life Lessons 4125

1096 Ceremonial Sticks 15000

1113 Shadow House 5500


11 Lerman
1090 Off the Grid 8000

1115 Starry Night 8500


12 Metz
1003 Spring Flowers 2400

1008 End of the Path 1900


13 Novarre   
1081 Coming Under Fire 650

1112 Dark Canyon 8000


14 Ortega
1006 House Remembered 700

1009 Amen 3000

1014 Plenty 500

1015 Punch 10000

1030 Ash Bench 13000

1039 Treachery 20000

1046 Immediate Gratification 1500

1076 Ride the Bronco 1500

1102 Crying Hats 10000


15 Parker
1023 Shooting the Rapids 1300

1031 Inside/Out 3500

1043 Creosote Bushes 18000

1073 Dancing in the Light 4000

1105 Meteor Show 10000


16 Penn
1027 Mountain Climber 4700

1052 American Rodeo 3500

1078 Chuckwagon 32000

1087 Three Woman 20000

1107 Striking It Rich 1750


17 Pierobon
1007 Homage to the Ancestors 1200

1035 Nature/Nurture 1300

1041 Night Version 3800

1051 Night Version 7000

1059 Dwelling 16000


18 Prinzen   
1005 The Hang 8000

1040 Night on the Praire 1300

1082 Spring Flowers 20000

1088 Lessons 3700

1094 Life Is Sweet 25000


19 Quiroz
1011 Eve 975

1045 Leaf Patterns 2100

1065 Moonlite 1300

1106 Horse Corral 12500

1116 Apache Warrior 23000


20 Rath
1029 Horseshoe Falls 15000

1062 Cowboy and Saddle 18000

1092 Dressing Up 1300

1099 Watch That Rattler 900

1100 Hungry Cowboys 750

Required Files:

Artist.java:

Art.java:

p7DoublyLinkedList.java:

DoublyLinkedList.java:

ArtistArtComparator.java:

ArraySOrter.java:

Explanation / Answer

Answer: See the updated class below:

1. p7DoublyLinkedList class:

------------------------------------------------------------

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class p7DoublyLinkedList {

   private static final String ARTIST_FILE = "C:/Users/patel/Desktop/JavaFiles/p7artists.txt";
   private static final String ART_FILE = "C:/Users/patel/Desktop/JavaFiles/p7arts.txt";
   private static final String ARTIST_ART_FILE = "C:/Users/patel/Desktop/JavaFiles/p7out(Aashiv).txt";

   /**
   * Reads from the artist file and creates an array of artist and returns the
   * array
   */
   public static DoublyLinkedList<Artist>[] getArtist() throws FileNotFoundException, NumberFormatException {
       try {
           // Scanner to read from the file
           Scanner in = new Scanner(new File(ARTIST_FILE));

           @SuppressWarnings("unchecked")
           DoublyLinkedList<Artist>[] artistArtArray = new DoublyLinkedList[0];
           // LinkedList<Artist>[] artistArtArray = (LinkedList<Artist>[])new
           // Object[0];

           // Read file
           while (in.hasNextLine()) {
               String data[] = in.nextLine().replaceAll("\s+", " ").split(" ");

               // Increment array size to add artist
               int len = artistArtArray.length;
               if (data.length > 1) {
                   artistArtArray = Arrays.copyOf(artistArtArray, len + 1);
               }

               // Add artist to array
               try {
                   artistArtArray[len] = new DoublyLinkedList<Artist>();
                   artistArtArray[len].addFirst(new Artist(data[1], Integer.parseInt(data[0])));
               } catch (NumberFormatException nfe) {
                   throw new NumberFormatException("Error: Invalid input string: " + data[0]);
               }
               // Close scanner
               in.close();
           }          
           return artistArtArray;
       } catch (FileNotFoundException fnfe) {
           throw new FileNotFoundException("Error: File not found " + ARTIST_FILE);
       }
   }

   /**
   * Finds the artist with id and returns the array index
   */
   public static int getArtist(DoublyLinkedList<Artist>[] artistArtArray, int artistId) {
       for (int i = 0; i < artistArtArray.length; i++) {
           if (((Artist)artistArtArray[i].getHead().getElement()).getArtistID() == artistId)
               return i;
       }

       return -1;
   }

   /**
   * Reads from the art file and updates the array containing artist
   */
   public static DoublyLinkedList<Artist>[] getArt(DoublyLinkedList<Artist>[] artistArtArray)
           throws FileNotFoundException, NumberFormatException {
       try {
           // Scanner to read from the file
           Scanner in = new Scanner(new File(ART_FILE));

           // Read file
           while (in.hasNextLine()) {

               // Add artist to array
               try {
                   // Get art id
                   int artID = in.nextInt();

                   // Get Art title
                   StringBuffer artTitle = new StringBuffer();
                   while (in.hasNext("[^0-9]+")) {
                       artTitle.append(in.next("[^0-9]+") + " ");
                   }

                   // Get artist id
                   int artistId = in.nextInt();

                   // Get appraised value
                   int appraisedValue = in.nextInt();

                   // Clear next line character
                   if (in.hasNextLine())
                       in.nextLine();

                   // Get the artist index in the array
                   int index = getArtist(artistArtArray, artistId);
                   Artist artist = artistArtArray[index].getHead().getElement();

                   // Add the art read from the file to the array
                   artistArtArray[index].addFirst(new Artist(artist.getArtistID(), artist.getArtistName(),
                           new Art(artID, artTitle.toString(), appraisedValue)));
               } catch (NumberFormatException nfe) {
                   throw new NumberFormatException("Error: Invalid input string: " + nfe.getMessage());
               }
               // Close scanner
               in.close();
           }
           return artistArtArray;
       } catch (FileNotFoundException fnfe) {
           throw new FileNotFoundException("Error: File not found " + ARTIST_FILE);
       }
   }

   /**
   * Writes the artist-wise art to the file
   *
   * @param artistArtArray
   * @throws IOException
   */
   public static void writeToFile(DoublyLinkedList<Artist>[] artistArtArray) throws IOException {
       // Writer to write to the file
       PrintWriter pw = new PrintWriter(new FileWriter(ARTIST_ART_FILE));

       // Write header to the file
       pw.println(String.format("%-9s %-20s %-6s %-25s %s", "Artist ID", "Artist Name", "Art ID", "Art Title",
               "Appraised Value"));

       // Write artist
       for (DoublyLinkedList<Artist> list : artistArtArray) {
           // Sort the list
           //Collections.sort(list.subList(1, list.size()));
           ArraySorter.selectionSort(list.toArray(), list.getSize());

           if (list.size() > 1) {
               // Print artist id and name
               Artist artist = list.getHead().getElement();
               pw.println();
               pw.print(artist + " " + artist.getArt() + " ");
               pw.println();

               // Print all arts
               for (int i = 2; i < list.size(); i++) {
                   pw.print(String.format("%30s %s", " ", list.getHead().getElement().getArt()) + " ");
                   pw.println();
               }
           }
           pw.flush();
       }

       // Close writer
       pw.close();
   }

   public static void main(String[] args) {

       try {
           // Create an array of linked list
           DoublyLinkedList<Artist>[] artistArtArray;

           // Fill array with artists
           artistArtArray = getArtist();

           // Update array with art
           artistArtArray = getArt(artistArtArray);

           // Sort array
           // Arrays.sort(artistArtArray, new ArtistArtComparator());

           // Write artist art detail to file
           writeToFile(artistArtArray);

           System.out.println();
       } catch (NumberFormatException nfe) {
           System.out.println(nfe.getMessage());
       } catch (FileNotFoundException fnfe) {
           System.out.println(fnfe.getMessage());
       } catch (IOException e) {
           System.out.println(e.getMessage());
       }
   }  
}

-------------------------------------------

2. DoublyLinkedlist class:

-----------------------------------------------

import java.util.NoSuchElementException;

/*
* Java Program to Implement Doubly Linked List
*/

public class DoublyLinkedList<E> {

   private Node head;
   /**
   * @return the head
   */
   public Node getHead() {
       return head;
   }

   /**
   * @return the tail
   */
   public Node getTail() {
       return tail;
   }

   /**
   * @return the size
   */
   public int getSize() {
       return size;
   }

   private Node tail;
   private int size;

   public DoublyLinkedList() {
       size = 0;
   }

   /**
   * this class keeps track of each element information
   *
   */

   class Node {
       E element;
       Node next;
       Node prev;

       public Node(E element, Node next, Node prev) {
           this.element = element;
           this.next = next;
           this.prev = prev;
       }

       /**
       * @return the element
       */
       public E getElement() {
           return element;
       }

       /**
       * @param element the element to set
       */
       public void setElement(E element) {
           this.element = element;
       }

       /**
       * @return the next
       */
       public Node getNext() {
           return next;
       }

       /**
       * @param next the next to set
       */
       public void setNext(Node next) {
           this.next = next;
       }

       /**
       * @return the prev
       */
       public Node getPrev() {
           return prev;
       }

       /**
       * @param prev the prev to set
       */
       public void setPrev(Node prev) {
           this.prev = prev;
       }
      
      
   }

   /**
   * returns the size of the linked list
   *
   * @return
   */

   public int size() {
       return size;
   }

   /**
   * return whether the list is empty or not
   *
   * @return
   */

   public boolean isEmpty() {
       return size == 0;
   }

   /**
   * adds element at the starting of the linked list
   *
   * @param element
   */

   public void addFirst(E element) {

       Node tmp = new Node(element, head, null);

       if (head != null) {
           head.prev = tmp;
       }
       head = tmp;

       if (tail == null) {
           tail = tmp;
       }
       size++;

       System.out.println(element);
   }

   /**
   * adds element at the end of the linked list
   *
   * @param element
   */

   public void addLast(E element) {

       Node tmp = new Node(element, null, tail);

       if (tail != null) {
           tail.next = tmp;
       }
       tail = tmp;

       if (head == null) {
           head = tmp;
       }
       size++;

       System.out.println(element);
   }

   /**
   * this method walks forward through the linked list
   */

   public void iterateForward() {

       System.out.println("iterating forward..");

       Node tmp = head;

       while (tmp != null) {
           System.out.println(tmp.element);
           tmp = tmp.next;
       }
   }
  
   /**
   * this method converts list to array
   */

   public Artist[] toArray() {

       System.out.println("Converting to array..");
      
       Artist[] artists=new Artist[size];
       int i=0;
       Node tmp = head;

       while (tmp != null) {
           artists[i]=(Artist)tmp.element;          
           tmp = tmp.next;
           i++;
       }
       return artists;
   }

   /**
   * this method walks backward through the linked list
   */

   public void iterateBackward() {

       System.out.println("iterating backward..");

       Node tmp = tail;

       while (tmp != null) {
           System.out.println(tmp.element);
           tmp = tmp.prev;
       }
   }

   /**
   * this method removes element from the start of the linked list
   *
   * @return
   */

   public E removeFirst() {

       if (size == 0)
           throw new NoSuchElementException();

       Node tmp = head;
       head = head.next;
       head.prev = null;
       size--;

       System.out.println("deleted: " + tmp.element);
       return tmp.element;
   }

   /**
   * this method removes element from the end of the linked list
   *
   * @return
   */

   public E removeLast() {

       if (size == 0)
           throw new NoSuchElementException();

       Node tmp = tail;
       tail = tail.prev;
       tail.next = null;
       size--;

       System.out.println("deleted: " + tmp.element);
       return tmp.element;
   }

   public static void main(String a[]) {

       DoublyLinkedList<Integer> dll = new DoublyLinkedList<Integer>();

       // Sample code
       dll.addFirst(10);
       dll.addFirst(100);
       dll.addFirst(20);
       dll.addFirst(200);
       dll.addFirst(34);
       dll.addLast(56);
       dll.addLast(364);
       dll.addLast(36);
       dll.addLast(34);
       dll.addLast(25);
       dll.iterateForward();
       dll.removeFirst();
       dll.removeFirst();
       dll.removeFirst();
       dll.removeLast();
       dll.removeLast();
       dll.removeLast();
       dll.iterateBackward();
   }
}

---------------------------------------------------------------

Note: Input data is not provided. For actual output, it is required.