Part A. Data Files: “p1artists.txt” that we used for project #1 and “p2changes.t
ID: 3590375 • Letter: P
Question
Part A.
Data Files: “p1artists.txt” that we used for project #1 and “p2changes.txt”.
Assignment: Use the Linked approach to update “p1artists.txt” through “p2changes.txt” to produce “p3artists.txt”, which should have the same results produced in project #2.
Use System.nanoTime() to find the time spent on this approach and compare the result with the output from project #2. (Update your summary file from project 2 to include the result from this project and submit the final version this time.)
Part B.
Data File: “p1arts”.txt”
Assignment:
Use an array of linked list to process the data file to produce the output similar to the following: Name the output file “p3artists_arts.txt”.
Artist ID
Artist Name
Art ID
Art Title
Appraised Value
50
Morgan
1001
Red Rock Mountain
18000
1028
Tired Cowboy
4700
1054
Snake Charmer
4500
1068
Moonlight
9750
1069
Renaissance
5500
1113
Shadow House
5500
1114
Storytelling at the Campfire
18000
52
Novarre
1002
Offerings
10000
Each artist will occupy one row of the array.
Try to design the structure of the first node. In addition to “artistID” and “next” fields, what else should it contain?
----------------------------------------------------
p1artists.txt
-----------------------------------------------
1 Acconci
2 Ames
3 Aserty
4 Baron
5 Battenberg
6 Bindner
7 Blain
8 Blum
9 Budd
10 Cardenas
11 Carpenter
12 Chico
13 Colvin
14 Cox
15 Cridler
16 Curtis
17 Dawson
18 DiGrigoro
19 Dill
20 Edwards
21 Fleming
22 Fratt
23 Garber
24 Garin
25 Giama
26 Gilhooly
27 Gonzales
28 Guys
29 Hamend
30 Higgins
31 Hofmann
32 Ibe
33 Indiana
34 Ingraham
35 Irvin
36 Kerrihard
37 Kollasch
38 Kritz
39 Lerman
40 Long
41 Lowney
42 Lundquist
43 Lutes
44 Maglich
45 McGraw
46 McIver
47 Meglech
48 Metz
49 Miller
50 Mogan
51 Motherwell
52 Novarre
53 Odiezma
54 Ortega
55 Parker
56 Penn
57 Pierobon
58 Prinzen
59 Quiroz
60 Rath
-------------
p1arts
--------------
1001 Red Rock Mountain 50 18000
1002 Offerings 52 10000
1003 Spring Flowers 12 2400
1004 Seeking Shelter 64 52000
1005 The Hang 18 8000
1006 House Remembered 32 700
1007 Homage to the Ancestors 82 1200
1008 End of the Path 26 1900
1009 Amen 28 3000
1010 Untitled (land with adobe) 71 800
1011 Eve 19 975
1012 Man on Horseback 74 8000
1013 Superstitions 3 78000
1014 Plenty 45 500
1015 Punch 46 10000
1016 Untitled 65 6000
1017 Brittlecone 6 1300
1018 Mountain Scene 8 2500
1019 The White Heart 61 9300
1020 Untitled (Man holding coat) 73 3000
1021 Bead Wall 3 14000
1022 The Cowboy 69 4200
1023 Shooting the Rapids 47 1300
1024 Spirit and Nature 48 592
1025 Profile of a Woman 68 625
1026 Untitled (couple) 66 4000
1027 Mountain Climber 47 4700
1028 Tired Cowboy 50 4700
1029 Horseshoe Falls 31 15000
1030 Ash Bench 28 13000
1031 Inside/Out 34 3500
1032 Rising Sun 42 2000
1033 Untitled (Woman abstract) 77 2500
1034 Beaver Pole Jumble 3 28000
1035 Nature/Nurture 47 1300
1036 Blackhawk 5 25500
1037 Floating World 21 2350
1038 Spring Flowers 1 800
1039 Treachery 14 20000
1040 Night on the Praire 47 1300
1041 Night Version 29 3800
1042 Coffee on the Trail 2 7544
1043 Creosote Bushes 28 18000
1044 Mexican Fiesta 43 14000
1045 Leaf Patterns 38 2100
1046 Immediate Gratification 33 1500
1047 Medicine Man 44 2500
1048 Comfy Chair 57 800
1049 Buttercup with Red Lip 7 400
1050 Cattle Ranch 1 10000
1051 Night Version 36 7000
1052 American Rodeo 16 3500
1053 Blue Eyed Indian 6 40000
1054 Snake Charmer 50 4500
1055 Starlit Evening 9 9500
1056 Cavalry Is Coming 6 1900
1057 Untitled 66 4500
1058 The Gathering 60 250
1059 Dwelling 17 16000
1060 Story Sticks 42 650
1061 Untitled Mural 78 3520
1062 Cowboy and Saddle 41 18000
1063 Asleep in the Garden 3 110000
1064 Spirit Columns 51 7000
1065 Moonlite 47 1300
1066 Untitled (still life) 76 19500
1067 Owl in Flight 49 7000
1068 Moonlight 50 9750
1069 Renaissance 50 5500
1070 Beginnings 4 27500
1071 Ride the Rapids 79 300
1072 Funnel 24 4500
1073 Dancing in the Light 15 4000
1074 Storm on the Rise 55 8000
1075 Western Boots and Spurs 6 6000
1076 Ride the Bronco 79 1500
1077 Bull Riding 6 5200
1078 Chuckwagon 28 32000
1079 Carrying the Mail 62 8000
1080 The Dust Behind 59 18000
1081 Coming Under Fire 13 650
1082 Spring Flowers 29 20000
1083 Untitled 64 2500
1084 Crossing the Platt River 23 2200
1085 Traces 63 20000
1086 Untitled (desert landscape) 67 18000
1087 Three Woman 81 20000
1088 Lessons 37 3700
1089 Life Lessons 53 4125
1090 Off the Grid 11 8000
1091 Stone Palette 54 11500
1092 Dressing Up 47 1300
1093 Antelopes 62 12500
1094 Life Is Sweet 39 25000
1095 The Spirit 61 20000
1096 Ceremonial Sticks 10 15000
1097 Untitled (Sea) 75 2800
1098 Sweet Project 56 592
1099 Watch That Rattler 20 900
1100 Hungry Cowboys 38 750
1101 The Red Door 58 10000
1102 Crying Hats 14 10000
1103 Trail End 1 8000
1104 Untitled 70 1800
1105 Meteor Show 80 10000
1106 Horse Corral 40 12500
1107 Striking It Rich 35 1750
1108 Untitled Mural 77 400
1109 Friends 22 16000
1110 Three Sisters 62 6500
1111 Untitled (man and crucifix) 72 3200
1112 Dark Canyon 27 8000
1113 Shadow House 50 5500
1114 Storytelling at the Campfire 50 18000
1115 Starry Night 25 8500
1116 Apache Warrior 30 23000
---------------------
p2changes
---------------
A Reed
A Rissman
D 11
A Rogers
A Roman
A Schenck
D 16
A Scherzel
A Scholder
D 21
D 31
A Senior
D 41
A Shenal
A Statom
A Swartz
A Tidwell
D 46
A Turrell
A Udinotti
A Van Coller
A Waid
D 51
A Werner
A Wittner
D 55
A Wright
A Xie
A Yasami
A Zischke
Artist ID
Artist Name
Art ID
Art Title
Appraised Value
50
Morgan
1001
Red Rock Mountain
18000
1028
Tired Cowboy
4700
1054
Snake Charmer
4500
1068
Moonlight
9750
1069
Renaissance
5500
1113
Shadow House
5500
1114
Storytelling at the Campfire
18000
52
Novarre
1002
Offerings
10000
Explanation / Answer
Main.java
--------------------------------------------------
import java.io.IOException;
public class Main {
public static void main(String[] args0) throws IOException{
UpdateArtistList x =new UpdateArtistList();
}
}
---------------------------------------------------
Art.java
---------------------------------
public class Art {
int artistID,artID,value;
String artTitle;
public Art(int artistID, int artID, int value, String artTitle) {
this.artistID = artistID;
this.artID = artID;
this.value = value;
this.artTitle = artTitle;
}
public int getArtistID() {
return artistID;
}
public void setArtistID(int artistID) {
this.artistID = artistID;
}
public int getArtID() {
return artID;
}
public void setArtID(int artID) {
this.artID = artID;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getArtTitle() {
return artTitle;
}
public void setArtTitle(String artTitle) {
this.artTitle = artTitle;
}
@Override
public String toString() {
return String.format("%5d %10s %5d",artID,artTitle,value);
}
}
--------------------------------------------------------------------
ArtistList.java
-----------------------------------
import java.util.ArrayList;
public class Artist {
private int artistID;
private String artistName;
ArrayList<Art> art;
public Artist(int iD, String name) {
artistID = iD;
artistName = name;
art=new ArrayList<>();
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public int getArtistID() {
return artistID;
}
public void setArtistID(int artistID) {
this.artistID = artistID;
}
@Override
public String toString() {
return artistID + " " + artistName;
}
}
-------------------------------------------------------------------------
Artist.java
----------------------------------
import java.util.ArrayList;
public class Artist {
private int artistID;
private String artistName;
ArrayList<Art> art;
public Artist(int iD, String name) {
artistID = iD;
artistName = name;
art=new ArrayList<>();
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public int getArtistID() {
return artistID;
}
public void setArtistID(int artistID) {
this.artistID = artistID;
}
@Override
public String toString() {
return artistID + " " + artistName;
}
}
----------------------------------------------------------------------------------------
ArtistNode.java
------------------------------------------
public class ArtistNode extends Artist{
public ArtistNode next;
int totalEntries;
public ArtistNode(int id, String name, ArtistNode next) {
super(id, name);
this.next=next;
}
public boolean isEmpty(){return totalEntries==0;}
public ArtistNode getNext() {
return next;
}
public void setNext(ArtistNode next) {
this.next = next;
}
@Override
public String toString() {
return getArtistID() + " " + getArtistName();
}
}
------------------------------------------------------------------------------------
ReadArtistList.java
--------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadArtistList {
private Scanner fileArtist;
public ReadArtistList(){
try {
fileArtist=new Scanner(new File("p1artists.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public ArtistList readFile(ArtistList list){
fileArtist.useDelimiter(" | ");
while (fileArtist.hasNext()){
int id=fileArtist.nextInt();
String name=fileArtist.next();
list.addNode(id,name);
}
return list;
}
public void closeFile(){
fileArtist.close();
}
}
---------------------------------------------------------------------------------------
UpdateArtistList.java
------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class UpdateArtistList {
java.io.PrintWriter output = new java.io.PrintWriter(new File("p3artists.txt"));
ArtistList artistList = new ArtistList();
Scanner fileChange;
ReadArtistList artistListFile = new ReadArtistList();
ArrayList<Artist> artists=new ArrayList<>();
public UpdateArtistList() throws IOException{
long startTime=System.nanoTime();
artistList=artistListFile.readFile(artistList);
artistListFile.closeFile();
changes();
long stopTime=System.nanoTime();
long elapsedTime=stopTime-startTime;
artistList.print(ArtistList.lastNode,output);
output.close();
arts();
output.println(" StartTime = " + startTime + " StopTime = " + stopTime
+ " ElapsedTime = " + elapsedTime);
output.close();
}
public void changes() throws IOException {
fileChange=new Scanner(new File("p2changes.txt"));
int i=artistList.totalEntries+1;
while (fileChange.hasNext()){
String sample=fileChange.nextLine();
String[] sample2=sample.split(" ");
String codeStr = sample2[0];
if (codeStr.charAt(0) == 'D'||codeStr.charAt(0) == 'd') {
int id = Integer.parseInt(sample2[1]);
artistList.delete(id);
} else if (codeStr.charAt(0) == 'A'||codeStr.charAt(0) == 'a') {
String itemname=sample2[1];
artistList.addNode(i,itemname);
i++;
}
}fileChange.close();
}
public void arts() throws IOException {
Scanner read=new Scanner(new File("p3artists.txt"));
read.useDelimiter(" | ");
while (read.hasNext()){
int id=read.nextInt();
String name=read.next();
artists.add(new Artist(id,name));
}read.close();
Scanner artFile=new Scanner(new File("p1arts.txt"));
artFile.useDelimiter(" | ");
while (artFile.hasNext()){
int artID=artFile.nextInt();
String title=artFile.next();
int id=artFile.nextInt();
int value=artFile.nextInt();
for (Artist artist:artists){
if (artist.getArtistID()==id){
artist.art.add(new Art(id,artID,value,title));
}
}
}artFile.close();
java.io.PrintWriter artlist=new java.io.PrintWriter(new File("p3artists_arts.txt"));
for(Artist artist:artists) {
artlist.print(artist);
for (Art art:artist.art)
artlist.println(" "+art);
}
artlist.close();
}
}
-------------------------------------------------------------------
BagInterface.java
--------------------------------------
public interface BagInterface<T>
{
public int getCurrentSize();
public boolean isEmpty();
public boolean add(T newEntry);
public T remove();
public boolean remove(T anEntry);
public void clear();
public int getFrequencyOf(T anEntry);
public boolean contains(T anEntry);
public T[] toArray();
// public <T> T[] toArray(); // Alternate
// public Object[] toArray(); // Alternate
// public BagInterface<T> union(BagInterface<T> anotherBag);
// public BagInterface<T> intersection(BagInterface<T> anotherBag);
// public BagInterface<T> difference(BagInterface<T> anotherBag);
} // end BagInterface
-------------------------------------------------------------------------------------
LinkedBag.java
---------------------------------
public final class LinkedBag<T> implements BagInterface<T>
{
private Node firstNode; // Reference to first node
private int numberOfEntries;
public LinkedBag()
{
firstNode = null;
numberOfEntries = 0;
} // end default constructor
public boolean isEmpty()
{
return numberOfEntries == 0;
} // end isEmpty
public int getCurrentSize()
{
return numberOfEntries;
} // end getCurrentSize
public boolean add(T newEntry) // OutOfMemoryError possible
{
// Add to beginning of chain:
Node newNode = new Node(newEntry);
newNode.next = firstNode; // Make new node reference rest of chain
// (firstNode is null if chain is empty)
firstNode = newNode; // New node is at beginning of chain
numberOfEntries++;
return true;
} // end add
public T[] toArray()
{
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast
int index = 0;
Node currentNode = firstNode;
while ((index < numberOfEntries) && (currentNode != null))
{
result[index] = currentNode.getData();
index++;
currentNode = currentNode.getNextNode();
} // end while
return result;
} // end toArray
public int getFrequencyOf(T anEntry)
{
int frequency = 0;
int counter = 0;
Node currentNode = firstNode;
while ((counter < numberOfEntries) && (currentNode != null))
{
if (anEntry.equals(currentNode.getData()))
{
frequency++;
} // end if
counter++;
currentNode = currentNode.getNextNode();
} // end while
return frequency;
} // end getFrequencyOf
public boolean contains(T anEntry)
{
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null))
{
if (anEntry.equals(currentNode.getData()))
found = true;
else
currentNode = currentNode.getNextNode();
} // end while
return found;
} // end contains
// Locates a given entry within this bag.
// Returns a reference to the node containing the entry, if located,
// or null otherwise.
private Node getReferenceTo(T anEntry)
{
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null))
{
if (anEntry.equals(currentNode.getData()))
found = true;
else
currentNode = currentNode.getNextNode();
} // end while
return currentNode;
} // end getReferenceTo
public void clear()
{
while (!isEmpty())
remove();
} // end clear
public T remove()
{
T result = null;
if (firstNode != null)
{
result = firstNode.getData();
firstNode = firstNode.getNextNode(); // Remove first node from chain
numberOfEntries--;
} // end if
return result;
} // end remove
public boolean remove(T anEntry)
{
boolean result = false;
Node nodeN = getReferenceTo(anEntry);
if (nodeN != null)
{
nodeN.setData(firstNode.getData()); // Replace located entry with entry in first node
firstNode = firstNode.getNextNode(); // Remove first node
numberOfEntries--;
result = true;
} // end if
return result;
} // end remove
private class Node
{
private T data; // Entry in bag
private Node next; // Link to next node
private Node(T dataPortion)
{
this(dataPortion, null);
} // end constructor
private Node(T dataPortion, Node nextNode)
{
data = dataPortion;
next = nextNode;
} // end constructor
private T getData()
{
return data;
} // end getData
private void setData(T newData)
{
data = newData;
} // end setData
private Node getNextNode()
{
return next;
} // end getNextNode
private void setNextNode(Node nextNode)
{
next = nextNode;
} // end setNextNode
} // end Node
} // end LinkedBag