Use advanced GUI concepts in the recursion (largest number) import java.util.Sca
ID: 3657079 • Letter: U
Question
Use advanced GUI concepts in the recursion (largest number) import java.util.Scanner; import java.io.*; import javax.swing.*; import java.awt.*; public class maxElement extends JFrame { private JPanel panel; private JLabel messageLabel_highestnum; private JTextField highestnumTextField; private JButton thelargestofsix; private final int WINDOW_WIDTH = 300; private final int WINDOW_HEIGHT = 250; public maxElement() { setTitle("Highest Number"); setSize (WINDOW_WIDTH, WINDOW_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buildPanel(); addPanel(); add(panel); setVisible (true); } private void buildPanel () { messageLabel_highestnum = new Jlabel ("Enter 6 numbers"); highestnumTextField = new JTextField(12); thelargestofsixButton = new JButton("Click to see the largest number."); thelargestofsixButton.addActionListener (new Outcome()); panel = new JPanel (); panel.add(messageLabel_highestnum); panel.add(highestnumTextField); panel.add(thelargestofsix); } public class Outcome implements ActionListener { public void actionPerformed (ActionEvent e); public static int largest_in_array(int[] a, int n) { if (n == 1) return 0; else return Math.max(a[n-1], largest_in_array(a,n-1)); } public static void main(String[] args) { int[] collection = new int[6]; Scanner sc = new Scanner(System.in); System.out.println("Please enter 6 numbers: "); for (int i = 0; i < collection.length; i ++) { collection[i] = sc.nextInt(); } System.out.println(); System.out.println("The largest number in your collection is: "); System.out.println(largest_in_array(collection,6)); } } }Explanation / Answer
while (e = eventQueueCheck()) { if (e.hasEvent) { processEvent(e) } else { idleProcess() // garbage collection, etc. or perhaps sleep(0.01) // or noop() or the like, depending on environment. } } Example "Linda", demonstrating dual use (input and output) of array/structure. event click_button_foo(info[]) { if (info[x_rel_cordinate] > 55) { info['error_msg'] = 'You clicked the very edge of the button'; } else { process_button... } return(info); // perhaps unneeded if passed by reference } // OO-style syntax event click_button_foo(info) { if (info.x_rel_cordinate > 55) { info.error_msg = 'You clicked the very edge of the button'; } else { process_button... } return(info); // perhaps unneeded if passed by reference } Note that "rel_cordinate" stands for "relative coordinate", which is the coordinate relative to the target widget (button in this case). That way if the screen is rearranged, we less likely have to change the value. (Using coordinates is perhaps a risky approach anyhow because we may resize the button, but this is just an illustrative example, and sometimes there are no alternatives.) I have tentatively decided that the best way to standardize the event interface is to pass in two by-reference associative arrays (or objects for OO fans): event foo_activity(eventStruct[], contextStruct[]) { ... if (someError) { eventStruct.errorMessage = "you can't click here after midnight." } ... }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.