Sorry, that post might be quite confusing, my mind has kind of melted... let me know if further detail is needed.
String key = Greenfoot.getKey(); if (key != null) System.out.println(key);
public void updateDisplay(){
Calculate calculate = new Calculate();
String displayCalculation = calculate.getCurrentCalculation();
String cursor = ":";
int index = calculate.getCurrentIndex();
if (!displayCalculation.equals("")){
displayCalculation = calculate.insert(displayCalculation, index, cursor);
}
else{
displayCalculation = "0";
}
System.out.println(displayCalculation);
showText(displayCalculation, xCoord4, yCoord4);
} public static String delete(String aString, int index){
String placeholderString1 = "";
String placeholderString2 = "";
if (index != 0){
placeholderString1 = aString.substring(0, index-1);
}
if (index != aString.length()){
placeholderString2 = aString.substring(index, aString.length());
}
aString = placeholderString1 + placeholderString2;
return aString;
}
public static String insert(String aString, int index, String character){
String placeholderString1 = "";
String placeholderString2 = "";
if (index != 0){
placeholderString1 = aString.substring(0, index);
}
if (index != aString.length()){
placeholderString2 = aString.substring(index, aString.length());
}
aString = placeholderString1 + character + placeholderString2;
return aString;
}String key = Greenfoot.getKey(); if (key != null) System.out.println(key);
//Button class
public void buttonPress(){ /**override this in subclasses*/
if(getWorld() instanceof Calculator){//if world is 'Calculator'
/**possible source of issue*/
Calculate calculate = new Calculate();
calculate.add(button);//call add method in calculate
if (!button.equals("=")){
/**possible source of issue*/
Calculator world = new Calculator();
world.updateDisplay();
}
else{
Calculator world = new Calculator();
/**world.showAnswer*/
}
/**call method from world that updates the display.
* the method must call on getcurrentcalculation method of calculate
* and assign that string to a string called displayCalculation
* the method must call on getcurrentindex method of calculate
* and then insert ":" at index+1 of displayCalculation
* (possibly using calculate's insert method)
* and showText(displaycalculation) etc
*/
}
/**delete*/
System.out.println(button);
}
//Calculate class
public void add(String chars){
/**check if chars == mode alpha store. if so, then 'return;'*/
if (processingCalculation == false){
holder = chars;
//check to see if input is a number
if (checkIfNumber(holder)){
currentCalculation = insert(currentCalculation, currentIndex, holder);
}
else if (holder.equals("Clear")){
currentCalculation = "";
currentIndex = -1; //+1 is added at end of method
System.out.println("CLEARED");
System.out.println("_______");
}
else if (holder.equals("Delete")){
if (! currentCalculation.equals("")){
currentCalculation = delete(currentCalculation, currentIndex);
}
if (currentIndex > 0){
currentIndex = currentIndex - 2;
//currentIndex - 2 + 1 = intended currentIndex
//will not matter if currentIndex results as -1...
//...because +1 is added at the end of the method
}
}
}
/**make sure subclasses of button arent registered!*/
/**starts at zero each time a button is pressed then increases by 1, to equal 1?*/
currentIndex = currentIndex + 1;
System.out.println("a" + currentIndex); //error checking
} public void buttonPress(){ /**override this in subclasses*/
if(getWorld() instanceof Calculator){//if world is 'Calculator'
Calculator world = (Calculator) getWorld();
calculate.add(button);//call add method in calculate
if (!button.equals("=")){
world.updateDisplay();
}public Calculator()
{
super(420, 600, 1);
makeNumButtons();
makeOpButtons();
makeAdvOpButtons();
makeActionButtons();
makeArrowButtons();
prepare();
updateDisplay();
}
public void makeNumButtons(){
for(int i = 0; i < 15; i++) { /**change 17 to length of array*/
if(!numButton[i].equals("")){
Button myButton = new Button(numButton[i]);
addObject(myButton, xCoord1, yCoord1);
}
xCoord1 += buttonSpacingXVal;
if((i+1)%4 == 0){
xCoord1 = xCoordStart;
yCoord1 = (yCoordStart - buttonSpacingYVal) - buttonSpacingYVal * ((i-3)/4);
}
}
}
public void makeOpButtons(){
for(int i = 0; i < 16; i++) { /**change 17 to length of array*/
if(!operationButton[i].equals("")){
Button myButton = new Button(operationButton[i]);
addObject(myButton, xCoord2, yCoord2);
}
xCoord2 += buttonSpacingXVal;
if((i+1)%4 == 0){
xCoord2 = xCoordStart;
yCoord2 = (yCoordStart - buttonSpacingYVal) - buttonSpacingYVal * ((i-3)/4);
}
}
}
public void makeAdvOpButtons(){ /**change 17 to length of array*/
for(int i = 0; i < 28; i++) {
if(!advOperationButton[i].equals("")){
Button myButton = new Button(advOperationButton[i], advOperationButtonName[i]);
addObject(myButton, xCoord3, yCoord3);
}
xCoord3 += buttonSpacingXVal;
if((i+1)%4 == 0){
xCoord3 = xCoordStart;
yCoord3 = (yCoordStart - buttonSpacingYVal) - buttonSpacingYVal * ((i-3)/4);
}
}
//Clear and Delete buttons
Button clear = new Button("Clear", " AC");
addObject(clear, xCoordStart + (buttonSpacingXVal * 3), yCoordStart - (buttonSpacingYVal * 8));
Button delete = new Button("Delete", "Del");
addObject(delete, xCoordStart + (buttonSpacingXVal * 5 / 2), yCoordStart - (buttonSpacingYVal * 7));
}
public void makeActionButtons(){
StoreButton store = new StoreButton();
ModeButton mode = new ModeButton();
AlphaButton alpha = new AlphaButton();
addObject(store, xCoordStart, yCoordStart - (buttonSpacingYVal * 5));
addObject(mode, xCoordStart, yCoordStart - (buttonSpacingYVal * 8));
addObject(alpha, xCoordStart + (buttonSpacingXVal/2), yCoordStart - (buttonSpacingYVal * 7));
}
public void makeArrowButtons(){ /**change 17 to length of array*/
//make the area that the buttons will occupy
Arrows arrowCircle = new Arrows();
addObject(arrowCircle, (getWidth() / 2 + 5), (getHeight() / 2 - 60));
DownArrow down = new DownArrow();
addObject(down, (getWidth() / 2 + 5), (getHeight() / 2 - 27));
UpArrow up = new UpArrow();
addObject(up, (getWidth() / 2 + 5), (getHeight() / 2 - 97));
LeftArrow left = new LeftArrow();
addObject(left, (getWidth() / 2 - 30), (getHeight() / 2 - 62));
RightArrow right = new RightArrow();
addObject(right, (getWidth() / 2 + 39), (getHeight() / 2 - 62));
}
public void prepare(){
CheckKeyPress keypress = new CheckKeyPress();
addObject(keypress, 0, 0);
}Calculate calculate = ((Calculator)getWorld()).myCalculator;
Calculate calculate = new Calculate();