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

The final program for CTS2450 is an individual program of your choosing. You dec

ID: 3814508 • Letter: T

Question

The final program for CTS2450 is an individual program of your choosing. You decide what problem you want to solve and write the program to solve it. It must include a written description of what the program does and instructions about how the program works in the comments section. I do not want to see a program from the text, but all of you have interests and hobbies that you could design a program that would meet the following criteria.

Comments

Enough so another programmer will know what the program is doing without even reading your description

Also, include your name, date, and program name at the top of the program

Selection structure

Include either If/else logic or a case structure

Repetition structure

Include either While, Do While or For loop

Array

Any type of an array

Inheritance

Use Inheritance and/or Interfaces

Exception

Use try…catch block

Components

Button, TextField, Label, or ComboBox

Theme

There must be some point to your program, not just a collection of components

Receives user input

Performs output  

Add a graphic or color                   

To receive full credit you will design a JavaFX Application that contains at least 4 of the 6, (selection, loop, array, inheritance, components or exception) plus all of the others mentioned above.

Comments

Enough so another programmer will know what the program is doing without even reading your description

Also, include your name, date, and program name at the top of the program

Selection structure

Include either If/else logic or a case structure

Repetition structure

Include either While, Do While or For loop

Array

Any type of an array

Inheritance

Use Inheritance and/or Interfaces

Exception

Use try…catch block

Components

Button, TextField, Label, or ComboBox

Theme

There must be some point to your program, not just a collection of components

Explanation / Answer

package fx;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.HashMap;
import java.util.Map;

// a simple JavaFX Calcultorulator.
public class Calcultor extends Application {
private static final String[][] template = {
      { "7", "8", "9", "/" },
      { "4", "5", "6", "*" },
      { "1", "2", "3", "-" },
      { "0", "c", "=", "+" }
};

private final Map<String, Button> accelerators = new HashMap<>();

private DoubleProperty stackValue = new SimpleDoubleProperty();
private DoubleProperty value = new SimpleDoubleProperty();

private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }

private Op curOp   = Op.NOOP;
private Op stackOp = Op.NOOP;

public static void main(String[] args) { launch(args); }

@Override public void start(Stage stage) {
    final TextField screen = createScreen();
    final TilePane buttons = createButtons();

    stage.setTitle("Calcultor");
    stage.initStyle(StageStyle.UTILITY);
    stage.setResizable(false);
    stage.setScene(new Scene(createLayout(screen, buttons)));
    stage.show();
}

private VBox createLayout(TextField screen, TilePane buttons) {
    final VBox layout = new VBox(20);
    layout.setAlignment(Pos.CENTER);
    layout.setStyle("-fx-background-color: chocolate; -fx-padding: 20; -fx-font-size: 20;");
    layout.getChildren().setAll(screen, buttons);
    handleAccelerators(layout);
    screen.prefWidthProperty().bind(buttons.widthProperty());
    return layout;
}

private void handleAccelerators(VBox layout) {
    layout.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
      @Override
      public void handle(KeyEvent keyEvent) {
        Button activated = accelerators.get(keyEvent.getText());
        if (activated != null) {
          activated.fire();
        }
      }
    });
}

private TextField createScreen() {
    final TextField screen = new TextField();
    screen.setStyle("-fx-background-color: aquamarine;");
    screen.setAlignment(Pos.CENTER_RIGHT);
    screen.setEditable(false);
    screen.textProperty().bind(Bindings.format("%.0f", value));
    return screen;
}

private TilePane createButtons() {
    TilePane buttons = new TilePane();
    buttons.setVgap(7);
    buttons.setHgap(7);
    buttons.setPrefColumns(template[0].length);
    for (String[] r: template) {
      for (String s: r) {
        buttons.getChildren().add(createButton(s));
      }
    }
    return buttons;
}

private Button createButton(final String s) {
    Button button = makeStandardButton(s);

    if (s.matches("[0-9]")) {
      makeNumericButton(s, button);
    } else {
      final ObjectProperty<Op> triggerOp = determineOperand(s);
      if (triggerOp.get() != Op.NOOP) {
        makeOperandButton(button, triggerOp);
      } else if ("c".equals(s)) {
        makeClearButton(button);
      } else if ("=".equals(s)) {
        makeEqualsButton(button);
      }
    }

    return button;
}

private ObjectProperty<Op> determineOperand(String s) {
    final ObjectProperty<Op> triggerOp = new SimpleObjectProperty<>(Op.NOOP);
    switch (s) {
      case "+": triggerOp.set(Op.ADD);      break;
      case "-": triggerOp.set(Op.SUBTRACT); break;
      case "*": triggerOp.set(Op.MULTIPLY); break;
      case "/": triggerOp.set(Op.DIVIDE);   break;
    }
    return triggerOp;
}

private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
    button.setStyle("-fx-base: lightgray;");
    button.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent actionEvent) {
        curOp = triggerOp.get();
      }
    });
}

private Button makeStandardButton(String s) {
    Button button = new Button(s);
    button.setStyle("-fx-base: beige;");
    accelerators.put(s, button);
    button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
    return button;
}

private void makeNumericButton(final String s, Button button) {
    button.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent actionEvent) {
        if (curOp == Op.NOOP) {
          value.set(value.get() * 10 + Integer.parseInt(s));
        } else {
          stackValue.set(value.get());
          value.set(Integer.parseInt(s));
          stackOp = curOp;
          curOp = Op.NOOP;
        }
      }
    });
}

private void makeClearButton(Button button) {
    button.setStyle("-fx-base: mistyrose;");
    button.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent actionEvent) {
        value.set(0);
      }
    });
}

private void makeEqualsButton(Button button) {
    button.setStyle("-fx-base: ghostwhite;");
    button.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent actionEvent) {
        switch (stackOp) {
          case ADD:      value.set(stackValue.get() + value.get()); break;
          case SUBTRACT: value.set(stackValue.get() - value.get()); break;
          case MULTIPLY: value.set(stackValue.get() * value.get()); break;
          case DIVIDE:   value.set(stackValue.get() / value.get()); break;
        }
      }
    });
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote