In this project, we combine the concepts of Recursion and Sorting. Since the Java Collection API
provides various built-in sorting capabilities, so we will focus on Merge Sort and have it applied
to a file.
a) The input file that you are going to use is the tab-delimited text file \"p1arts.txt\".
b) the output file that you are going to produce using File I/O is also the tabdelimited text file
called \"p6sortedArts.txt\" which is sorted ascendingly on artistID and then artID both.
Example follows:
(sample output just for artistID) (have to sort both, ArtistID and then ArtID):
ArtistID ArtID 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
Programming Steps:
1) Create a class called Art that implements Comparable interface.
2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file.
3) Read some more records from the file, sort them, and merge them with the sorted file on the
disk.
4) Repeat the above step until it is all done.
p1arts.txt:
I am providing the sample programs that you might need:
MergeSort.java:
ArraySorter.java:
Name.java:
Artist.java:
Driver.java:
Solution
//i added 2 classes for sorting
public class Record implements Comparable {
public int artId;
public String title;
public int artistId;
public int AppraisedValue;
/**
* @param artId
* @param title
* @param artistId
* @param appraisedValue
*/
public Record(int artId, String title, int artistId, int appraisedValue) {
this.artId = artId;
this.title = title;
this.artistId = artistId;
this.AppraisedValue = appraisedValue;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + AppraisedValue;
result = prime * result + artId;
result = prime * result + artistId;
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Record other = (Record) obj;
if (AppraisedValue != other.AppraisedValue)
return false;
if (artId != other.artId)
return false;
if (artistId != other.artistId)
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return artistId + \" \" + artId + \" \" + title + \" \" + AppraisedValue;
}
@Override
public int compareTo(Record record) {
// to sort ascending order
return this.artistId - record.artistId;
}
}
***********************************************************************
import java.io.BufferedReader.
In this project, we combine the concepts of Recursion and Sorting. S.pdf
1. In this project, we combine the concepts of Recursion and Sorting. Since the Java Collection API
provides various built-in sorting capabilities, so we will focus on Merge Sort and have it applied
to a file.
a) The input file that you are going to use is the tab-delimited text file "p1arts.txt".
b) the output file that you are going to produce using File I/O is also the tabdelimited text file
called "p6sortedArts.txt" which is sorted ascendingly on artistID and then artID both.
Example follows:
(sample output just for artistID) (have to sort both, ArtistID and then ArtID):
ArtistID ArtID 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
Programming Steps:
1) Create a class called Art that implements Comparable interface.
2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file.
3) Read some more records from the file, sort them, and merge them with the sorted file on the
disk.
4) Repeat the above step until it is all done.
p1arts.txt:
I am providing the sample programs that you might need:
MergeSort.java:
ArraySorter.java:
Name.java:
Artist.java:
Driver.java:
Solution
//i added 2 classes for sorting
public class Record implements Comparable {
2. public int artId;
public String title;
public int artistId;
public int AppraisedValue;
/**
* @param artId
* @param title
* @param artistId
* @param appraisedValue
*/
public Record(int artId, String title, int artistId, int appraisedValue) {
this.artId = artId;
this.title = title;
this.artistId = artistId;
this.AppraisedValue = appraisedValue;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + AppraisedValue;
result = prime * result + artId;
result = prime * result + artistId;
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
3. @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Record other = (Record) obj;
if (AppraisedValue != other.AppraisedValue)
return false;
if (artId != other.artId)
return false;
if (artistId != other.artistId)
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return artistId + " " + artId + " " + title + " " + AppraisedValue;
}
@Override
public int compareTo(Record record) {
// to sort ascending order
return this.artistId - record.artistId;
}
4. }
***********************************************************************
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Art {
public static void main(String[] args) throws IOException {
List records = new ArrayList();
BufferedReader reader = new BufferedReader(new FileReader(new File(
"D:p6sortedArts.txt")));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
String[] words = line.split("s+");
int artId = Integer.parseInt(words[0]);
String title = words[1];
int artistId = Integer.parseInt(words[2]);
int appraisedValue = Integer.parseInt(words[3]);
Record record = new Record(artId, title, artistId, appraisedValue);
System.out.println(record);
records.add(record);
}
System.out
.println("before sorting using collections by using artist id");
for (Record record : records) {
System.out.println(record);
}
System.out
.println("*********************************************************************
");
5. System.out.println("after sorting based on artist id");
// after sorting
// collections uses internally merge sort
Collections.sort(records);
for (Record record : records) {
System.out.println(record);
}
System.out
.println("*******************************************************************");
// sorting using arrays based on artist id
// it uses quicksort internally
System.out.println("sorting using arrays class sort method");
Record[] array = records.toArray(new Record[records.size()]);
Arrays.sort(array);
for (int j = 0; j < array.length; j++) {
System.out.println(array[j]);
}
reader.close();
}
}
*****************************************************************************
***********************
given text file (p6artists.txt)
1001 RedRockMountain 50 18000
1002 Offerings 52 10000
1003 SpringFlowers 12 2400
1004 SeekingShelter 64 52000
1005 TheHang 18 8000
1006 HouseRemembered 32 700
1007 HomagetotheAncestors 82 1200
1008 EndofthePath 26 1900
1009 Amen 28 3000
1010 Untitled(landwithadobe) 71 800
1011 Eve 19 975
1012 ManonHorseback 74 8000