Almost have Step 1 working but just need to modify to add key of artID and value
ID: 3828244 • Letter: A
Question
Almost have Step 1 working but just need to modify to add key of artID and value of artistID in TreeMap.
I. Step 1: Map is a very convenient structure for locating data. In this step, you are going to build a
map by reading "exam4Arts.txt" that use "artID" as the key and "artistID" as the value.
A. What to do:
1. Modify ArtMap.java to do the following:
- "map" of TreeMap that uses "artID" as key and "artistID" as its value.
2. Modify “Exam4Step1.java” to test ArtMap that you defined above by doing the following:
- Ask for an artID
- Print the corrosponding artistID.
3. The sample output follows:
Please enter an Art ID(0 to quit) ==> 1000
Artist not found!
Please enter an Art ID(0 to quit) ==> 1001
Artist = 9
Please enter an Art ID(0 to quit) ==> 1042
Artist = 2
Please enter an Art ID(0 to quit) ==> 0
Bye!
Now, add methods and required code to do the following. Keep the output from Step1 intact.
II. Step 2: In this step, you are going to construct a map that uses artistID as the key and its corresponding ArtistNode as the value
so that you may directly locate a particular ArtistNode based upon the entered artistID
A. What to do:
1. Rename your “MyArtistNodes.java” as “MyArtists.java” with the following modification:
1. Add a TreeMap named "map" that takes artistID as the key and ArtistNode as its value.
(Notes : In this constructor, you simply add a statement to construct the map when you construct the ArrayList of Artist)
2. Use "map" to locate a particular ArtistNode.
2. Write “Exam4Step2.java” to test "MyArtist.java" that you defined above by doing the following:
A. What to do:
1. Pass "exam4artists,txt" when making an instance of MyArtists.
2. Ask for an artistID
3. As long as the inputted artistID > 0, do the following:
i) Print the corresponding ArtistNode
ii) Ask for another artistID
a. The sample output follows:
Please enter an Artist ID(0 to quit) ==> 3
Artist = 3, Name = Carpenter, Total Artworks = 0, Total Value = 0.0
Please enter an Artist ID(0 to quit) ==> 5
Artist = 5, Name = Edwards, Total Artworks = 0, Total Value = 0.0
Please enter an Artist ID(0 to quit) ==> 12
Artist 12 not found!
Please enter an Artist ID(0 to quit) ==> 0
Bye!
Required Files:
exam4Arts.txt:
1038 Spring Flowers 1 800
1050 Cattle Ranch 1 10000
1103 Trail End 1 8000
1042 Coffee on the Trail 2 7544
1013 Superstitions 3 78000
1021 Bead Wall 3 14000
1034 Beaver Pole Jumble 3 28000
1063 Asleep in the Garden 3 110000
1070 Beginnings 4 27500
1036 Blackhawk 5 25500
1017 Brittlecone 6 1300
1053 Blue Eyed Indian 6 40000
1056 Cavalry Is Coming 6 1900
1075 Western Boots and Spurs 6 6000
1077 Bull Riding 6 5200
1049 Buttercup with Red Lip 7 400
1018 Mountain Scene 8 2500
1055 Starlit Evening 9 9500
1096 Ceremonial Sticks 10 15000
1037 Floating World 7 2350
1109 Friends 8 16000
1084 Crossing the Platt River 9 2200
1072 Funnel 10 4500
1032 Rising Sun 7 2000
1060 Story Sticks 8 650
1044 Mexican Fiesta 9 14000
1047 Medicine Man 10 2500
1024 Spirit and Nature 7 592
1067 Owl in Flight 8 7000
1001 Red Rock Mountain 9 18000
1028 Tired Cowboy 10 4700
1054 Snake Charmer 7 4500
1068 Moonlight 8 9750
1069 Renaissance 9 5500
1113 Shadow House 10 5500
1114 Storytelling at the Campfire 7 18000
1064 Spirit Columns 8 7000
1002 Offerings 9 10000
1089 Life Lessons 10 4125
1091 Stone Palette 7 11500
1074 Storm on the Rise 8 8000
1098 Sweet Project 9 592
1048 Comfy Chair 10 800
1101 The Red Door 2 10000
1080 The Dust Behind 3 18000
1058 The Gathering 3 250
1019 The White Heart 3 9300
1095 The Spirit 3 20000
1079 Carrying the Mail 4 8000
1093 Antelopes 5 12500
1110 Three Sisters 6 6500
1085 Traces 6 20000
1004 Seeking Shelter 6 52000
1016 Untitled 6 6000
1026 Untitled (couple) 7 4000
1057 Untitled 8 4500
1086 Untitled (desert landscape) 2 18000
1025 Profile of a Woman 3 625
1022 The Cowboy 3 4200
1104 Untitled 3 1800
1010 Untitled (land with adobe) 3 800
1111 Untitled (man and crucifix) 4 3200
1020 Untitled (Man holding coat) 5 3000
1012 Man on Horseback 6 8000
1097 Untitled (Sea) 6 2800
1066 Untitled (still life) 6 19500
1033 Untitled (Woman abstract) 6 2500
1061 Untitled Mural 7 3520
1071 Ride the Rapids 8 300
exam4Artists.txt:
1 Acconci
2 Budd
3 Carpenter
4 Dill
5 Edwards
6 Fleming
7 Garber
8 Higgins
9 Ibe
10 Kollasch
ArtMap.java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
public class ArtMap {
// map to store data
Map artistMap;
public ArtMap(String fileName) throws IOException {
// initialize the map
artistMap = new TreeMap<>();
// read data using reader
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
// while file has lines
while((line = reader.readLine()) != null) {
// split token using one or more spaces
String tokens[] = line.split(" ");
artistMap.put(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[2]));
}
// close reader
reader.close();
}
/*
* Method which returns name corresponding to id
*/
Integer getName(int id){
if(artistMap.containsKey(id))
return artistMap.get(id);
return null;
}
}
Exam4Step1.java:
import java.io.IOException;
import java.util.Scanner;
public class Exam4Step1 {
public static void main(String[] args) throws IOException {
ArtMap artistMap = new ArtMap("C:/Users/patel/Desktop/JavaFiles/exam4arts.txt");
Scanner scanner = new Scanner(System.in);
int id;
// ask the name for the first time
System.out.print("Please enter an Art ID(0 to quit) ==> ");
id = scanner.nextInt();
// keep looping till user enters a id of 0 or less
while(id > 0) {
// get the ID from map
Integer ID = artistMap.getName(id);
if(ID == null) {
System.out.println("Artist not found!");
//name = "Artist not found!";
}
// print the name
System.out.println("Artist = " + ID);
// ask again for next input id
System.out.print("Please enter an Art ID(0 to quit) ==> ");
id = scanner.nextInt();
}
/*while (id <= 0) {
System.out.println("Bye!");
}*/
scanner.close();
}
}
MyArtistNodes.java:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;
public class MyArtistNodes implements java.lang.Comparable{
public ArrayList myArtistNodes = new ArrayList();
public ArrayList myArtNodes = new ArrayList();
ListIterator lr = myArtistNodes.listIterator();
public MyArtistNodes(String filename) {
Scanner input;
try {
input = new Scanner(new File(filename));
myArtistNodes = new ArrayList();
while(input != null && input.hasNext()) {
String line = input.nextLine();
String[] tokens = line.split(" ");
// System.out.println(tokens.length+" "+tokens[1]+" -- "+Integer.parseInt(tokens[0].trim()));
ArtistNode newA = new ArtistNode(tokens[1],Integer.parseInt(tokens[0].trim()));
myArtistNodes.add(newA);
System.out.println(newA);
while (lr.hasPrevious()){
//System.out.println("Aashiv");
System.out.println(lr.previous());
}
}
input.close();
} catch (NumberFormatException | FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void print(String heading, String outFile) {
//write to file
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter(outFile));
writer.write(heading + " ");
writer.write(System.getProperty("line.separator"));
for(Artist a : myArtistNodes) {
//writer.write(newA);
writer.write(a.toString()+" ");
writer.write(System.getProperty("line.separator"));
}
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int compareTo(ArrayList o1, ArrayList o2) {
o2.get(0);
o1.get(0);
int compareValue = (Artist.getArtistID() - Artist.getArtistID());
return compareValue;
}
/**
* Finds the artist with id and returns the array index
*/
public static int getArtist(ArrayList[] artistArtArray, int artistId) {
for (int i = 0; i < artistArtArray.length; i++) {
artistArtArray[i].get(0);
if (Artist.getArtistID() == artistId)
return i;
}
return -1;
}
/*
// write to console
public void printList() {
for(Artist a : myArtistNodes) {
System.out.println(a.toString());
}
}
// write to console
public void printList() {
for(Art a : myArtNodes) {
System.out.println(a.toString());
}
}
public void append()
{
}
/*public int indexOfArtistID(int artistID)
{
int index;
return index;
}*/
@Override
public int compareTo(Artist o) {
// TODO Auto-generated method stub
return 0;
}
}
Artist.java:
public class Artist implements java.lang.Comparable<Artist> {
// Instance variables
private int artistID; // id of artist
private String artistName; // name of artist
private Art art;
/**
* Constructor
*
* @param name
* @param number
*/
Artist(String name, int number) {
this.artistID = number;
this.artistName = name;
}
/**
* Constructor
*
* @param artistID
* @param artistName
* @param art
*/
public Artist(int artistID, String artistName, Art art) {
this.artistID = artistID;
this.artistName = artistName;
this.art = art;
}
/**
* 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;
}
/**
* @return the art
*/
public Art getArt() {
return art;
}
/**
* @param art
* the art to set
*/
public void setArt(Art art) {
this.art = art;
}
public int compareTo(Artist o) {
return this.getArt().compareTo(o.getArt());
}
/**
* toString method
*/
public String toString() {
return String.format("%-9d %-20s", this.artistID, this.artistName);
}
}
ArtistNode.java: (Modify to remove errors)
public class ArtistNode extends Artist
{
private ArtNode firstNode;
private ArtNode lastNode;
int totalEntries;
double totalValue;
public ArtistNode(String artistName, int artistID)
{
super(artistName,artistID);
this.firstNode = null;
this.lastNode = null;
totalEntries = 0;
totalValue = 0;
}
public int getTotalEntries() {
return totalEntries;
}
/**
* return the artistId
*
* @return
*/
public double getTotalValue() {
return totalValue;
}
public int compareTo(Artist o) {
return this.getArt().compareTo(o.getArt());
}
/**
* toString method
*/
public String toString() {
return String.format(Artist.getArtistName() + " " + ArtistNode.getArtistID() + " , %-9d , %-20s", this.totalEntries, this.totalValue);
}
}
Art.java:
public class Art implements java.lang.Comparable<Art> {
// Instance variables
private int artID;
private String artTitle;
private int appraisedValue;
/**
* Constructor
*
* @param artID
* - art id
* @param artTitle
* - title of the art
* @param appraisedValue
* - value
*
public Art(int artID, String artTitle, int appraisedValue) {
this.artID = artID;
this.artTitle = artTitle;
this.appraisedValue = appraisedValue;
}
/**
* @return the artID
*
public int getArtID() {
return artID;
}
/**
* @return the artTitle
*
public String getArtTitle() {
return artTitle;
}
/**
* @return the appraisedValue
*
public int getAppraisedValue() {
return appraisedValue;
}
/**
* @param artID
* the artID to set
*
public void setArtID(int artID) {
this.artID = artID;
}
/**
* @param artTitle
* the artTitle to set
*
public void setArtTitle(String artTitle) {
this.artTitle = artTitle;
}
/**
* @param appraisedValue
* the appraisedValue to set
*
public void setAppraisedValue(int appraisedValue) {
this.appraisedValue = appraisedValue;
}
public int compareTo(Art o) {
return (this.getArtID() - o.getArtID());
}
@Override
public String toString() {
return String.format("%-6d %-25s %d", artID, artTitle, appraisedValue);
}
}
Explanation / Answer
Defined in the art class
public void modifyArtID(int new_artID )
{
this.artID = new_artID;
}
Defined in the Artist class
public void modifyArtistID(int new_artistID )
{
this.artistID = new_artistID;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.