Hello, I am working on a Data Structures HW Problem using Java, and I am having
ID: 3723086 • Letter: H
Question
Hello, I am working on a Data Structures HW Problem using Java, and I am having a little trouble creating working code with the variables I have been given. Can you please help me trouble shoot what I already have?
class StackProblem {
private int[] array;
private int topOfArray;
private int maxArraySize;
public StackProblem(int m) { //constructor and parameters defined.
maxArraySize = m;
array = new int[maxArraySize];
topOfArray = -1;
}
public int popArray() {
return array[topOfArray--]; //return array-1 when array is popped.
}
public void pushArray(int i) {
array[++topOfArray] = additionToArray; //adds 1 element to array when push method occurs.
}
}
class ArrayEvaluation {
public int find(String stack) {
int value;
int start = 0;
value = stack.length();
StackProblem array = new StackProblem(value);
for (int i=0; i<value; i++) {
char start = stack.charAt(i);
if (start >= '0' && start <= '9') {
array.pushArray((int)(start -'0')); //push array (from 0th term) ?? trouble here
}
else {
int a = array.popArray();
int b = array.popArray();
int result;
if ('+') { //unsure how to make this a viable answer
result = a+b;
} else if ('-') {
result = b-a;
} else if ('*') {
result = a*b;
} else if ('/') {
result = b/a;
} else if (result == 0){
array.pushArray(result);
}
result = array.popArray();
return (result);
}
}
}
}
Explanation / Answer
class StackProblem {
private int[] array;
private int topOfArray;
private int maxArraySize;
public StackProblem(int m) { //constructor and parameters defined.
maxArraySize = m;
array = new int[maxArraySize];
topOfArray = -1;
}
public int popArray() {
return array[topOfArray--]; //return array-1 when array is popped.
}
public void pushArray(int i) {
array[++topOfArray] = i; //adds 1 element to array when push method occurs.
}
}
class ArrayEvaluation {
public int find(String stack) {
int value;
int result = 0;
value = stack.length();
StackProblem array = new StackProblem(value);
for (int i=0; i<value; i++) {
char start = stack.charAt(i);
if (start >= '0' && start <= '9') {
array.pushArray((int)(start -'0')); //push array (from 0th term) ?? trouble here
}
else {
int a = array.popArray();
int b = array.popArray();
if (start =='+') { //unsure how to make this a viable answer
result = a+b;
} else if (start == '-') {
result = b-a;
} else if (start =='*') {
result = a*b;
} else if (start=='/') {
result = b/a;
}
array.pushArray(result);
}
}
return array.popArray();
}
}
=================
I have corrected them all, Have a look..There are no errors now
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.