arts.txt 1038+Spring Flowers*1$800.99 1050+Cattle Ranch*1$10000.99 1103+Trail En
ID: 3600342 • Letter: A
Question
arts.txt 1038+Spring Flowers*1$800.99 1050+Cattle Ranch*1$10000.99 1103+Trail End*1$8000.50 1042+Coffee on the Trail*2$7544.50 1013+Superstitions*3$78000.40 1021+Bead Wall*3$14000.00 1034+Beaver Pole Jumble*3$28000.00 1063+Asleep in the Garden*3$110000 1070+Beginnings*4$27500.00 1036+Blackhawk*5$25500.00artists.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 arts.txt 1038+Spring Flowers*1$800.99 1050+Cattle Ranch*1$10000.99 1103+Trail End*1$8000.50 1042+Coffee on the Trail*2$7544.50 1013+Superstitions*3$78000.40 1021+Bead Wall*3$14000.00 1034+Beaver Pole Jumble*3$28000.00 1063+Asleep in the Garden*3$110000 1070+Beginnings*4$27500.00 1036+Blackhawk*5$25500.00
artists.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 --------------------------- MyStack.java
import java.util.Arrays; import java.util.EmptyStackException;
public final class MyStack<T> { private T[] stack; private int topIndex; private boolean initialized = false; private static final int DEFAULT_CAPACITY = 20; private static final int MAX_CAPACITY = 5000; public MyStack() { this(DEFAULT_CAPACITY); } // end default constructor public MyStack(int initialCapacity) { checkCapacity(initialCapacity);
@SuppressWarnings("unchecked") T[] tempStack = (T[])new Object[initialCapacity]; stack = tempStack; topIndex = -1; initialized = true; } public void push(T newEntry) { checkInitialization(); ensureCapacity(); stack[topIndex + 1] = newEntry; topIndex++; }
public T peek() { checkInitialization(); if (isEmpty()) throw new EmptyStackException(); else return stack[topIndex]; }
public T pop() { checkInitialization(); if (isEmpty()) throw new EmptyStackException(); else { T top = stack[topIndex]; stack[topIndex] = null; topIndex--; return top; } }
public boolean isEmpty() { return topIndex < 0; } public void clear() { checkInitialization();
while (topIndex > -1) { stack[topIndex] = null; topIndex--; }
}
private void checkInitialization() { if (!initialized) throw new SecurityException ("ArrayStack object is not initialized properly."); }
private void checkCapacity(int capacity) { if (capacity > MAX_CAPACITY) throw new IllegalStateException("Attempt to create a stack " + "whose capacity exceeds " + "allowed maximum."); }
private void ensureCapacity() { if (topIndex >= stack.length - 1) { int newLength = 2 * stack.length; checkCapacity(newLength); stack = Arrays.copyOf(stack, newLength); } } } import java.util.Arrays; import java.util.EmptyStackException;
public final class MyStack<T> { private T[] stack; private int topIndex; private boolean initialized = false; private static final int DEFAULT_CAPACITY = 20; private static final int MAX_CAPACITY = 5000; public MyStack() { this(DEFAULT_CAPACITY); } // end default constructor public MyStack(int initialCapacity) { checkCapacity(initialCapacity);
@SuppressWarnings("unchecked") T[] tempStack = (T[])new Object[initialCapacity]; stack = tempStack; topIndex = -1; initialized = true; } public void push(T newEntry) { checkInitialization(); ensureCapacity(); stack[topIndex + 1] = newEntry; topIndex++; }
public T peek() { checkInitialization(); if (isEmpty()) throw new EmptyStackException(); else return stack[topIndex]; }
public T pop() { checkInitialization(); if (isEmpty()) throw new EmptyStackException(); else { T top = stack[topIndex]; stack[topIndex] = null; topIndex--; return top; } }
public boolean isEmpty() { return topIndex < 0; } public void clear() { checkInitialization();
while (topIndex > -1) { stack[topIndex] = null; topIndex--; }
}
private void checkInitialization() { if (!initialized) throw new SecurityException ("ArrayStack object is not initialized properly."); }
private void checkCapacity(int capacity) { if (capacity > MAX_CAPACITY) throw new IllegalStateException("Attempt to create a stack " + "whose capacity exceeds " + "allowed maximum."); }
private void ensureCapacity() { if (topIndex >= stack.length - 1) { int newLength = 2 * stack.length; checkCapacity(newLength); stack = Arrays.copyOf(stack, newLength); } } } part. Both input files are orn folder Part II-Step 2: Test your programming skill on File /O and Stack. (70 points) Continue with the main program of Exam2 and uncomment the three lines related to Step2 and then complete this step according to the following: public class Exam2 ( public static void main (Stringl) args) ( System.out.printin"Exam 2: Step 1 by (Your Name)"): Step1 s1 = new Step1(); //Step 2 System.out.printin("nExam 2: Step 2 by (Your Name)"); String fileName = "xxxxx.txt'.. Step2 s2 = new Step2(fileName); //Step3 /You may refer to the sample output at 4 first. 1. Assign "e2arts.txt" to the fileName. i.e., filename "e2arts.txt" 2. Write a simple class called MyArt which contains the following: a. Two fields: artName, artistName. b. Constructor that takes the values of above two fields. c. toString that prints these two values separated by a tab. 3. Complete Step2 class according to the following: which contains the following lines inside your program: int STACK-SIZE = 20; MyStack myArtStack new MyStack(STACK SIZE); //This is your generic stack that you created during our lab hours MyArtistList myArtistList new MyArtistList("e2artists.txt"); a. //This is the class that you defined in Unit 1. Remember that this class will read the entire artists into an ArrayList or an array. b. this program logic is similar to processing the transaction file that you did fo project 2. The only difference is that this program will read "e2arts.txt" an the artist name associated with this piece of art. (i.e., in your MyArtistList
Explanation / Answer
//Step2.java
public class Step2 {
public static void main(String[] args)
{
System.out.println(" 2: Step 2 by (Your Name)");
String fileName="arts.txt";
MyArtistList myArtistList=
new MyArtistList(fileName);
myArtistList.print();
System.out.println(" MyArtStack = ");
System.out.println(myArtistList);
}
}
---------------------------------------------------------------------------
////MyArtistList.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class MyArtistList {
private int STACK_SIZE=20;
private MyStack<MyArt> myArtStack;
private ArrayList<String>arts;
/**
* Constructor that takes a fileName
* of arts and read artists and insert
* into the myartsstack
* */
public MyArtistList(String fileName) {
myArtStack=new MyStack<MyArt>(STACK_SIZE);
arts=new ArrayList<String>(STACK_SIZE);
readArtists("artists.txt",arts);
Scanner filescanner=null;
try {
filescanner=new Scanner(new File(fileName));
while(filescanner.hasNextLine())
{
String line=filescanner.nextLine();
//user delimiter string pattern for +, * and $
//The tokens separated by the +, * and $
StringTokenizer tokens
=new StringTokenizer(line, "+*$");
int num=Integer.parseInt(tokens.nextToken());
String artName=tokens.nextToken();
int artistID=Integer.parseInt(tokens.nextToken());
double cost=Double.parseDouble(tokens.nextToken());
String artistName=getArtistName(artistID,arts);
myArtStack.push(new MyArt(artName,artistName ));
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
/**
* Function to print artist
* */
public void print()
{
for (int i = 0; i < arts.size(); i++)
{
System.out.println(i+1 +" "+arts.get(i));
}
}
/**
* Override the toString method
* */
public String toString() {
String temp="";
while (!myArtStack.isEmpty()) {
temp+=myArtStack.pop()+" ";
}
return temp;
}
/**
* Method read artists into the array,artists
* */
private void readArtists(String fileName,
ArrayList<String> artists)
{
Scanner filescanner=null;
try {
filescanner=new Scanner(new File(fileName));
while(filescanner.hasNextLine())
{
int id=filescanner.nextInt();
String artist=filescanner.nextLine();
arts.add(id-1,artist);
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
/**
* Method that takes artistID and
* returns the name of the artist
* */
private String getArtistName(int artistID,
ArrayList<String> artists) {
return artists.get(artistID-1);
}
}
---------------------------------------------------------------------------
//MyStack.java
import java.util.Arrays;
import java.util.EmptyStackException;
public final class MyStack<T>
{
private T[] stack;
private int topIndex;
private boolean initialized = false;
private static final int DEFAULT_CAPACITY = 20;
private static final int MAX_CAPACITY = 5000;
public MyStack()
{
this(DEFAULT_CAPACITY);
} // end default constructor
public MyStack(int initialCapacity)
{
checkCapacity(initialCapacity);
@SuppressWarnings("unchecked")
T[] tempStack = (T[])new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
initialized = true;
}
public void push(T newEntry)
{
checkInitialization();
ensureCapacity();
stack[topIndex + 1] = newEntry;
topIndex++;
}
public T peek()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
return stack[topIndex];
}
public T pop()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
{
T top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
return top;
}
}
public boolean isEmpty()
{
return topIndex < 0;
}
public void clear()
{
checkInitialization();
while (topIndex > -1)
{
stack[topIndex] = null;
topIndex--;
}
}
private void checkInitialization()
{
if (!initialized)
throw new SecurityException ("ArrayStack object is not initialized properly.");
}
private void checkCapacity(int capacity)
{
if (capacity > MAX_CAPACITY)
throw new IllegalStateException("Attempt to create a stack " +
"whose capacity exceeds " +
"allowed maximum.");
}
private void ensureCapacity()
{
if (topIndex >= stack.length - 1)
{
int newLength = 2 * stack.length;
checkCapacity(newLength);
stack = Arrays.copyOf(stack, newLength);
}
}
}
---------------------------------------------------------------------------
//MyArt.java
public class MyArt {
private String artName;
private String artistName;
//constructor that takes artname and artist name
public MyArt(String artName,String artistName ) {
this.artName=artName;
this.artistName=artistName;
}
//Override toStirng method
public String toString() {
return artName+" "+artistName;
}
}
---------------------------------------------------------------------------
arts.txt
1038+Spring Flowers*1$800.99
1050+Cattle Ranch*1$10000.99
1103+Trail End*1$8000.50
1042+Coffee on the Trail*2$7544.50
1013+Superstitions*3$78000.40
1021+Bead Wall*3$14000.00
1034+Beaver Pole Jumble*3$28000.00
1063+Asleep in the Garden*3$110000
1070+Beginnings*4$27500.00
1036+Blackhawk*5$25500.00
artists.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
---------------------------------------------------------------------------
Sample Ouptut Screenshot:
2: Step 2 by (Your Name)
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
MyArtStack =
Blackhawk Edwards
Beginnings Dill
Asleep in the Garden Carpenter
Beaver Pole Jumble Carpenter
Bead Wall Carpenter
Superstitions Carpenter
Coffee on the Trail Budd
Trail End Acconci
Cattle Ranch Acconci
Spring Flowers Acconci
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.