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

Java programming book : www.murach.com/download/javp.htm Here is the picture/ pr

ID: 3667166 • Letter: J

Question

Java programming book : www.murach.com/download/javp.htm
Here is the picture/ problem I need help with. 4 Section 2 Object-oriented programming with Java Enhance the Future Value application Exercise 7-2 This exercise guides you through the process of modifying the Future Value application so it uses classes that provide static methods. 1. Open the project named ch07_ex2_Future Value that' s stored in the ex _starts directory. Then, review the code for the Future Value App class. Start a new class named Validator in the same package as the Future Value class. Then, move the getDouble, getDouble WithinRange, getInt, an getInt WithinRange methods from the Future Value App class to the Validator elass. For this to work, you will also need to add an import statement for the 2. Pp Scanner class to the Validator class. Change the name of the getDouble WithinRange method to getDouble, and change the name of the getlntWithinRange method to getInt. This overloads the getDouble and getInt methods. Modify the Future Value App class so it uses the methods in the Validator class. Then, run the application to make sure that it still works correctly. Start a new class named FinancialCalculations and save it in the same package as the other classes. Then, move the calculateFuture Value method from the Future ValueApp class to the FinancialCalculations class, and make 3. 4. 5. sure that the method is public. Modify the Future Value App class so it uses the static calculateFuture Value 6. method that's stored in the FinancialCalculations class. Then, run the application to make sure that it still works properly.

Explanation / Answer

FutureValueFrame.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class HelloWorld
{
    public static void main(String[] args)
    {
        JFrame frame = new FutureValueFrame();
        frame.setVisible(true);
    }
}

class FutureValueFrame extends JFrame
{
    public FutureValueFrame()
    {
        setTitle("Future Value Calculator");
        centerWindow(this);
        setSize(275, 200);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new FutureValuePanel();
        this.add(panel);
    }

    private void centerWindow(Window w)
    {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
    }
}

class FutureValuePanel extends JPanel //implements ActionListener
{
    ActionListener listener = new FutureValueActionListener(this);
    private JTextField paymentTextField,
                        rateTextField,
                        yearsTextField,
                        futureValueTextField;
    private JLabel      paymentLabel,
                        rateLabel,
                        yearsLabel,
                        futureValueLabel;
    private JButton     calculateButton,
                        exitButton;

    public FutureValuePanel()
    {
        // display panel
        JPanel displayPanel = new JPanel();
        displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        // payment label
        paymentLabel = new JLabel("Monthly Payment:");
        displayPanel.add(paymentLabel);

        // payment text field
        paymentTextField = new JTextField(10);
        displayPanel.add(paymentTextField);

        // rate label
        rateLabel = new JLabel("Yearly Interest Rate:");
        displayPanel.add(rateLabel);

        // rate text field
        rateTextField = new JTextField(10);
        displayPanel.add(rateTextField);

        // years label
        yearsLabel = new JLabel("Number of Years:");
        displayPanel.add(yearsLabel);

        // years text field
        yearsTextField = new JTextField(10);
        displayPanel.add(yearsTextField);

        // future value label
        futureValueLabel = new JLabel("Future Value:");
        displayPanel.add(futureValueLabel);

        // future value text field
        futureValueTextField = new JTextField(10);
        futureValueTextField.setEditable(false);
        futureValueTextField.setFocusable(false);
        displayPanel.add(futureValueTextField);

        // button panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        // calculate button
        calculateButton = new JButton("Calculate");
        calculateButton.addActionListener(listener);
        buttonPanel.add(calculateButton);

        // exit button
        exitButton = new JButton("Exit");
        exitButton.addActionListener(listener);
        buttonPanel.add(exitButton);

        // add panels to main panel
        this.setLayout(new BorderLayout());
        this.add(displayPanel, BorderLayout.CENTER);
        this.add(buttonPanel, BorderLayout.SOUTH);
    }
    /*
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == exitButton)
            System.exit(0);
        else if (source == calculateButton)
        {
            double payment = Double.parseDouble(paymentTextField.getText());
            double rate = Double.parseDouble(rateTextField.getText());
            int years = Integer.parseInt(yearsTextField.getText());
            double futureValue = FinancialCalculations.calculateFutureValue(
                payment, rate, years);
            NumberFormat currency = NumberFormat.getCurrencyInstance();
            futureValueTextField.setText(currency.format(futureValue));
        }
    }
    */
    class FutureValueActionListener implements ActionListener
    {
        private FutureValuePanel panel;
        public FutureValueActionListener(FutureValuePanel p)
        {
            this.panel = p;
        }
      
        public void actionPerformed(ActionEvent e)
        {
            Object source = e.getSource();
            if (source == panel.exitButton)
                { System.exit(0); }
            else if (source == panel.calculateButton)
            {
               if (isValidData())
               {
                double payment = Double.parseDouble(paymentTextField.getText());
                double rate = Double.parseDouble(rateTextField.getText());
                int years = Integer.parseInt(yearsTextField.getText());
                double futureValue = FinancialCalculations.calculateFutureValue(
                payment, rate, years);
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                futureValueTextField.setText(currency.format(futureValue));
               }
            }
        }
      
        public boolean isValidData()
        {
            return SwingValidator.isPresent(paymentTextField, "Monthly Investment")
                && SwingValidator.isDouble(paymentTextField, "Monthly Investment")
                && SwingValidator.isPresent(rateTextField, "Interest Rate")
                && SwingValidator.isDouble(rateTextField, "Interest Rate")
                && SwingValidator.isPresent(yearsTextField, "Number of Years")
                && SwingValidator.isDouble(yearsTextField, "Number of Years");
        }
    }
}

SwingValidator.java

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.math.BigDecimal;

public class SwingValidator
{
   public static boolean isPresent(JTextComponent c, String title)
   {
       if (c.getText().length() == 0)
       {
            showMessage(c, title + " is a required field. "
               + "Please re-enter.");
           c.requestFocusInWindow();
           return false;
       }
       return true;
   }

   public static boolean isInteger(JTextComponent c, String title)
   {
       try
       {
           int i = Integer.parseInt(c.getText());
           return true;
       }
       catch (NumberFormatException e)
       {
            showMessage(c, title + " must be an integer. "
               + "Please re-enter.");
           c.requestFocusInWindow();
           return false;
       }
   }

   public static boolean isDouble(JTextComponent c, String title)
   {
       try
       {
           double d = Double.parseDouble(c.getText());
           return true;
       }
       catch (NumberFormatException e)
       {
            showMessage(c, title + " must be a valid number number. "
               + "Please re-enter.");
           c.requestFocusInWindow();
           return false;
       }
   }

   private static void showMessage(JTextComponent c, String message)
   {
            JOptionPane.showMessageDialog(c, message, "Invalid Entry",
               JOptionPane.ERROR_MESSAGE);
   }


}

FinancialCalculations.java


public class FinancialCalculations
{
    public static final int MONTHS_IN_YEAR = 12;

    public static double calculateFutureValue(double monthlyPayment,
    double yearlyInterestRate, int years)
    {
        int months = years * MONTHS_IN_YEAR;
        double monthlyInterestRate = yearlyInterestRate/MONTHS_IN_YEAR/100;
        double futureValue = 0;
        for (int i = 1; i <= months; i++)
        {
            futureValue = (futureValue + monthlyPayment) *
            (1 + monthlyInterestRate);
        }
        return futureValue;
    }
}

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