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

Already have most of the code after these modifications below. But it is giving

ID: 3828906 • Letter: A

Question

Already have most of the code after these modifications below. But it is giving me an error saying:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
   The constructor Art(int, String, int) is undefined
   The method getArtID() is undefined for the type ArtNode
   The method getArtTitle() is undefined for the type ArtNode
   The method getAppraisedValue() is undefined for the type ArtNode

   Exception in thread "main" java.lang.NoSuchMethodError:

at ArtNode.<init>(ArtNode.java:21)
   at MyArtWorks.<init>(MyArtWorks.java:40)
   at Exam4Step3.<init>(Exam4Step3.java:15)
   at Exam4Step3.main(Exam4Step3.java:32)

III. Step 3: In this step, Using Doubly Linked List you are going to sort the Artworks for each artist so that his artworks will be
on ascending order on artID.

Project Specifications:

A. What to Do:
1. Modify your “ArtistNode.java” as following:
   i. Construct “add” method so that the new instance of ArtNode can be added in ascending order on artID.
   Hints: You will need to consider 4 different cases for a new Node that can be
       1. the only node of the list
       2. The last(but not the only) node of the list
       3. The first(but not the only) node of the list
       4. In the middle of the list

2. Construct MyArtWorks.java:

   - Contains:
       1. "artists"
       - An instance of MyArtistNodes that reads input file into the list.

       2. Constructor:
       - Accepts the input file Name so that the entire entries from the file will be constructed.

       3. print() method:
       - To write it to the output file. (This method will be called later in Exam4Step3.java to write to the file)

3. Add code to Modify “MyArtworks.java” with the following modifications:
   1. Pass "exam4Artists.txt" when making an instance of MyArtists (let's call it "myArtists")
   2. Whenever you read an ArtWork, you will use the map to locate the Artist to whom ArtWork should belong.

3. Write “Exam4Step3.java” to test your program:
   1. Pass "exam4Arts.txt" when making an instance of MyArtWorks.
   2. Construct a print method to write the required output to the output file "out3.txt".

Sample Output: (out3.txt)
1   Acconci       3   18800.00
1038 Spring Flowers       1    800.00
1050 Cattle Ranch           1    10000.00
1103 Trail End           1   8000.00


2   Budd       3   35544.00
1042 Coffee on the Trail       2    800.00
1086 Untitled (desert Landscape)   2    10000.00
1101 The Red Door           2   8000.00


3 Carpenter 12   284975.00
1010 Untitled (land with adobe)   3    800.00
1013 Superstitions           3    78000.00
1103 Trail End           1   8000.00

And so on... for other Artists

Required Files:

exam4Artists.txt:

exam4Arts.txt:

1038   Spring Flowers   1   800
1050   Cattle Ranch   1   10000
1103   Trail End   1   8000
1042   Coffee on the Trail   2   7544
1013   Superstitions   3   78000
1021   Bead Wall   3   14000
1034   Beaver Pole Jumble   3   28000
1063   Asleep in the Garden   3   110000
1070   Beginnings   4   27500
1036   Blackhawk   5   25500
1017   Brittlecone   6   1300
1053   Blue Eyed Indian   6   40000
1056   Cavalry Is Coming   6   1900
1075   Western Boots and Spurs   6   6000
1077   Bull Riding   6   5200
1049   Buttercup with Red Lip   7   400
1018   Mountain Scene   8   2500
1055   Starlit Evening   9   9500
1096   Ceremonial Sticks   10   15000
1037   Floating World   7   2350
1109   Friends   8   16000
1084   Crossing the Platt River   9   2200
1072   Funnel   10   4500
1032   Rising Sun   7   2000
1060   Story Sticks   8   650
1044   Mexican Fiesta   9   14000
1047   Medicine Man   10   2500
1024   Spirit and Nature   7   592
1067   Owl in Flight   8   7000
1001   Red Rock Mountain   9   18000
1028   Tired Cowboy   10   4700
1054   Snake Charmer   7   4500
1068   Moonlight   8   9750
1069   Renaissance   9   5500
1113   Shadow House   10   5500
1114   Storytelling at the Campfire   7   18000
1064   Spirit Columns   8   7000
1002   Offerings    9   10000
1089   Life Lessons   10   4125
1091   Stone Palette   7   11500
1074   Storm on the Rise   8   8000
1098   Sweet Project   9   592
1048   Comfy Chair   10   800
1101   The Red Door   2   10000
1080   The Dust Behind   3   18000
1058   The Gathering   3   250
1019   The White Heart   3   9300
1095   The Spirit   3   20000
1079   Carrying the Mail   4   8000
1093   Antelopes    5   12500
1110   Three Sisters   6   6500
1085   Traces   6   20000
1004   Seeking Shelter   6   52000
1016   Untitled   6   6000
1026   Untitled (couple)   7   4000
1057   Untitled   8   4500
1086   Untitled (desert landscape)   2   18000
1025   Profile of a Woman   3   625
1022   The Cowboy   3   4200
1104   Untitled    3   1800
1010   Untitled (land with adobe)   3   800
1111   Untitled (man and crucifix)   4   3200
1020   Untitled (Man holding coat)   5   3000
1012   Man on Horseback   6   8000
1097   Untitled (Sea)   6   2800
1066   Untitled (still life)   6   19500
1033   Untitled (Woman abstract)   6   2500
1061   Untitled Mural   7   3520
1071   Ride the Rapids   8   300

ArtistNode.java:

public class ArtistNode extends Artist {

   // Instance variables
   private ArtNode firstNode;
   private ArtNode lastNode;
   private int totalEntries;
   private double totalValue;

   /**
   * Constructor
   *
   * @param artistName
   * - artist name
   * @param artistID
   * - artist id
   */
   public ArtistNode(String artistName, int artistID) {
       super(artistName, artistID);
       this.firstNode = null;
       this.lastNode = null;
       totalEntries = 0;
       totalValue = 0;
   }

   /**
   * Adds an art node
   */
   public void add(ArtNode newArt) {
       // If this is the first node
       if (this.firstNode == null) {
           this.firstNode = newArt;
           this.lastNode = newArt;
       } else {
           // Find the appropriate place for the new art i.e. to have the art
           // nodes in ascending order
           ArtNode curr = this.firstNode;
          
           // Traverse the nodes
           while (curr != null) {
               if (newArt.compareTo(curr) < 0) {
                   if (curr.getPrevious() != null)
                       curr.getPrevious().setNext(newArt);
                      
                   // Set previous and next of new art
                   newArt.setPrevious(curr.getPrevious());
                   newArt.setNext(curr);
                  
                   // set prev of cuur
                   curr.setPrevious(newArt);
                  
                   break;
               }
              
               // Advance curr
               curr = curr.getNext();
           }
          
           // Check if newArt is the first node
           if (curr == this.firstNode)
               this.firstNode = newArt;
          
           // Check if the newArt is added
           if (curr == null) {
               this.lastNode.setNext(newArt);
               newArt.setPrevious(this.lastNode);
               this.lastNode = newArt;
           }
       }

       // Update count and value
       totalEntries += 1;
       totalValue += newArt.getAppraisedValue();
   }

   /**
   * Returns the total art work by this artist
   */
   public int getTotalEntries() {
       return totalEntries;
   }

   /**
   * Returns the total value of the art work by this artist
   */
   public double getTotalValue() {
       return totalValue;
   }

   /**
   * Displays the artist details
   */
   public String toString() {
       StringBuffer art = new StringBuffer();
       String lineSep = System.getProperty("line.separator");
      
       // Add arts
       // Traverse the nodes
       ArtNode curr = this.firstNode;
       while (curr != null) {
           art.append(lineSep);
           art.append(curr);
          
           // Advance curr
           curr = curr.getNext();
       }
      
       return String.format("%-3d %-15s %-3d %.2f", getArtistID(), getArtistName(), this.totalEntries,
               this.totalValue) + art.toString() + lineSep;
   }
}

Artist.java:

public class Artist implements java.lang.Comparable {
// Instance variables
private int artistID; // id of artist
private String artistName; // name of artist
private Art art;

/**
* Constructor
*
* @param name
* @param number
*/
Artist(String name, int number) {
this.artistID = number;
this.artistName = name;
}

/**
* Constructor
*
* @param artistID
* @param artistName
* @param art
*/
public Artist(int artistID, String artistName, Art art) {
this.artistID = artistID;
this.artistName = artistName;
this.art = art;
}

/**
* return the artistName
*
* @return
*/
public String getArtistName() {
return artistName;
}

/**
* set the artistName
*
* @param artistName
*/
public void setArtistName(String artistName) {
this.artistName = artistName;
}

/**
* return the artistId
*
* @return
*/
public int getArtistID() {
return artistID;
}

/**
* set the artistId
*
* @param artistId
*/
public void setArtistID(int artistId) {
this.artistID = artistId;
}

/**
* @return the art
*/
public Art getArt() {
return art;
}

/**
* @param art
* the art to set
*/
public void setArt(Art art) {
this.art = art;
}

public int compareTo(Artist o) {
return this.getArt().compareTo(o.getArt());
}

/**
* toString method
*/
public String toString() {
return String.format("%-9d %-20s", this.artistID, this.artistName);
}
}

Art.java:

public class Art implements java.lang.Comparable {

// Instance variables
private int artID;
private String artTitle;
private int appraisedValue;

/**
* Constructor
*
* @param artID
* - art id
* @param artTitle
* - title of the art
* @param appraisedValue
* - value
*
public Art(int artID, String artTitle, int appraisedValue) {
this.artID = artID;
this.artTitle = artTitle;
this.appraisedValue = appraisedValue;
}

/**
* @return the artID
*
public int getArtID() {
return artID;
}

/**
* @return the artTitle
*
public String getArtTitle() {
return artTitle;
}

/**
* @return the appraisedValue
*
public int getAppraisedValue() {
return appraisedValue;
}

/**
* @param artID
* the artID to set
*
public void setArtID(int artID) {
this.artID = artID;
}

/**
* @param artTitle
* the artTitle to set
*
public void setArtTitle(String artTitle) {
this.artTitle = artTitle;
}

/**
* @param appraisedValue
* the appraisedValue to set
*
public void setAppraisedValue(int appraisedValue) {
this.appraisedValue = appraisedValue;
}
  
public int compareTo(Art o) {
return (this.getArtID() - o.getArtID());
}

@Override
public String toString() {
return String.format("%-6d %-25s %d", artID, artTitle, appraisedValue);
}
}

ArtNode.java:

public class ArtNode extends Art {

   // Instance variables
   private ArtNode next;
   private ArtNode previous;
   private int artistID;

   /**
   * Constructor
   *
   * @param artID
   * - art id
   * @param artTitle
   * - art title
   * @param artistID
   * - artist id of this art
   * @param appraisedValue
   * - value
   */
   public ArtNode(int artID, String artTitle, int artistID, int appraisedValue) {
       super(artID, artTitle, appraisedValue);
       this.artistID = artistID;
       this.next = null;
       this.previous = null;
   }

   /**
   * Sets the next node
   */
   public void setNext(ArtNode n) {
       this.next = n;
   }

   /**
   * Returns the next node
   */
   public ArtNode getNext() {
       return this.next;
   }
  
   /**
   * Sets the next node
   */
   public void setPrevious(ArtNode n) {
       this.previous = n;
   }

   /**
   * Returns the next node
   */
   public ArtNode getPrevious() {
       return this.previous;
   }

   /**
   * Prints the art details
   */
   @Override
   public String toString() {
       return String.format("%-5d %-30s %-2d %.2f", getArtID(), getArtTitle(), artistID, (double)getAppraisedValue());
   }
}

MyArtists.java:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class MyArtists {

   // Instance variables
   Map<Integer, ArtistNode> artistTree;;

   /**
   * Constructor
   */
   public MyArtists(String fileName) throws FileNotFoundException {
       // Scanner to read from the file
       Scanner input;
       input = new Scanner(new File(fileName));
       artistTree = new TreeMap<Integer, ArtistNode>();

       // while file has lines
       String line;
       while (input.hasNextLine()) {
           line = input.nextLine().trim();
           String tokens[] = line.split("\s+");

           // Create a new artist node
           ArtistNode newA = new ArtistNode(tokens[1], Integer.parseInt(tokens[0].trim()));

           // Add new artist to the map
           try {
               artistTree.put(Integer.parseInt(tokens[0]), newA);
           } catch (NumberFormatException e) {
               System.out.println("Artist id "" + tokens[0] + "" not a valid integer.");
           }
       }

       // close scanner
       input.close();
   }

   /**
   * Adds an art node
   */
   public void addArt(int artistId, ArtNode newArt) {
       // Check if artist is found
       if (artistTree.containsKey(artistId)) {
           // If artist is found
           ArtistNode artist = artistTree.get(artistId);
          
           // Add art to the artist
           artist.add(newArt);
       }
   }

   /**
   * Prints the artwork to an output file
   */
   public void print(String outFile) {
       // write to file
       BufferedWriter writer;
       try {
           writer = new BufferedWriter(new FileWriter(outFile));
          
           for (Integer artistId : artistTree.keySet()) {
               System.out.println(artistTree.get(artistId));
              
               // Write artist to the file
               writer.write(artistTree.get(artistId).toString());
               writer.write(System.getProperty("line.separator"));
           }
           writer.flush();
           writer.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}

MyArtWorks.java:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MyArtWorks {

   // Instance variables
   private MyArtists myArtists;

   /**
   * Constructor
   *
   * @param fileName
   * - file from which a list of art works is to be read
   */
   public MyArtWorks(String fileName) throws FileNotFoundException {
       this.myArtists = new MyArtists("exam4Artists.txt");

       // Scanner to read from the file
       Scanner inFile = new Scanner(new File(fileName));

       // Read file one line at a time
       while (inFile.hasNextLine()) {
           // Get art id
           int artID = inFile.nextInt();

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

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

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

           // Create a new art node
           ArtNode newArt = new ArtNode(artID, artTitle.toString(), artistId, appraisedValue);

           // Add new art to the corresponding artist
           myArtists.addArt(artistId, newArt);
       }

       // close scanner
       inFile.close();
   }

   /**
   * Writes to the output file
   */
   public void print(String outFile) {
       myArtists.print(outFile);
   }
}

Exam4Step3.java:

import java.io.FileNotFoundException;

public class Exam4Step3 {
  
   private static final String OUT_FILE = "out3.txt";
  
   // Instance variables
   private MyArtWorks myArtWorks;
  
   /**
   * Constructor
   */
   public Exam4Step3() {
       try {
           myArtWorks = new MyArtWorks("exam4Arts.txt");
          
           // Print to artist - art to an output file
           print();
       } catch (FileNotFoundException fnfe) {
           System.out.println(fnfe.getMessage());
       }
   }

   /**
   * Writes the output to a file
   */
   public void print() {
       myArtWorks.print(OUT_FILE);
   }
  
   public static void main(String[] args) {
       new Exam4Step3();
   }
}

Explanation / Answer

Hi,

Your code was having compilation issues in:

1. Art.java

2.Artist.java

In both above mentioned classes you are implementing Comparable interface. So it is mandatory to implement compareTo method. In your case you were having compareTo method but with different signature.

You were using:

1. In Artist.java:

public int compareTo(Artist o) {

//Your implementation

}

However the actual signature is compareTo(Object o)

So here your method would be:

public int compareTo(Object o) {
   return this.getArt().compareTo(((Artist) o).getArt());
   }

2. Similarly in case of Art.java:

public int compareTo(Object o) {
      
   return (this.getArtID() - ((Art) o).getArtID());
   }

With these changes code worked fine for me. I am attachinng all java classess along with output for your ready reference:

============================================================

Art.java:

package com.example.node;

public class Art implements java.lang.Comparable {
   // Instance variables
   private int artID;
   private String artTitle;
   private int appraisedValue;

   /**
   * Constructor
   *
   * @param artID
   * - art id
   * @param artTitle
   * - title of the art
   * @param appraisedValue
   * - value
   **/
   public Art(int artID, String artTitle, int appraisedValue) {
   this.artID = artID;
   this.artTitle = artTitle;
   this.appraisedValue = appraisedValue;
   }

   /**
   * @return the artID
   **/
   public int getArtID() {
   return artID;
   }

   /**
   * @return the artTitle
   **/
   public String getArtTitle() {
   return artTitle;
   }

   /**
   * @return the appraisedValue
   **/
   public int getAppraisedValue() {
   return appraisedValue;
   }

   /**
   * @param artID
   * the artID to set
   **/
   public void setArtID(int artID) {
   this.artID = artID;
   }

   /**
   * @param artTitle
   * the artTitle to set
   **/
   public void setArtTitle(String artTitle) {
   this.artTitle = artTitle;
   }

   /**
   * @param appraisedValue
   * the appraisedValue to set
   **/
   public void setAppraisedValue(int appraisedValue) {
   this.appraisedValue = appraisedValue;
   }
  
   public int compareTo(Object o) {
      
   return (this.getArtID() - ((Art) o).getArtID());
   }

   @Override
   public String toString() {
   return String.format("%-6d %-25s %d", artID, artTitle, appraisedValue);
   }

  
}


===========================================================

Artist.java:

package com.example.node;

public class Artist implements java.lang.Comparable{
   // Instance variables
   private int artistID; // id of artist
   private String artistName; // name of artist
   private Art art;

   /**
   * Constructor
   *
   * @param name
   * @param number
   */
   Artist(String name, int number) {
   this.artistID = number;
   this.artistName = name;
   }

   /**
   * Constructor
   *
   * @param artistID
   * @param artistName
   * @param art
   */
   public Artist(int artistID, String artistName, Art art) {
   this.artistID = artistID;
   this.artistName = artistName;
   this.art = art;
   }

   /**
   * return the artistName
   *
   * @return
   */
   public String getArtistName() {
   return artistName;
   }

   /**
   * set the artistName
   *
   * @param artistName
   */
   public void setArtistName(String artistName) {
   this.artistName = artistName;
   }

   /**
   * return the artistId
   *
   * @return
   */
   public int getArtistID() {
   return artistID;
   }

   /**
   * set the artistId
   *
   * @param artistId
   */
   public void setArtistID(int artistId) {
   this.artistID = artistId;
   }

   /**
   * @return the art
   */
   public Art getArt() {
   return art;
   }

   /**
   * @param art
   * the art to set
   */
   public void setArt(Art art) {
   this.art = art;
   }

   public int compareTo(Object o) {
   return this.getArt().compareTo(((Artist) o).getArt());
   }

   /**
   * toString method
   */
   public String toString() {
   return String.format("%-9d %-20s", this.artistID, this.artistName);
   }
}


=============================================================

ArtistNode.java:

package com.example.node;

public class ArtistNode extends Artist{
   // Instance variables
       private ArtNode firstNode;
       private ArtNode lastNode;
       private int totalEntries;
       private double totalValue;

       /**
       * Constructor
       *
       * @param artistName
       * - artist name
       * @param artistID
       * - artist id
       */
       public ArtistNode(String artistName, int artistID) {
           super(artistName, artistID);
           this.firstNode = null;
           this.lastNode = null;
           totalEntries = 0;
           totalValue = 0;
       }

       /**
       * Adds an art node
       */
       public void add(ArtNode newArt) {
           // If this is the first node
           if (this.firstNode == null) {
               this.firstNode = newArt;
               this.lastNode = newArt;
           } else {
               // Find the appropriate place for the new art i.e. to have the art
               // nodes in ascending order
               ArtNode curr = this.firstNode;
            
               // Traverse the nodes
               while (curr != null) {
                   if (newArt.compareTo(curr) < 0) {
                       if (curr.getPrevious() != null)
                           curr.getPrevious().setNext(newArt);
                        
                       // Set previous and next of new art
                       newArt.setPrevious(curr.getPrevious());
                       newArt.setNext(curr);
                    
                       // set prev of cuur
                       curr.setPrevious(newArt);
                    
                       break;
                   }
                
                   // Advance curr
                   curr = curr.getNext();
               }
            
               // Check if newArt is the first node
               if (curr == this.firstNode)
                   this.firstNode = newArt;
            
               // Check if the newArt is added
               if (curr == null) {
                   this.lastNode.setNext(newArt);
                   newArt.setPrevious(this.lastNode);
                   this.lastNode = newArt;
               }
           }

           // Update count and value
           totalEntries += 1;
           totalValue += newArt.getAppraisedValue();
       }

       /**
       * Returns the total art work by this artist
       */
       public int getTotalEntries() {
           return totalEntries;
       }

       /**
       * Returns the total value of the art work by this artist
       */
       public double getTotalValue() {
           return totalValue;
       }

       /**
       * Displays the artist details
       */
       public String toString() {
           StringBuffer art = new StringBuffer();
           String lineSep = System.getProperty("line.separator");
        
           // Add arts
           // Traverse the nodes
           ArtNode curr = this.firstNode;
           while (curr != null) {
               art.append(lineSep);
               art.append(curr);
            
               // Advance curr
               curr = curr.getNext();
           }
        
           return String.format("%-3d %-15s %-3d %.2f", getArtistID(), getArtistName(), this.totalEntries,
                   this.totalValue) + art.toString() + lineSep;
       }
}


============================================================

ArtNode.java:

package com.example.node;

public class ArtNode extends Art {
   // Instance variables
       private ArtNode next;
       private ArtNode previous;
       private int artistID;

       /**
       * Constructor
       *
       * @param artID
       * - art id
       * @param artTitle
       * - art title
       * @param artistID
       * - artist id of this art
       * @param appraisedValue
       * - value
       */
       public ArtNode(int artID, String artTitle, int artistID, int appraisedValue) {
           super(artID, artTitle, appraisedValue);
           this.artistID = artistID;
           this.next = null;
           this.previous = null;
       }

       /**
       * Sets the next node
       */
       public void setNext(ArtNode n) {
           this.next = n;
       }

       /**
       * Returns the next node
       */
       public ArtNode getNext() {
           return this.next;
       }
  
       /**
       * Sets the next node
       */
       public void setPrevious(ArtNode n) {
           this.previous = n;
       }

       /**
       * Returns the next node
       */
       public ArtNode getPrevious() {
           return this.previous;
       }

       /**
       * Prints the art details
       */
       @Override
       public String toString() {
           return String.format("%-5d %-30s %-2d %.2f", getArtID(), getArtTitle(), artistID, (double)getAppraisedValue());
       }
}


============================================================

MyArtists.java:

package com.example.node;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class MyArtists {
   // Instance variables
       Map<Integer, ArtistNode> artistTree;;

       /**
       * Constructor
       */
       public MyArtists(String fileName) throws FileNotFoundException {
           // Scanner to read from the file
           Scanner input;
           input = new Scanner(new File(fileName));
           artistTree = new TreeMap<Integer, ArtistNode>();

           // while file has lines
           String line;
           while (input.hasNextLine()) {
               line = input.nextLine().trim();
               String tokens[] = line.split("\s+");

               // Create a new artist node
               ArtistNode newA = new ArtistNode(tokens[1], Integer.parseInt(tokens[0].trim()));

               // Add new artist to the map
               try {
                   artistTree.put(Integer.parseInt(tokens[0]), newA);
               } catch (NumberFormatException e) {
                   System.out.println("Artist id "" + tokens[0] + "" not a valid integer.");
               }
           }

           // close scanner
           input.close();
       }

       /**
       * Adds an art node
       */
       public void addArt(int artistId, ArtNode newArt) {
           // Check if artist is found
           if (artistTree.containsKey(artistId)) {
               // If artist is found
               ArtistNode artist = artistTree.get(artistId);
            
               // Add art to the artist
               artist.add(newArt);
           }
       }

       /**
       * Prints the artwork to an output file
       */
       public void print(String outFile) {
           // write to file
           BufferedWriter writer;
           try {
               writer = new BufferedWriter(new FileWriter(outFile));
            
               for (Integer artistId : artistTree.keySet()) {
                   System.out.println(artistTree.get(artistId));
                
                   // Write artist to the file
                   writer.write(artistTree.get(artistId).toString());
                   writer.write(System.getProperty("line.separator"));
               }
               writer.flush();
               writer.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
}


============================================================

MyArtworks.java:

package com.example.node;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MyArtWorks {
   // Instance variables
       private MyArtists myArtists;

       /**
       * Constructor
       *
       * @param fileName
       * - file from which a list of art works is to be read
       */
       public MyArtWorks(String fileName) throws FileNotFoundException {
           this.myArtists = new MyArtists("exam4Artists.txt");

           // Scanner to read from the file
           Scanner inFile = new Scanner(new File(fileName));

           // Read file one line at a time
           while (inFile.hasNextLine()) {
               // Get art id
               int artID = inFile.nextInt();

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

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

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

               // Create a new art node
               ArtNode newArt = new ArtNode(artID, artTitle.toString(), artistId, appraisedValue);

               // Add new art to the corresponding artist
               myArtists.addArt(artistId, newArt);
           }

           // close scanner
           inFile.close();
       }

       /**
       * Writes to the output file
       */
       public void print(String outFile) {
           myArtists.print(outFile);
       }
}


=============================================================

Exam4Step3.java

package com.example.node;
import java.io.FileNotFoundException;
public class Exam4Step3 {
   private static final String OUT_FILE = "out3.txt";
  
       // Instance variables
       private MyArtWorks myArtWorks;
  
       /**
       * Constructor
       */
       public Exam4Step3() {
           try {
               myArtWorks = new MyArtWorks("exam4Arts.txt");
            
               // Print to artist - art to an output file
               print();
           } catch (FileNotFoundException fnfe) {
               System.out.println(fnfe.getMessage());
           }
       }

       /**
       * Writes the output to a file
       */
       public void print() {
           myArtWorks.print(OUT_FILE);
       }
  
       public static void main(String[] args) {
           new Exam4Step3();
       }
}


===========================================================

out3.txt:

1   Acconci         3   18800.00
1038 Spring Flowers                 1 800.00
1050 Cattle Ranch                   1 10000.00
1103 Trail End                      1 8000.00

2   Budd            3   35544.00
1042 Coffee on the Trail            2 7544.00
1086 Untitled (desert landscape)    2 18000.00
1101 The Red Door                   2 10000.00

3   Carpenter       12 284975.00
1010 Untitled (land with adobe)     3 800.00
1013 Superstitions                  3 78000.00
1019 The White Heart                3 9300.00
1021 Bead Wall                      3 14000.00
1022 The Cowboy                     3 4200.00
1025 Profile of a Woman             3 625.00
1034 Beaver Pole Jumble             3 28000.00
1058 The Gathering                  3 250.00
1063 Asleep in the Garden           3 110000.00
1080 The Dust Behind                3 18000.00
1095 The Spirit                     3 20000.00
1104 Untitled                       3 1800.00

4   Dill            3   38700.00
1070 Beginnings                     4 27500.00
1079 Carrying the Mail              4 8000.00
1111 Untitled (man and crucifix)    4 3200.00

5   Edwards         3   41000.00
1020 Untitled (Man holding coat)    5 3000.00
1036 Blackhawk                      5 25500.00
1093 Antelopes                      5 12500.00

6   Fleming         13 171700.00
1004 Seeking Shelter                6 52000.00
1012 Man on Horseback               6 8000.00
1016 Untitled                       6 6000.00
1017 Brittlecone                    6 1300.00
1033 Untitled (Woman abstract)      6 2500.00
1053 Blue Eyed Indian               6 40000.00
1056 Cavalry Is Coming              6 1900.00
1066 Untitled (still life)          6 19500.00
1075 Western Boots and Spurs        6 6000.00
1077 Bull Riding                    6 5200.00
1085 Traces                         6 20000.00
1097 Untitled (Sea)                 6 2800.00
1110 Three Sisters                  6 6500.00

7   Garber          9   46862.00
1024 Spirit and Nature              7 592.00
1026 Untitled (couple)              7 4000.00
1032 Rising Sun                     7 2000.00
1037 Floating World                 7 2350.00
1049 Buttercup with Red Lip         7 400.00
1054 Snake Charmer                  7 4500.00
1061 Untitled Mural                 7 3520.00
1091 Stone Palette                  7 11500.00
1114 Storytelling at the Campfire   7 18000.00

8   Higgins         9   55700.00
1018 Mountain Scene                 8 2500.00
1057 Untitled                       8 4500.00
1060 Story Sticks                   8 650.00
1064 Spirit Columns                 8 7000.00
1067 Owl in Flight                  8 7000.00
1068 Moonlight                      8 9750.00
1071 Ride the Rapids                8 300.00
1074 Storm on the Rise              8 8000.00
1109 Friends                        8 16000.00

9   Ibe             7   59792.00
1001 Red Rock Mountain              9 18000.00
1002 Offerings                      9 10000.00
1044 Mexican Fiesta                 9 14000.00
1055 Starlit Evening                9 9500.00
1069 Renaissance                    9 5500.00
1084 Crossing the Platt River       9 2200.00
1098 Sweet Project                  9 592.00

10 Kollasch        7   37125.00
1028 Tired Cowboy                   10 4700.00
1047 Medicine Man                   10 2500.00
1048 Comfy Chair                    10 800.00
1072 Funnel                         10 4500.00
1089 Life Lessons                   10 4125.00
1096 Ceremonial Sticks              10 15000.00
1113 Shadow House                   10 5500.00


=============================================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote