i am providing the class file templates and methods to complete this program( yo
ID: 3788830 • Letter: I
Question
i am providing the class file templates and methods to complete this program( you need to add the code for it) and the text files.
Using Linked list approach, read the files and (add or delete specified records using linked list) and output the updated list to "p3artists.txt" file:
Introduction:
We continue with the Bag data structure. However, in this project we focus on the representation of the Bag structure using singular linked list as described in chapter 3 of your textbook. Please study the sample programs “LinkedBag.java” and “LinkedBagDemo.java”.
Part A.
Data Files: “p1artists.txt” and “p2changes.txt”.
Assignment:
1) Use the Linked approach to update “p1artists.txt” through “p2changes.txt” to produce “p3artists.txt”.
If A : Add record using linked list
If D : Delete record using linked list
2) Use System.nanoTime() to find the time spent on this approach
a) Artist.java
public class Artist {
private int artistID; //id of artist
private String artistName; //name of artist
Artist(String name, int number) {
this.artistID = number;
this.artistName = name;
}
//return the artistName
public String getArtistName() {
return artistName;
}
//set the artistName
public void setArtistName(String artistName) {
this.artistName = artistName;
}
//return the artistId
public int getArtistID() {
return artistID;
}
// set the artistId
public void setArtistID(int artistId) {
this.artistID = artistId;
}
// toString method
public String toString() {
return this.artistID + " " + this.artistName;
}
}
b) ArtistNode.java:
public class ArtistNode extends Artist{
private ArtistNode next;
public ArtistNode(int ID, String name, ArtistNode next) {
super(ID, name);
this.next = next;
}
public void setNext(ArtistNode next)
public ArtistNode getNext()
public String toString()
}
c) ArtistList.java:
public class ArtistList
{
ArtistNode list, lastNode;
int totalEntries;
int nextID;
public ArtistList() {
list = null;
lastNode = null;
totalEntries = 0;
nextID = 0;
}
public void setNextID(int id)
15 public int getNextID()
public boolean isEmpty()
public void print()
public void addNode(int id, String name) {
ArtistNode tmp = new ArtistNode(id, name, null);
if (isEmpty()) { … } else { … }
}
public void add(String name)
public boolean delete(int id) {
ArtistNode previous = null, current = list;
if (isEmpty()) { … }
else {
while (current != null) {
if (id == current.getID()) { … }
else {
previous = current;
current = current.getNext();
}
}
}
return false;
}
public ArtistNode find(String name)
{ ,,,
}
}
d) ReadArtistList.java:
To read the main file "p1artists.txt".
e)UpdateArtistList.java:
To update according to "p2changes.txt" file after reading the file (A: add the record or D: delete the record specified)
Output the updated list to p3artists.txt
f) P3a.java:
public static void main {
UpdateArtistList x = new UpdateArtistList();
}
p2changes.txt:
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
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
61 Reed
62 Rissman
63 Rogers
64 Roman
65 Schenck
66 Scherzel
67 Scholder
68 Senior
69 Shenal
70 Statom
71 Swartz
72 Tidwell
73 Turrell
74 Udinotti
75 Van Coller
76 Waid
77 Werner
78 Wittner
79 Wright
80 Xie
81 Yasami
82 Zischke
Explanation / Answer
//Following are all filesmodified as per mentioned in problem statement.
// Artist.java
public class Artist
{
private int artistID; //id of artist
private String artistName; //name of artist
Artist(String name, int number)
{
this.artistID = number;
this.artistName = name;
}
//return the artistName
public String getArtistName() {
return artistName;
}
//set the artistName
public void setArtistName(String artistName) {
this.artistName = artistName;
}
//return the artistId
public int getArtistID() {
return artistID;
}
// set the artistId
public void setArtistID(int artistId) {
this.artistID = artistId;
}
// toString method
public String toString() {
return this.artistID + " " + this.artistName;
}
}
// ArtistNode.java
public class ArtistNode extends Artist
{
private ArtistNode next;
public ArtistNode(int ID, String name, ArtistNode next)
{
super(name,ID);
this.next = next;
}
public void setNext(ArtistNode next)
{
this.next=next;
}
public ArtistNode getNext()
{
return next;
}
public String toString()
{
return super.toString();
}
}
// ArtistList.java
public class ArtistList
{
ArtistNode list, lastNode;
int totalEntries;
int nextID;
public ArtistList()
{
list = null;
lastNode = null;
totalEntries = 0;
nextID = 0;
}
public ArtistNode getListNode()
{
return list;
}
public void setNextID(int id)
{
nextID=id;
}
public int getNextID()
{
return nextID;
}
public boolean isEmpty()
{
if(list==null)
return true;
return false;
}
public void print()
{
System.out.println("Artist id Artist Name ");
ArtistNode temp=list;
while(temp!=null)
{
System.out.println(temp.toString());
temp=temp.getNext();
}
}
public void addNode(int id, String name)
{
ArtistNode tmp = new ArtistNode(id, name, null);
if (isEmpty())
{
list=tmp;
lastNode=tmp;
}
else
{
lastNode.setNext(tmp);
lastNode=tmp;
}
totalEntries++;
}
public void add(String name)
{
//ArtistNode temp=new ArtistNode(lastNode.getArtistID()+1,name,null);
//addNode(lastNode.getArtistID()+1,name,null);
addNode(getNextID(),name);
setNextID(getNextID()+1);
}
public boolean delete(int id)
{
ArtistNode previous = null, current = list;
if (isEmpty())
{
return false;
}
else
{
while (current != null)
{
if (id == current.getArtistID())
{
previous.setNext(current.getNext());
return true;
}
else
{
previous = current;
current = current.getNext();
}
}
}
return false;
}
public ArtistNode find(String name)
{
ArtistNode temp=list;
while(temp!=null)
{
if(temp.getArtistName().equals(name))
return temp;
temp=temp.getNext();
}
return null;
}
}
// ReadArtistList.java
import java.io.*;
import java.lang.*;
public class ReadArtistList
{
/* public ReadArtistList()
{
}
*/ public ArtistList getList()
{
ArtistList a=new ArtistList();
try
{
//reading from file "items.txt"
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("./p1artists.txt")));
String line = reader.readLine();
while (line != null)
{
//splitting file by comma
String[] array = line.split(" ");
a.addNode(Integer.parseInt(array[0]),array[1]);
line = reader.readLine();
}
reader.close();
//return a;
}
catch(Exception e)
{
e.printStackTrace();
}
return a;
}
/* public static void main()
{
}
*/
}
// UpdateArtistList.java
import java.io.*;
import java.lang.*;
public class UpdateArtistList
{
private ArtistList t;
public UpdateArtistList()
{
try
{
t=new ReadArtistList().getList();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("./p2art_changes.txt")));
String line = reader.readLine();
while (line != null)
{
//splitting file by comma
String[] array = line.split(" ");
//checking the string of whether it is for adding new item or deleting an item
if(array[0].equals("A") || array[0].equals("a"))
{
t.add(array[1]);
}
else if(array[0].equals("D") || array[0].equals("d"))
{
//iterating through array for finding an item to delete
t.delete(Integer.parseInt(array[1]));
}
line = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public ArtistList getUpdatedList()
{
return t;
}
}
// P3a.java
import java.io.*;
import java.lang.*;
public class P3a
{
public static void main(String args[])
{
try
{
UpdateArtistList up=new UpdateArtistList();
PrintWriter writer = new PrintWriter("./p3artists.txt");
ArtistList a=up.getUpdatedList();
ArtistNode temp=a.getListNode();
while(temp!=null)
{
writer.println(temp.toString());
temp=temp.getNext();
}
writer.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//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
61 Reed
62 Rissman
63 Rogers
64 Roman
65 Schenck
66 Scherzel
67 Scholder
68 Senior
69 Shenal
70 Statom
71 Swartz
72 Tidwell
73 Turrell
74 Udinotti
75 Van Coller
76 Waid
77 Werner
78 Wittner
79 Wright
80 Xie
81 Yasami
82 Zischke
// p2art_changes.txt
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
// Output :
//p3artists.txt
1 Acconci
2 Ames
3 Aserty
4 Baron
5 Battenberg
6 Bindner
7 Blain
8 Blum
9 Budd
10 Cardenas
12 Chico
13 Colvin
14 Cox
15 Cridler
17 Dawson
18 DiGrigoro
19 Dill
20 Edwards
22 Fratt
23 Garber
24 Garin
25 Giama
26 Gilhooly
27 Gonzales
28 Guys
29 Hamend
30 Higgins
32 Ibe
33 Indiana
34 Ingraham
35 Irvin
36 Kerrihard
37 Kollasch
38 Kritz
39 Lerman
40 Long
42 Lundquist
43 Lutes
44 Maglich
45 McGraw
47 Meglech
48 Metz
49 Miller
50 Mogan
52 Novarre
53 Odiezma
54 Ortega
56 Penn
57 Pierobon
58 Prinzen
59 Quiroz
60 Rath
61 Reed
62 Rissman
63 Rogers
64 Roman
65 Schenck
66 Scherzel
67 Scholder
68 Senior
69 Shenal
70 Statom
71 Swartz
72 Tidwell
73 Turrell
74 Udinotti
75 Van
76 Waid
77 Werner
78 Wittner
79 Wright
80 Xie
81 Yasami
82 Zischke
0 Reed
1 Rissman
2 Rogers
3 Roman
4 Schenck
5 Scherzel
6 Scholder
7 Senior
8 Shenal
9 Statom
10 Swartz
11 Tidwell
12 Turrell
13 Udinotti
14 Van
15 Waid
16 Werner
17 Wittner
18 Wright
19 Xie
20 Yasami
21 Zischke
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.