180 likes | 309 Views
The for Statement. for ( int i = 1; i <= n; i ++) { double interest = balance * rate / 100; balance = balance + interest; } . Nested Loops. The body of the loop contains another loop Each time through the outer loop, the inner loop goes through its full set of iterations.
E N D
TheforStatement for (inti = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; }
Nested Loops The body of the loop contains another loop • Each time through the outer loop, the inner loop goes through its full set of iterations
Nested loop design Example X=6; while (x >=4) { y = 3; while (y <=6) { y++; System.out.println(y + “ “ + x); } x--; }
Let’s Do This Together! • Write a method to: • Receive an int number in a method called count • Using a for loop, print asterisks across the console, the number of asterisks to be the passed number
Screen Press Button (Buttons other objects generate events: Source) Event Notification Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Event Driven Programs OS Interrupt Generates an Event Object e.g. button Listener or middleman (interface) Event Handler Methods
GUI Components and Screen Design Panel Instance Frame Instance GUI Demo Textfield Instance Message Hello World Label Instance Button Instance Display Clear Close
JButtonbutton = new JButton("Add Interest"); JLabellabel = new JLabel("balance: " + account.getBalance()); JPanelpanel = new JPanel() or extends JPanel; panel.add(button); panel.add(label); frame.add(panel); (or the class that extends a panel) frame.add (aClass);
Building Applications with Buttons • class AddInterestListener implements ActionListener { public void actionPerformed(ActionEvent event) { double interest = account.getBalance() * INTEREST_RATE / 100; bank.deposit(interest); label.setText("balance=" + account.getBalance()); } }
STEPS IN THE PROCESS • Add the class within the original class where buttons are activated • Create a global listener reference: • ActionListener listener; • In the constructor • listener = new AddInterestListener (); • button1.addActionListener(listener ); • this.add(button1);
InvestmentViewer.java (note changes from text) • package investment; • import javax.swing.Jframe; • public class InvestmentViewer • { • public static void main (String [] args) • { • JFrame frame = newJFrame ("Investment Viewer"); • frame.setSize(30, 200); • Investment inv = new Investment (); • frame.add (inv); • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); • frame.setVisible(true); • } • }
package investment; • import java.awt.event.ActionEvent; • import java.awt.event.ActionListener; • import javax.swing.JButton; • import javax.swing.JLabel; • import javax.swing.Jpanel; • public class Investment extends • JPanel • { • private JButton button; • private JLabel label, ansLabel; • private BankAccount bank; • private final int INTEREST_RATE • = 7; • ActionListener listener = new • AddInterestListener (); • public Investment () • { • bank = new BankAccount (); • getPanelReady (); • } • public void getPanelReady () { • button = new JButton ("Sample • Button"); • button.addActionListener(listener); • label = new JLabel ("GJY"); • ansLabel = new JLabel (); • add(button); • add(label); • add(ansLabel); • } • class AddInterestListener implements • ActionListener { • public void actionPerformed • (ActionEvent arg0) { • double interest; • interest = bank.getBalance () + • (INTEREST_RATE / 100.0); • ansLabel.setText("Balance: " + • interest); • } • } • }
LAB: (Homework through the week was: Chapter 9 : Exercise P14) Lab: P9. 15
Mouse Events public interface MouseListener { void mousePressed(MouseEvent event); // Called when a mouse button has been pressed on a component void mouseReleased(MouseEvent event); // Called when a mouse button has been released on a component void mouseClicked(MouseEvent event); // Called when the mouse has been clicked on a component void mouseEntered(MouseEvent event); // Called when the mouse enters a component
Mouse Events • void mouseExited(MouseEvent event); // Called when the mouse exits a component } • mousePressed, mouseReleased: called when a mouse button is pressed or released • mouseClicked: if button is pressed and released in quick succession, and mouse hasn't moved • mouseEntered, mouseExited: mouse has entered or exited the component's area
Mouse Events • Add a mouse listener to a component by calling the addMouseListenermethod:public class MyMouseListener implements MouseListener{ // Implements five methods } MouseListener listener = new MyMouseListener();component.addMouseListener(listener); • Sample program: enhance RectangleComponent – when user clicks on rectangle component, move the rectangle