Introduction: In this project, we use the same file structure of Artist and Art
ID: 3714901 • Letter: I
Question
Introduction:
In this project, we use the same file structure of Artist and Art to extend the concepts of array and linked list and have them applied to sorting.The input file of p1arts.txt has been slightly modified and it is named p7arts.txt.
This project covers the materials up to chapter 18 of your textbook.
Description:
As you have learned so far that array is a very efficient way of handling a collection of related data, but the problem is that the size needs to be predetermined and storage needs to be reserved.However, in many real world applications, the amount of data is hard to predict. So, we will need to employ dynamic memory allocation approach, which means we allocate storage only when needed.
Assignments:
Implement a doubly linked list class so that you can use it to arrange the input file to produce the output similar to the following (Name this file p7out(yourName).txt:
Artistist ID
Art ID
Art Title
Appraised Value
1
1038
Spring Flowers
800
1
1050
Cattle Ranch
10000
1
1103
Trail End
8000
2
1042
Coffee on the Trail
7544
3
1013
Superstitions
78000
3
1021
Bead Wall
14000
3
1034
Beaver Pole Jumble
28000
3
1063
Asleep in the Garden
110000
You will need to have your class implement Comparable interface.
Design and implement your doubly linked list so that you will be able to place data in ascending order. There is plenty of information available online. For example, the following segment of code from the web should give you a good starting point
public class DoublyLinkedListImpl<E> {
private Node head;
private Node tail;
private int size;
public DoublyLinkedListImpl(){
size = 0;
}
/**
* this class keeps track of each element information
* @author java2novice
*/
private class Node {
E element;
Node next;
Node prev;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
}
Hints:
This project is similar to what you did for project 3. The only differences are: (1) you need to have data arranged in order and (2) you need to use a doubly linked list.
What methods shall you include in your class? Be creative. You may just implement a minimal set of operations that enable you to sort the list in a convenient way.
Artistist ID
Art ID
Art Title
Appraised Value
1
1038
Spring Flowers
800
1
1050
Cattle Ranch
10000
1
1103
Trail End
8000
2
1042
Coffee on the Trail
7544
3
1013
Superstitions
78000
3
1021
Bead Wall
14000
3
1034
Beaver Pole Jumble
28000
3
1063
Asleep in the Garden
110000
Explanation / Answer
import java.util.Iterator;
public class DoublyLinkedListImpl<E extends Comparable<E>> {
class Node {
// Instance variables
private E element;
private Node prev;
private Node next;
/**
* Constructor
*
* @param element
* - element
*/
public Node(E element) {
this.element = element;
this.prev = null;
this.next = null;
}
}
// Instance variable for LinkedList
private Node head;
private Node tail;
private int size;
/**
* Constructor
*/
public DoublyLinkedListImpl() {
this.head = null;
this.tail = null;
this.size = 0;
}
/**
* Adds a new node to the list
*/
public void add(E element) {
// Create new node
Node newNode = new Node(element);
// Check if list is null
if (this.size == 0) {
// Set newNode as the head
this.head = newNode;
// Set tail as the new head
this.tail = newNode;
} else {
// If list is not null
boolean added = false;
// Get the head node
Node node = this.head;
while (!added && (node != null)) {
// Compare element with element at node
int res = element.compareTo(node.element);
if (res <= 0) { // If this is less than or equal
// to the element at node
// Check if node is the head node
if (node.prev == null) {
// Set new node as head
this.head = newNode;
} else {
// Set newNode.prev as node.prev
newNode.prev = node.prev;
// Set node.prev.next as newNode
node.prev.next = newNode;
}
// Set node.prev as newNode
node.prev = newNode;
// Set newNode.next as node
newNode.next = node;
added = true;
}
// Go to next node
node = node.next;
}
// Check if node was not added
if (!added) {
// Set tail.next as newNode
this.tail.next = newNode;
// Set newNode.prev as tail
newNode.prev = this.tail;
// Set newNode as tail
this.tail = newNode;
}
}
// Increment count
this.size += 1;
}
/**
* Returns an iterator for the list
*/
public Iterator<E> iterator() {
if (this.size == 0)
return null;
else
return new MyIterator();
}
// Iterator for list
class MyIterator implements Iterator<E> {
private Node currNode;
/**
* Constructor
*/
public MyIterator() {
this.currNode = head;
}
@Override
public boolean hasNext() {
return (this.currNode != null);
}
@Override
public E next() {
// Get element at currNode
E obj = this.currNode.element;
// Move currNode to next node
this.currNode = this.currNode.next;
return obj;
}
}
}
//Art.java
public class Art implements Comparable<Art> {
// Instance variables
private int artID;
private String title;
private int artistID;
private int appraisedVal;
/**
* Constructor
*
* @param artID
* - art id
* @param title
* - art title
* @param artistID
* - artist id of this art
* @param appraisedVal
* - appraised value of this art
*/
public Art(int artID, String title, int artistID, int appraisedVal) {
this.artID = artID;
this.title = title;
this.artistID = artistID;
this.appraisedVal = appraisedVal;
}
/**
* @return the artID
*/
public int getArtID() {
return artID;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @return the artistID
*/
public int getArtistID() {
return artistID;
}
/**
* @return the appraisedVal
*/
public int getAppraisedVal() {
return appraisedVal;
}
@Override
public int compareTo(Art a2) {
return (this.artID - a2.artID);
}
}
//Artist.java
public class Artist {
// Instance variables
private int artistID;
private String name;
private DoublyLinkedListImpl<Art> artList;
/**
* Constructor
*
* @param artistID
* - artist id
* @param name
* - artist name
*/
public Artist(int artistID, String name) {
this.artistID = artistID;
this.name = name;
this.artList = new DoublyLinkedListImpl<Art>();
}
/**
* @return the artistID
*/
public int getArtistID() {
return artistID;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* Adds an art to the list.
*/
public void addArt(int artID, String title, int artistID, int appraisedVal) {
artList.add(new Art(artID, title, artistID, appraisedVal));
}
/**
* @return the artList
*/
public DoublyLinkedListImpl<Art> getArtList() {
return artList;
}
}
//ArtistArtDemo.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;
public class ArtistArtDemo {
private static final String ART_FILE = "p7arts.txt";
private static final String ARTIST_FILE = "p7artists.txt";
static final String OUT_FILE = "p7outYOURNAME.txt";
private static final String DELIMITER = "\s+";
/**
* Returns an of artists from the file
*
* @throws FileNotFoundException
*/
private static Artist[] getArtist() throws FileNotFoundException {
Artist[] artist = new Artist[0];
// Scanner to read form the file
Scanner inFile = new Scanner(new File(ARTIST_FILE));
// Read from the file
while (inFile.hasNextLine()) {
String[] data = inFile.nextLine().split(DELIMITER);
// Get length of array
int len = artist.length;
// Expand array to accommodate an artist
artist = Arrays.copyOf(artist, len + 1);
// Add artist to array
artist[len] = new Artist(Integer.parseInt(data[0]), data[1]);
}
// Close scanner
inFile.close();
return artist;
}
/**
* This is a helper method for getArtist
*/
private static int getArtist(Artist[] artist, int artistId, int start,
int end) {
// Check if start <= end
if (start > end)
return -1;
int mid = (start + end) / 2;
// Compare artistId with artistId at mid
int compareVal = artistId - artist[mid].getArtistID();
if (compareVal == 0)
return mid;
else {
if (compareVal < 0)
return getArtist(artist, artistId, start, mid - 1);
else
return getArtist(artist, artistId, mid + 1, end);
}
}
/**
* Returns the index of artist with id artistId if found, else returns -1
*/
private static int getArtist(Artist[] artist, int artistId) {
return getArtist(artist, artistId, 0, artist.length - 1);
}
/**
* Returns the artist array with their respective arts from the file
*
* @throws FileNotFoundException
*/
private static void getArtistArtList(Artist[] artist)
throws FileNotFoundException {
// Scanner to read form the file
Scanner inFile = new Scanner(new File(ART_FILE));
// Read from the file
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();
// Find the artist with artistId in the array
int index = getArtist(artist, artistId);
if (index != -1) {
// Add art to artist
artist[index].addArt(artID, artTitle.toString(), artistId,
appraisedValue);
}
// Clear next line character
if (inFile.hasNextLine())
inFile.nextLine();
}
// Close scanner
inFile.close();
}
/**
* Writes the artist-art list to the file
*
* @throws IOException
*/
private static void writeToFile(Artist[] artist) throws IOException {
// Writer to write to the file
PrintWriter pw = new PrintWriter(new FileWriter(OUT_FILE));
// Write header to the file
pw.println(String.format("%-9s %-20s %-6s %-29s %s", "Artist ID",
"Artist Name", "Art ID", "Art Title", "Appraised Value"));
// Write artist
for (int i = 0; i < artist.length; i++) {
// Get artist
Artist a = artist[i];
StringBuffer sb = new StringBuffer();
// Add artist details
sb.append(String.format("%3s %-5d %-20s ", "", a.getArtistID(),
a.getName()));
// Get arts
Iterator<Art> it = a.getArtList().iterator();
// Get and add art
boolean first = true;
while (it.hasNext()) {
// Get art
Art art = it.next();
// Add art
if (first) {
sb.append(String.format("%6d %-29s %16d", art.getArtID(),
art.getTitle(), art.getAppraisedVal()));
first = false;
} else
sb.append(String.format(" %30s %6d %-29s %16d", "",
art.getArtID(), art.getTitle(),
art.getAppraisedVal()));
}
// Write sb to writer
pw.println(sb.toString());
}
// Close writer
pw.close();
}
public static void main(String[] args) {
try {
// Get array of artists from file
Artist[] artist = getArtist();
// Get arts from file
getArtistArtList(artist);
// Write to file
writeToFile(artist);
System.out.println("Output written to " + OUT_FILE);
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.