Question
JavaFX question 13.6 (Tip Calculator Modification) The Tip Calculator app from Section 12.5 does not need a Button to perform its calculations. Reimplement this app
JavaFX question
13.6(Tip Calculator Modification)TheTip Calculatorapp from Section 12.5 does not need a Button to perform its calculations. Reimplement this app to use property listeners to perform the calculations whenever the user modifies the bill amount or changes the custom tip percentage. Also use a property binding to update the Label that displays the tip percentage.
I don't have an option to attach a file, so I will have to paste all code here....
This is from a partial answer in which the Calculate button is gone and the tip and total are instant, but the tip slider must be set before entering an amount, and after entering an amount, you cannot change the amount or tip. I need the tip slider and Amount to be dynamic.
3 files, TipCalculator.java, TipCalculator.FXML, and TipCalculatorController.java. I believe all modifications are done to the Controller file. I will also attach a pic of what the listeners and binders should look like.
These files are from https://www.coursehero.com/tutors-problems/Computer-Science/10509735-I-need-a-modification-on-the-following-program-attached-Here-is-the-m/
They are not my work. They are answers to the same question, but they are not complete.
/////////////////////////////////////////////
TipCalculator.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TipCalculator extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root =
FXMLLoader.load(getClass().getResource("TipCalculator.fxml"));
Scene scene = new Scene(root); // attach scene graph to scene
stage.setTitle("Tip Calculator"); // displayed in window's title bar
stage.setScene(scene); // attach scene to stage
stage.show(); // display the stage
}
public static void main(String[] args)
{
// create a TipCalculator object and call its start method
launch(args);
}
}
////////////////////////////////////////////////////////////////////////////
TipCalculatorController.java
package application;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
public class TipCalculatorController
{
// formatters for currency and percentages
private static final NumberFormat currency =
NumberFormat.getCurrencyInstance();
private static final NumberFormat percent =
NumberFormat.getPercentInstance();
private BigDecimal tipPercentage = new BigDecimal(0.15); // 15% default
// GUI controls defined in FXML and used by the controller's code
@FXML
private TextField amountTextField;
@FXML
private Label tipPercentageLabel;
@FXML
private Slider tipPercentageSlider;
@FXML
private TextField tipTextField;
@FXML
private TextField totalTextField;
// calculates and displays the tip and total amounts
@FXML
private void calculateButtonPressed(Event event)
{
try
{
BigDecimal amount = new BigDecimal(amountTextField.getText());
BigDecimal tip = amount.multiply(tipPercentage);
BigDecimal total = amount.add(tip);
tipTextField.setText(currency.format(tip));
totalTextField.setText(currency.format(total));
}
catch (NumberFormatException ex)
{
amountTextField.setText("Enter amount");
amountTextField.selectAll();
amountTextField.requestFocus();
}
}
// called by FXMLLoader to initialize the controller
public void initialize()
{
// 0-4 rounds down, 5-9 rounds up
currency.setRoundingMode(RoundingMode.HALF_UP);
// listener for changes to tipPercentageSlider's value
tipPercentageSlider.valueProperty().addListener((ObservableValue extends Number> ov, Number oldValue, Number newValue) -> {
tipPercentage =
BigDecimal.valueOf(newValue.intValue() / 100.0);
tipPercentageLabel.setText(percent.format(tipPercentage));
});
}
}
/////////////////////////////////////////////////////////////
TipCalculator.FXML
//////////////////////////////////////////////////////////////////////////////////////
What the mods should look like (just an example) near the bottom of TipCalculatorController.java
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