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

Please help me with this java assignment using javafx. We will make our GUI actu

ID: 3822414 • Letter: P

Question

Please help me with this java assignment using javafx.

We will make our GUI actually control our memory calculator. To do this, you will need to create an instance of MemoryCalc in the CalcGUI and write action listeners to pass information back and forth between the calculator and the GUI. There are many possible ways to achieve this, but the easiest is probably to develop four different action listeners: DigitListener – This action listener is added to the 0-9 and . (dot) keys. If the equals button was just pressed, this action listener will overwrite the total shown in the text field with the value of the key (its label) that was just pressed. If the equals button was not just pressed, this action listener will append the current key's label onto the end of the string shown in the text field. OperatorListener – This action listener is added to the +, -, *, and / keys. It sets the value of a class field to the operator that the user has chosen. Clear Listener – This action listener is added to the C key. It calls the calculator's clear method and sets the value of the text field back to 0.0. EqualsListener – This action listener is added to the = key. It reads the current value from the text field, converts it to a double, and calls the appropriate calculator method based on the current operator, passing in the double value. The calculator will compute the answer, and then this action listener will update the value in the text field to the calculator's current total.When the EqualsListener is converting the text shown in the text field into a double value in order to pass it to the calculator, be careful to handle the case where the text field contains an invalid value, such as 6..72. In this case you should catch the exception, display an error message to the user, and abort the call to the calculator (just restore the current total to the text field).

PLEASE use this code that I have done so far, the design of this GUI should look like what I got it in this code.

package myJavaFx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Labeled;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class javaFx extends Application {
  
   private MemoryCalculator calc;
   private TextField txtDisplay;
  
   MemoryCalculator clc = new MemoryCalculator();
  
   public void start(Stage primaryStage){
      
       calc = new MemoryCalculator();
      
       GridPane buttonPane = new GridPane();
       buttonPane.setHgap(0);
       buttonPane.setVgap(0);
       for (int i = 0; i < 4; i++) {
   ColumnConstraints column = new ColumnConstraints();
   column.setPercentWidth(25);
   buttonPane.getColumnConstraints().add(column);
   }
      
       Button bt7 = new Button("7");
       bt7.setPrefSize(60, 60);
       bt7.setOnAction(digitListener);
      
       Button bt8 = new Button("8");
       bt8.setPrefSize(60, 60);
       bt8.setOnAction(digitListener);
      
       Button bt9 = new Button("9");
       bt9.setPrefSize(60, 60);
       bt9.setOnAction(digitListener);

       Button btD = new Button("/");
       btD.setPrefSize(60, 60);

       buttonPane.addRow(1, bt7, bt8, bt9, btD);
      
       Button bt4 = new Button("4");
       bt4.setPrefSize(60, 60);
       bt4.setOnAction(digitListener);
      
       Button bt5 = new Button("5");
       bt5.setPrefSize(60, 60);
       bt5.setOnAction(digitListener);
      
       Button bt6 = new Button("6");
       bt6.setPrefSize(60, 60);
       bt6.setOnAction(digitListener);

       Button btM = new Button("*");
       btM.setPrefSize(60, 60);
      
       buttonPane.addRow(2, bt4, bt5, bt6, btM);
      
       Button bt1 = new Button("1");
       bt1.setPrefSize(60, 60);
       bt1.setOnAction(digitListener);
      
       Button bt2 = new Button("2");
       bt2.setPrefSize(60, 60);
       bt2.setOnAction(digitListener);
      
       Button bt3 = new Button("3");
       bt3.setPrefSize(60, 60);
       bt3.setOnAction(digitListener);

       Button btS = new Button("-");
       btS.setPrefSize(60, 60);
      
       buttonPane.addRow(3, bt1, bt2, bt3, btS);
      
       Button btC = new Button("C");
       btC.setPrefSize(60, 60);
       btC.setOnAction(clearListener);
      
       Button bt0 = new Button("0");
       bt0.setPrefSize(60, 60);
       bt0.setOnAction(digitListener);
      
       Button btDot = new Button(".");
       btDot.setPrefSize(60, 60);
       btDot.setOnAction(digitListener);

       Button btP = new Button("+");
       btP.setPrefSize(60, 60);
       btP.setOnAction(operatorListener);
      
       buttonPane.addRow(4, btC, bt0, btDot, btP);
      
       Button btE = new Button("=");
       btE.setPrefSize(240, 60);
       btE.setOnAction(equalsListener);
      
       buttonPane.add(btE, 0, 5, 4, 1);
      
       txtDisplay = new TextField();
       txtDisplay.setEditable(false);
       txtDisplay.setText("0.0");
      
       buttonPane.add(txtDisplay, 0, 0, 4, 1);
      
       Scene scene = new Scene(buttonPane);
       primaryStage.setTitle("GUI Calc");
       primaryStage.setScene(scene);
       primaryStage.sizeToScene();
       primaryStage.show();
      
      
   }
  
   public static void main(String[] args) {
       launch(args);
      
      
   }
  
   private EventHandler<ActionEvent> digitListener = new EventHandler<ActionEvent>() {
       public void handle (ActionEvent actionEvent) {
           String button = ((Labeled) actionEvent.getSource()).getText();
           if(txtDisplay.getText().equals("0.0"))
               txtDisplay.setText(button);
           else {
               txtDisplay.setText(txtDisplay.getText() + button);
           }
       }
   };
  
   private EventHandler<ActionEvent> operatorListener = new EventHandler<ActionEvent>(){

       @Override
       public void handle(ActionEvent e) {
           Button btn = (Button) e.getSource();
           String str = btn.getText();

           if (str.equals("+")) {
               double num2 = Double.parseDouble(txtDisplay.getText());
               clc.add(num2);
               txtDisplay.setText(String.valueOf(clc.getCurrentValue()));
              
           } else if (str.equals("-")) {
               double num2 = Double.parseDouble(txtDisplay.getText());
               clc.subtract(num2);
               txtDisplay.setText(String.valueOf(clc.getCurrentValue()));
              
           }
           else if (str.equals("*")){
               double num2 = Double.parseDouble(txtDisplay.getText());
               clc.multiply(num2);
               txtDisplay.setText(String.valueOf(clc.getCurrentValue()));
              
           }
           else if (str.equals("/")){
               double num2 = Double.parseDouble(txtDisplay.getText());
               clc.divide(num2);
               txtDisplay.setText(String.valueOf(clc.getCurrentValue()));
              
           }

           System.out.println(btn);
           System.out.println(str);
          
       }
      
   };
  
   private EventHandler<ActionEvent> clearListener = new EventHandler<ActionEvent>(){
         
       @Override
       public void handle(ActionEvent e) {
           Button btn = (Button) e.getSource();
                   String str = btn.getText();
                  
                   if (str.equals("C")) {
                       clc.clear();
                       txtDisplay.setText(String.valueOf(clc.getCurrentValue()));
                   }
      
       }
       };
      
       private EventHandler<ActionEvent> equalsListener = new EventHandler<ActionEvent>(){
                 
           @Override
           public void handle(ActionEvent e) {
               Button btn = (Button) e.getSource();
                       String str = btn.getText();
                      
                       if (str.equals("=")) {
                          
                       }
          
           }
           };


}

Explanation / Answer

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.text.NumberFormat;

//An event handler class is declared and specifying that it extends a class that

//implements an interface of ActionListener

public class Assignment16 extends JFrame implements ActionListener

{

    //Content pane layer is been retrieved so as to add objects    to it

    Container content = this.getContentPane();

    

     //Constructs a new TextField for Amount

    JTextField tfAmount= new JTextField();

    

     //Constructs a new TextField for Interest

    JTextField tfInterest= new JTextField();

    

     //Constructs a new TextField for Years

    JTextField tfYears = new JTextField();

    

    

     //Creating lables for payment and total cost

    JLabel lblPayment = new JLabel("");

    JLabel lblTotCost = new JLabel("");

    

    

    public Assignment16()

    {

          // frame window closing operation

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         

          //set frame size to 350*300

        this.setSize(350, 300);

         

          //frame title

        this.setTitle(" Interest Calculator ");

         

          //creates a gridlayout with specified count of rows &              columns

        content.setLayout(new GridLayout(0, 2, 5, 5));

         

          //label for Amount borrowed

        JLabel amtBorrow = new JLabel("Amount Borrowed");

         

          //Adding contents of input amount

        content.add(amtBorrow, BorderLayout.EAST);

        content.add(tfAmount);

         

          //label for Interest rate

        JLabel iRate = new JLabel("Interest Rate");

         

          //Adding contents of user input Interest rate

        content.add(iRate, BorderLayout.EAST);

        content.add(tfInterest);

         

          //Label for years

        JLabel yearPay = new JLabel("Years to Pay");

         

          //Adding contents of user input Years

        content.add(yearPay, BorderLayout.EAST);

        content.add(tfYears);

         

          //Label for Monthly Payment

        JLabel mPayment = new JLabel("Monthly Payment");

         

          //Adding contents of calculated monthly payment value

        content.add(mPayment, BorderLayout.EAST);

        content.add(lblPayment);

         

          //Label for total purchase cost

        JLabel totCost = new JLabel("Total Purchase Cost");

         

          //Adding contents of calculated purchase cost value

        content.add(totCost, BorderLayout.EAST);

        content.add(lblTotCost);

         

          //Calculate button

        JButton btn = new JButton(" Calculate ");

       

          //button is added to frame

          content.add(btn);

         

          //button event handler

        btn.addActionListener(this);

        this.setVisible(true);

    }

    @Override

    

     //method implementation in listener interface

    public void actionPerformed(ActionEvent e)

    {

         //Getting user input values

        String amount = tfAmount.getText();

        String interest = tfInterest.getText();

        String years = tfYears.getText();

        double amountVl = new Double(amount);

        double interestVl = new Double(interest);

        double yearsVl = new Double(years);

         

          // Interest calculation

        interestVl = interestVl/100;

        interestVl = interestVl/12;

         

          // equation for payment

        double payment = (amountVl * interestVl)/(1 - (Math.pow(1/(1+interestVl), yearsVl * 12)));

        NumberFormat form = NumberFormat.getCurrencyInstance();

       

          //Displaying values of payment and total cost

          lblPayment.setText(form.format(payment));

        lblTotCost.setText(form.format(payment*12*yearsVl));

    }

    

     //Main method

    public static void main(String[] args)

    {

        Assignment16 gui = new Assignment16();

    }

}

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