Programming Steps: (Please screen shot your output while answering the question)
ID: 3813875 • Letter: P
Question
Programming Steps: (Please screen shot your output while answering the question)
I am attaching ArtistNode.java and ArtNode.java and myArtistNodes.java (whatever I have) below so that you can modify as needed.
Before doing all this, I need to print the Artists in reverse order but mine would only print the last Artist. Need to modify myArtistNodes.java to print all the artists in Reverse order. Therefore, please try to do that first.
Now, The main part:
A. Files required:
1. Artist.java
2. Art.java
3. p7artists.java and p7artists.txt (Input files to read from)
4. out3B.txt (Output file to write to)
B. Java programs needed to write and/or modify:
1. MyArtistNodes.java: (Modify this class to add these methods)
- Contains:
1. "myArtistNodes" :
- public, ArrayList of ArtistNode.
2. A constructor that accepts one string parameter which is the input file name.This constructor will read the input file into the ArrayList "myArtistNodes".
3. Two print() methods:
- One takes 2 parameters: A heading and the output File ID. Example : (String Name, String outFile)
This first print method prints the contents of "file" in REVERSE order to the output file with the heading the first line.
- Implement Comparable to print the list in REVERSE order.
- SEcond one prints the list content to the console. (No Parameter)
Add or modify methods for following:
4. "indexOfArtistID":
- Accepts "artistID" and returnd its index so that you will be able to add an instance of ArtNode to the artist it belongs.
- Also, add some kind of Error handling if "artistID" is not found.
4. "printList"
- To print all instances of ArtNode
5. "append"
- To add an instance of ArtNode to the list to print the following sample output.
2. MyArtCollection.java:
- Contains:
1. "artists"
- An instance of MyArtistNodes that reads input file "p7artists.txt" into the list.
2. Constructor:
- Accepts the input file Name ("p7arts.txt"), so that the entire entries from the file will be constructed.
3. Two print() methods:
- (Like the one above) One will print to the output file.
- Other to print to the console (Example code follows)
Sample code for that:
public void print()
{
for (int i = 0; i < (artists.list).size(); i++){
System.out.println((artists.list).get(i));
((artists.list).get(i)).printList();
}
}
5. Step3B.java:
public class Step3B
{
public Step3B()
{
MyArtCollection myCollection = new MyArtCollection("p7arts.txt");
myCollection.print("Name", "out3B.txt");
}
}
6. Main.java: That calls the Step3B to inplement the classes to produce the output file to produce the following output.
Sample output (Not complete) :
13 Novarre 2 8650
1081 Coming Under Fire 13 650
1112 Dark Canyon 13 8000
12 Metz 2 4300
1003 Spring Flowers 12 2400
1008 End of the Patch 12 1900
Required Programs and Text files:
p7artists.txt:
1 Acconci
2 Budd
3 Carpenter
4 Dill
5 Edwards
6 Fleming
7 Garber
8 Higgins
9 Ibe
10 Kollasch
11 Lerman
12 Metz
13 Novarre
14 Ortega
15 Parker
16 Penn
17 Pierobon
18 Prinzen
19 Quiroz
20 Rath
p7arts.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
1090 Off the Grid 11 8000
1003 Spring Flowers 12 2400
1081 Coming Under Fire 13 650
1039 Treachery 14 20000
1102 Crying Hats 14 10000
1073 Dancing in the Light 15 4000
1052 American Rodeo 16 3500
1059 Dwelling 17 16000
1005 The Hang 18 8000
1011 Eve 19 975
1099 Watch That Rattler 20 900
1037 Floating World 7 2350
1109 Friends 8 16000
1084 Crossing the Platt River 9 2200
1072 Funnel 10 4500
1115 Starry Night 11 8500
1008 End of the Path 12 1900
1112 Dark Canyon 13 8000
1009 Amen 14 3000
1030 Ash Bench 14 13000
1043 Creosote Bushes 15 18000
1078 Chuckwagon 16 32000
1041 Night Version 17 3800
1082 Spring Flowers 18 20000
1116 Apache Warrior 19 23000
1029 Horseshoe Falls 20 15000
1006 House Remembered 14 700
1046 Immediate Gratification 14 1500
1031 Inside/Out 15 3500
1107 Striking It Rich 16 1750
1051 Night Version 17 7000
1088 Lessons 18 3700
1045 Leaf Patterns 19 2100
1100 Hungry Cowboys 20 750
1094 Life Is Sweet 18 25000
1106 Horse Corral 19 12500
1062 Cowboy and Saddle 20 18000
1032 Rising Sun 7 2000
1060 Story Sticks 8 650
1044 Mexican Fiesta 9 14000
1047 Medicine Man 10 2500
1014 Plenty 14 500
1015 Punch 14 10000
1023 Shooting the Rapids 15 1300
1027 Mountain Climber 16 4700
1035 Nature/Nurture 17 1300
1040 Night on the Praire 18 1300
1065 Moonlite 19 1300
1092 Dressing Up 20 1300
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
1083 Untitled 6 2500
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
1108 Untitled Mural 6 400
1061 Untitled Mural 7 3520
1071 Ride the Rapids 8 300
1076 Ride the Bronco 14 1500
1105 Meteor Show 15 10000
1087 Three Woman 16 20000
1007 Homage to the Ancestors 17 1200
Artist.java:
public class Artist implements java.lang.Comparable {
// Instance variables
private static int artistID; // id of artist
private static 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 static String getArtistName() {
return artistName;
}
/**
* set the artistName
*
* @param artistName
*/
public void setArtistName(String artistName) {
this.artistName = artistName;
}
/**
* return the artistId
*
* @return
*/
public static 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);
}
}
Art.java:
public class Art implements java.lang.Comparable {
// 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);
}
}
ArtNode.java:
public class ArtNode extends Art implements java.lang.Comparable<ArtNode> {
private ArtNode next;
private ArtNode previous;
private int artistID;
public ArtNode(int artID, String artTitle, int artistID, int appraisedValue) {
super(artID, artTitle, artistID, appraisedValue);
this.artID = artID;
this.artTitle = artTitle;
this.artistID = artID;
this.appraisedValue = appraisedValue;
// TODO Auto-generated constructor stub
}
public void setNext(ArtNode n) {
this.next = n;
}
public ArtNode getNext() {
return this.next;
}
public void setData(ArtNode p) {
this.previous = p;
}
public ArtNode getData() {
return this.previous;
}
public String toString()
{
return String.format("%5d %-15s %5d %10.2f", artID, artTitle, artistID, appraisedValue);
}
public int compareTo(ArtNode o) {
return (this.getArtID() - o.getArtID());
}
}
ArtistNode.java:
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);
}
}
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.Scanner;
public class MyArtistNodes {
public ArrayList<ArtistNode> myArtistNodes = new ArrayList<ArtistNode>();
public ArrayList<ArtNode> myArtNodes = new ArrayList<ArtNode>();
public MyArtistNodes(String filename) {
Scanner input;
try {
input = new Scanner(new File(filename));
myArtistNodes = new ArrayList<ArtistNode>();
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);
}
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 int indexOfArtistID(int artistID)
{
for (int i = 0; i < Art.size(); i++) {
Student student = studentsCourses.get(i);
System.out.println(student.getStudentNumber());
System.out.println(student.getCourse());
System.out.println(student.getMarkr());
}
return artistID;
}
// write to console
public void print() {
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;
}*/
}
Explanation / Answer
private static category BinOpNode extends ExpNode quantity.
ExpNode right; // The expression for its right quantity.
BinOpNode(char op, ExpNode left, ExpNode right) the required information.
assert op == '+' || op == '-' || op == '*' || op == '/';
assert left != null && right != null;
this.op = op;
this.left = left;
this.right = right;
}
double value() the worth is obtained by evaluating the left and right
// operands and mixing the values with the operator.
double x = left.value();
double y = right.value();
switch (op) come x + y;
case '-':
come back x - y;
case '*':
come back x * y;
case '/':
come back x / y;
default:
come back Double.NaN; // dangerous operator! mustn't be potential.
}
}
void printStackCommands() to guage the expression on a stack machine, first do
// no matter is important to guage the left quantity, leaving
// the solution on the stack. Then do constant issue for the
// second quantity. Then apply the operator (which suggests that sound
// the operands, applying the operator, and pushing the result).
left.printStackCommands();
right.printStackCommands();
System.out.println(" Operator " + op);
}
}
It's also attention-grabbing to appear at the new parsing subroutines. rather than computing a worth, every subprogram builds AN expression tree. as an example, the subprogram like the rule for <expression> becomes
static ExpNode expressionTree() throws ParseError browse AN expression from the present line of input and
// come back AN expression tree representing the expression.
TextIO.skipBlanks();
mathematician negative; // True if there's a number one sign.
negative = false;
if (TextIO.peek() == '-')
ExpNode exp; // The expression tree for the expression.
exp = termTree(); // begin with a tree for 1st term.
if (negative) single minus operator to the term we've
// simply scan.
exp = new UnaryMinusNode(exp);
}
TextIO.skipBlanks();
whereas ( TextIO.peek() == '+' || TextIO.peek() == '-' ) {
// scan successive term and mix it with the
// previous terms into an even bigger expression tree.
char op = TextIO.getAnyChar();
ExpNode nextTerm = termTree();
// produce a tree that applies the binary operator
// to the previous tree and also the term we tend to simply scan.
exp = new BinOpNode(op, exp, nextTerm);
TextIO.skipBlanks();
}
come back exp;
} // finish expressionTree()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.