i am providing the class file templates and methods to compl
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:
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 + \"\\t\" + 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
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
LinkedBag.java:
Solution
public class Artist {
// Instance variables
private int artistID; // id of artist
private String artistName; // name of artist
/**
* Constructor
* @param name
* @param number
*/
Artist(String name, int number) {
this.artistID = number;
this.artistName = name;
}
/**
* 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;
}
/**
* toString method
*/
public String toString() {
return this.artistID + \"\\t\" + this.artistName;
}
}
public class ArtistNode extends Artist {
// Instance variable
private ArtistNode next;
/**
* Constructor
* @param ID
* @param name
* @param next
*/
public ArtistNode(int ID, String name, ArtistNode next) {
super(name, ID);
this.next = next;
}
/**
* @return the next
*/
public ArtistNode getNext() {
return next;
}
/**
* @param next the next to set
*/
public void setNext(ArtistNode next) {
this.next = next;
}
public String toString() {
return super.toString();
}
}
public class ArtistList {
// Instance variables
private ArtistNode firstNode;
private int totalEntries;
private int nextID;
/**
* Constructor
*/
public ArtistList() {
this.firstNode = null;
this.totalEntries = 0;
}
/**
* Returns true if totalEntries = 0 false otherwise
*
* @return
*/
public boolean isEmpty() {
if (this.totalEntries == 0)
return true;
else
return false;
}
/**
* Adds an artist with id and name
*
* @param id
* @param name
*/
public void addNode(int id, String name) {
this.nextID = id + 1;
// Create a new ArtistNode
ArtistNode newNode = new ArtistNode(id, name, this.firstNode);
// Set newNode as firstNode
this.firstNode = newNode;
this.totalEntries += 1;
}
/**
* Adds an artist with name to the list
*
* @param name
*/
public void add(String name) {
addNode(this.nextID, name);
}
/**
* Deletes an artist from the list
*
* @param id
* @return
*/
public boolean delete(int id) {
if (!isEmpty()) {
ArtistNode node = this.firstNode;
boolean found = false;
// Traverse through the list
while (node != null) {
if (node.getArtistID() == id) {
found = true;
// Set data of this node to the data of the firstNode
node.setArtistID(this.firstNode.getArtistID());
node.setArtistName(this.firstNode.getArtistName());
// Remove firstNode
this.firstNode = this.firstNode.getNext();
this.totalEntries -= 1;
break;
}
// Move node to the next node
node = node.getNext();
}
return found;
}
return false;
}
/**
* Finds a node containing an artist by name and returns a reference to that
* node
*
* @param name
* @return
*/
public ArtistNode find(String name) {
if (!isEmpty()) {
ArtistNode node = this.firstNode;
// Traverse through the list
while (node != null) {
if (node.getArtistName().equalsIgnoreCase(name)) {
return node;
}
// Move node to the next node
node = node.getNext();
}
}
return null;
}
/**
* Prints the list
*/
public void print() {
System.out.println(this);
}
public String toString() {
StringBuffer sb = new StringBuffer();
if (this.firstNode != null) {
ArtistNode node = this.firstNode;
// Traverse through the list
while (node != null) {
sb.append(node + \"\ \");
node = node.getNext();
}
}
return sb.toString();
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadArtistList {
private static final String FILE_NAME = \"p1artists.txt\";
/**
* Reads from the file and creates a list of artist and returns the list
*
* @return
* @throws FileNotFoundException
*/
public static ArtistList getArtistList () throws FileNotFoundException, NumberFormatException {
try {
// Scanner to read from the file
Scanner in = new Scanner(new File(FILE_NAME));
// Create ArtistList object
ArtistList list = new ArtistList();
// Read file
while (in.hasNextLine()) {
String data[] = in.nextLine().split(\" \");
try {
list.addNode(Integer.parseInt(data[0]), data[1]);
} catch (NumberFormatException nfe) {
throw new NumberFormatException(\"Error: Invalid input string: \" + data[0]);
}
}
// Close scanner
in.close();
return list;
} catch (FileNotFoundException fnfe) {
throw new FileNotFoundException(\"Error: File not found \" + FILE_NAME);
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class UpdateArtistList {
private static final String FILE_NAME = \"p2changes.txt\";
/**
* Reads from the file and creates a list of artist and returns the list
*
* @param list
* @return
* @throws FileNotFoundException
*/
public static ArtistList modifyArtistList(ArtistList list) throws FileNotFoundException, NumberFormatException {
try {
// Scanner to read from the file
Scanner in = new Scanner(new File(FILE_NAME));
// Read file
while (in.hasNextLine()) {
String data[] = in.nextLine().replaceAll(\" +\", \" \").split(\" \");
if (data[0].equalsIgnoreCase(\"A\")) {
// Add artist
list.add(data[1]);
} else if (data[0].equalsIgnoreCase(\"D\")) {
// Remove artist
try {
list.delete(Integer.parseInt(data[1]));
} catch (NumberFormatException nfe) {
throw new NumberFormatException(\"Error: Invalid input string: \" + data[0]);
}
}
}
// Close scanner
in.close();
return list;
} catch (FileNotFoundException fnfe) {
throw new FileNotFoundException(\"Error: File not found \" + FILE_NAME);
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class P3a {
public static void main(String[] args) {
try {
// Get list from file
ArtistList list = ReadArtistList.getArtistList();
// Display list
list.print();
// Modify list
System.out.println(\"Modifying list...\");
UpdateArtistList.modifyArtistList(list);
// Display list
list.print();
// Write modified list to a file
// PrintWriter to write to the file
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(\"p3artists.txt\"))));
pw.println(list);
// Close writer
pw.close();
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
} catch (NumberFormatException nfe) {
System.out.println(nfe.getMessage());
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
SAMPLE OUTPUT:
p3artists.txt
82 Zischke
81 Yasami
80 Xie
79 Wright
77 Werner
75 Van
74 Udinotti
73 Turrell
71 Swartz
70 Statom
69 Shenal
64 Roman
63 Rogers
61 Reed
60 Rath
59 Quiroz
58 Prinzen
57 Pierobon
56 Penn
78 Wittner
54 Ortega
53 Odiezma
52 Novarre
76 Waid
50 Mogan
49 Miller
48 Metz
47 Meglech
72 Tidwell
45 McGraw
44 Maglich
43 Lutes
42 Lundquist
68 Senior
40 Long
39 Lerman
38 Kritz
37 Kollasch
36 Kerrihard
35 Irvin
34 Ingraham
33 Indiana
32 Ibe
66 Scherzel
30 Higgins
29 Hamend
28 Guys
27 Gonzales
26 Gilhooly
25 Giama
24 Garin
23 Garber
22 Fratt
67 Scholder
20 Edwards
19 Dill
18 DiGrigoro
17 Dawson
65 Schenck
15 Cridler
14 Cox
13 Colvin
12 Chico
62 Rissman
10 Cardenas
9 Budd
8 Blum
7 Blain
6 Bindner
5 Battenberg
4 Baron
3 Aserty
2 Ames
1 Acconci










