Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

e2arts.txt 1038+Spring Flowers*1$800.99 1050+Cattle Ranch*1$10000.99 1103+Trail

ID: 3606206 • Letter: E

Question

e2arts.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

e2artists.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);

}

}

}

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

a. MyArt.java

class MyArt {

String artName = null;

String artistName = null;

MyArt(String artName, String artistName) {
this.artName = artName;
this.artistName = artistName;
}

public void toString(String hello) {
System.out.println(this.artName + " " + this.artistName);
}

}

b. Is Given.

c. MyArtistList.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

class MyArtistList {

BufferedReader in = null;
Map<String, String> map = new HashMap<String, String>();

MyArtistList(String artistFile) {

try {
in = new BufferedReader(new FileReader(artistFile));

while (true) {
String str = in.readLine();
StringTokenizer st = new StringTokenizer(str);
if (str == null) {
break;
} else {
map.put(st.nextToken(), st.nextToken());
}
}

} catch (IOException e) {
System.out.println("Got Exception");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.out.println("Got Exception");
}
}
}

}

public String find(String artistId) {
return map.get(artistId);
}

public String find(int artistId) {
return map.get(Integer.toString(artistId));
}

}

d. Step2.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

class Step2 {

int STACK_SIZE = 20;
MyStack<MyArt> myArtStack = new MyStack<MyArt>(STACK_SIZE);
MyArtistList myArtistList = new MyArtistList("e2artist.txt");

BufferedReader in = null;

Step2(String artistFile) {
try {
in = new BufferedReader(new FileReader(artistFile));

while (true) {
String str = in.readLine();

if (str == null)
break;

StringTokenizer st = new StringTokenizer(str);

String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();
String s4 = st.nextToken();

System.out.println(s1 + "-" + s2 + "-" + s3 + "-" + s4);

myArtStack.push(new MyArt(s2, myArtistList.find(s3)));
}

System.out.println(" MyArtStack = ");
System.out.println(myArtStack);

} catch (IOException e) {
e.printStackTrace();
}

}

}

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote