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

Java Code a class called RecursiveMultiply in which it will accept an integer en

ID: 3838505 • Letter: J

Question

Java

Code a class called RecursiveMultiply in which it will accept an integer entry from user; pass this integer to a recursive method that returns the multiplication result of all integers from 1 up to the number passed to the method. For example, if the integer is 20, the recursive method will return the result of 1 * 2 * 3 * 4 * ...* 20.

You will write the application with GUI and recursion to display the result, based on the user’s entry from a text filed, while an OK button is pressed. You may display the answer in a non-editable text field.

You will make your own decision if there is any explanation that is not described in this specification.

Finally, code a driver program that will test your class RecursiveMultiply.

Explanation / Answer


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

class RecursiveMultiply extends JFrame {

    //Class Declarations
    JTextField jtfText1, jtfUneditableText;
    int result = 0;
    TextHandler handler = null;
    JButton okButton;

    //Constructor
    public RecursiveMultiply() {
        super("Factorial of Number");
        Container container = getContentPane();
        container.setLayout(new FlowLayout());
        jtfText1 = new JTextField(10);
        jtfUneditableText = new JTextField("", 20);
        jtfUneditableText.setEditable(false);
        okButton = new JButton("Ok");
        container.add(jtfText1);
        container.add(okButton);
        container.add(jtfUneditableText);
        handler = new TextHandler();
        okButton.addActionListener(handler);
        setSize(325, 100);
        setVisible(true);
    }
    //Inner Class TextHandler

    private class TextHandler implements ActionListener {
        /* Here i am considering a double value in order to print the factorial of larger value. If u wish you can take integer instead of double */
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == okButton) {
                double value = Double.parseDouble(jtfText1.getText());
                jtfUneditableText.setText(String.valueOf(fact(value)));
            }
        }
    }

    private double fact(double n) {
        if (n == 0 || n == 1) {
            return 1;
        } else {
            return n * fact(n - 1);
        }

    }

}// End of class Factorial

//Driver class to test the requiremrnt
public class Demo {
    //Main Program that starts Execution

    public static void main(String[] args) {
        RecursiveMultiply fact = new RecursiveMultiply();
        fact.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

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