Tip Calculator Programming Assignment For this assignment you are going to progr
ID: 3763523 • Letter: T
Question
Tip Calculator Programming Assignment
For this assignment you are going to program a JavaFX tip calculator app like you might find on your phone. The interface is shown below.
To use the app the user types in the check amount and the tip percent. Note that all text is right-justified. The user should be able to enter the tip percent as either a decimal (.18) or whole number (18). Your program should take care of converting the whole number to a decimal for calculating the tip amount. The user should not be allowed to edit or change the tip amount and the total.
Tip Calculator Check Amount 134.56 Tip Percent 18 Calculate Tip Tip Amount $24.22 Total $158.78Explanation / Answer
Code :
import javafx.application.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.stage.Stage;
/**
*
* @author EKTA
*/
public class TipCalculatorApp extends Application
{
@Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("Tip Calculator");
GridPane p1 = new GridPane();
p1.setAlignment(Pos.CENTER);
p1.setHgap(15);
p1.setVgap(15);
p1.setPadding(new Insets(20, 20, 20, 20));
Scene s1 = new Scene(p1, 350, 300);
Text Title1 = new Text("Tip Calculator");
p1.add(Title1, 0, 0, 2, 1);
Label Amount = new Label("Check Amount");
p1.add(Amount, 0, 1);
final TextField AmountField = new TextField();
p1.add(AmountField, 1, 1);
Label per = new Label("Tip Percent");
p1.add(per,0,2);
final TextField perField = new TextField();
p1.add(perField, 1, 2);
Button calcButton = new Button("Calculate Tip");
HBox h1 = new HBox(10);
h1.setAlignment(Pos.BOTTOM_RIGHT);
h1.getChildren().add(calcButton);
p1.add(h1, 1, 4);
Label Tip_Amount = new Label("Tip Amount");
p1.add(Tip_Amount, 1, 5);
final TextField F1 = new TextField();
p1.add(F1, 2, 5);
Label Total = new Label("Total");
p1.add(Total, 1, 6);
final TextField F2 = new TextField();
p1.add(F2, 2, 6);
calcButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent t) {
Double amt = Double.parseDouble(Amount Field.getText());
Double tip = Double.parseDouble(perField.getText())/100;
Double tip_value=tip*amt;
F1.setText(“$”+tip_value);
F1.setEditable(FALSE);
Double amount=amt+tip_value;
F2.setText(“$”+tip_value);
F1.setEditable(FALSE);
}
});
primaryStage.setScene(s1);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.