in java p2changes.txt”. The file, tab delimited, contains two types of record an
ID: 3782529 • Letter: I
Question
in java p2changes.txt”. The file, tab delimited, contains two types of
record and each record contains two fields as following:
Action ID or Name
A or a Artist Name
D or d Artist ID
Where ‘A’ means “Add” and ‘D’ means “Delete”.
II. Details:
1) Description:
To help you understand why we care how data is structured, we are going to use 3 different
ways to maintain data while it is being changed.
Assume that we start with the following records (Table-1):
ArtistID Artist Name
1 Acconci
2 Ames
3 Aserty
4 Baron
5 Battenberg
Later on, the file needs to be updated according to the following (Table-2):
Action ArtistID Artist Name
A 6 Bindner
A 7 Blain
D 2
A 8 Blum
D 4
A 9 Budd
D 8
(NOTE: For Add, the Artist ID is assigned by the program. In this case, we simply use the
number continuous from the last ID number. The number that has been deleted is not
reassigned.)
Using Array structure the 3 different approaches will end up with the following:
(1. Without Gap)
ArtistID Artist Name
1 Acconci
3 Aserty
5 Battenberg
6 Bindner
7 Blain
9 Budd
(2. Use Delete Field)
ArtistID Artist Name Delete
1 Acconci F
2 Ames T
3 Aserty F
4 Baron T
5 Battenberg F
6 Bindner F
7 Blain F
8 Blum T
9 Budd F
(3. Use Next Field)
ArtistID Artist Name Next
1 Acconci 3
2 Ames 0
3 Aserty 5
4 Baron 0
5 Battenberg 6
6 Bindner 7
7 Blain 9
8 Blum 0
9 Budd -1
2) Assignment:
i. Use Excel to trace every change that Table-2 makes to Tble-1. Name this file
p2transition (Your name).xlsx.
ii. Write a program that uses the 3 different approaches mentioned above to
produce the updated version of “p1artists.txt” through the use of
“p2changes.txt”. Let’s call the output files “p2artists2a.txt”, “p2artists2b.txt”,
and “p2artistis2c.txt”. Compare the files and they should be same.
iii. Use System.nanoTime() as following to find the time spent on each approach:
long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();
long duration = (endTime - startTime);
You are encouraged to try System.currentTimeMillis() to see the difference.
iv. Write a summary to compare the 3 different approaches.
here is p2changes.txt file
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
Explanation / Answer
Answer:
1. Excel file:
Gaps between records refers to deleted records.
Program:
package artist;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* ArtistProcessing class
* As per approach 1
*/
public class ArtistProcessing {
//Artist class
static class Artist
{
private int artistID; //id of artist
private String artistName; //name if artist
public Artist(int artistID, String artistName) {
this.artistID = artistID;
this.artistName = artistName;
}
public int getArtistID() {
return artistID;
}
public void setArtistID(int artistID) {
this.artistID = artistID;
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
}
/**
* @param args the command line arguments
* main function
*/
public static void main(String[] args) {
ArrayList<Artist> artistList=new <Artist>ArrayList();
String artistFileName="./src/artist/p1artists.txt"; //change as per your path
BufferedReader bufferedReader;
//read artist data
try {
bufferedReader=new BufferedReader(new FileReader(new File(artistFileName)));
String line;
line = null;
while((line=bufferedReader.readLine())!=null)
{
StringTokenizer tokenizer=new StringTokenizer(line);
String token=null;
int ID = 0;
String name = null;
while(tokenizer.hasMoreTokens())
{
token=tokenizer.nextToken();
if(token.matches("\d+"))
ID=Integer.parseInt(token);
else
name=token;
}
Artist artist;
artist = new Artist(ID, name);
artistList.add(artist);
}
bufferedReader.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(ArtistProcessing.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ArtistProcessing.class.getName()).log(Level.SEVERE, null, ex);
}
//process changes
String changesFileName="./src/artist/p1changes.txt"; //changes as per your path
try {
bufferedReader=new BufferedReader(new FileReader(new File(changesFileName)));
String line;
line = null;
while((line=bufferedReader.readLine())!=null)
{
StringTokenizer tokenizer=new StringTokenizer(line);
String token;
token = null;
char action = 0;
int ID = 0;
String name = null;
while(tokenizer.hasMoreTokens())
{
token=tokenizer.nextToken();
if(token.charAt(0)=='A' || token.charAt(0)=='D')
action=token.charAt(0);
else
{
if(action=='A')
{
if(token.matches("\d+"))
ID=Integer.parseInt(token);
else
name=token;
if(name!=null)
{
Artist artist;
artist = new Artist(ID, name);
artistList.add(artist);
}
}
else if(action=='D')
{
if(token.matches("\d+"))
ID=Integer.parseInt(token);
System.out.println("ÏD to be deleted:"+ID);
for(Artist artist:artistList)
{
if(artist.getArtistID()==ID)
{
artistList.remove(artist);
break;
}
}
}
}
}
}
bufferedReader.close();
}
catch (FileNotFoundException ex) {
Logger.getLogger(ArtistProcessing.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ArtistProcessing.class.getName()).log(Level.SEVERE, null, ex);
}
//write modified artist list to file
String outputFileName="./src/artist/p2artist2a.txt"; //change as per your path
try {
FileWriter fileWriter;
fileWriter = new FileWriter(new File(outputFileName));
for (Artist artist : artistList) {
String line=artist.getArtistID()+" "+artist.getArtistName()+" ";
fileWriter.write(line);
}
fileWriter.close();
} catch (IOException ex) {
Logger.getLogger(ArtistProcessing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Input files:
1. p1artist.txt:
1 Acconci
2 Ames
3 Aserty
4 Baron
5 Battenberg
2. p1changes.txt:
A 6 Bindner
A 7 Blain
D 2
A 8 Blum
D 4
A 9 Budd
D 8
Output file:
1. p2artist2a.txt:
1 Acconci
3 Aserty
5 Battenberg
6 Bindner
7 Blain
9 Budd
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.