Java Lab Assignment Recursive Multiplication. Write a GUI program that uses a re
ID: 3690831 • Letter: J
Question
Java Lab Assignment
Recursive Multiplication.
Write a GUI program that uses a recursive function. The user will enter a number in a textfield and when a button is clicked, the button will call a function called recursive.
This function accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 7 * 4 = 4 + 4 + 4 + 4 + 4 + 4 + 4 or 7 * 4 = 7 + 7 + 7 + 7
Test your program for negative numbers.
Explanation / Answer
import java.awt.*; import java.awt.event.*; public class Test extends Frame implements ActionListener { private Label xlblCount; private Label ylblCount; private Label rCount; private TextField xtfCount; private TextField ytfCount; private TextField rtfCount; private Button btnCount; private int value = 0; public Test () { setLayout(new FlowLayout()); xlblCount = new Label("Enter x"); add(xlblCount); xtfCount = new TextField("0", 10); add(xtfCount); ylblCount = new Label("Enter y"); add(ylblCount); ytfCount = new TextField("0", 10); add(ytfCount); rtfCount = new TextField("0", 10); rtfCount.setEditable(false); add(rtfCount); btnCount = new Button("Calculate"); add(btnCount); btnCount.addActionListener(this); setTitle("Test"); setSize(450, 200); setVisible(true); } public static void main(String[] args) { Test app = new Test(); } public void actionPerformed(ActionEvent evt) { int x = Integer.parseInt(xtfCount.getText()); int y = Integer.parseInt(ytfCount.getText()); value = cumilate(x,y); rtfCount.setText(value + ""); } public int cumilate(int x,int y){ if(x==0) return 0; else{ return y + cumilate(--x,y); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.