Question
I need assistance to make a accumulator accumulator-calculator that displays a sad face :-( whenever the number displayed by the calculator is negative
I need assistance to make a accumulator accumulator-calculator that displays a sad face " :-( " whenever the number displayed by the calculator is negative and a happy face " :-) " whenever the number displayed is positive.
The calculator responds to the following commands: num + , num - , num * , num / and C ( Clear ).
After initial run, if the user clicks 8 and then presses the "+" button for example, the accumulator value displays 8 in one field and a happy face in the other. If the second number entered by the user is 9 followed by "-" button, the accumulator displays -1 and the face changes to the sad mode. If the next command is C, the accumulator will be initialized back to zero and the two Text Fields will be cleared
Notice also that, this kind of calculator accepts only single digit numbers as input at any given time.
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
public class StartupAccumulatorCalculator_JavaFX extends Application {
private TextField txtOutput = new TextField();
TextField txtFace = new TextField();
private float inputValue;
private Calculator calculator = new Calculator();
private Face face = new Face();
public static void main(String[] args) {
launch(args);
}
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
Scene scene = new Scene(getPane(), 200, 250);
primaryStage.setTitle("Fun Calculator"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* getPane() returns a pane to be used with the scene of this calculator.
* In this method, you will need to generate the GUI of this calculator. Use different kinds of panes for alignment
* This method will also implement the event handlers for each button press. You may elect to divide the load among multiple methods if you prefer.
*/
protected BorderPane getPane() {
BorderPane mainPane = new BorderPane();
//Your code goes here .....
return mainPane;
}
//----------------------------------------------------------
/**
* This method checks the accumulator value and by calling getAccumValue(). based on this value it either calls the
* face.makeHappy() or face.makeSad() and sets the face to happy/sad in txtFace
* Will be called whenever one of the operation buttons is pressed
*/
public void display(){
//Your code goes here .....
}
}
//*********************************************************
class Calculator
{
private float currentAccumValue;
public Calculator (){
currentAccumValue = 0.0f;
}
public void add(float inputValue){
currentAccumValue+= inputValue;
}
public void subtract(float inputValue){
currentAccumValue-= inputValue;
}
public void multiply(float inputValue){
currentAccumValue*= inputValue;
}
public void divide(float inputValue){
if(inputValue == 0) {
JOptionPane.showMessageDialog(null, "Error: You cannot divide by 0!", "Error",
JOptionPane.INFORMATION_MESSAGE);
} else {
currentAccumValue /= inputValue;
}
}
public void clearAccum(){
currentAccumValue = 0;
}
public float getAccumValue(){
return currentAccumValue;
}
}
//*********************************************************
class Face
{
private boolean faceState;
public Face(){
makeHappy();
}
public void makeHappy(){
faceState = true;
}
public void makeSad(){
faceState = false;
}
public boolean getFaceState(){
return faceState;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started