1 / 18

The for Statement

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.

kimama
Download Presentation

The for Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TheforStatement for (inti = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; }

  2. 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

  3. Nested loop design Example X=6; while (x >=4) { y = 3; while (y <=6) { y++; System.out.println(y + “ “ + x); } x--; }

  4. 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

  5. Events and GUI Interfaces

  6. 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

  7. GUI Components and Screen Design Panel Instance Frame Instance GUI Demo Textfield Instance Message Hello World Label Instance Button Instance Display Clear Close

  8. Building Applications with Buttons

  9. 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);

  10. 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()); } }

  11. 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);

  12. 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); • } • }

  13. 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); • } • } • }

  14. LAB: (Homework through the week was: Chapter 9 : Exercise P14) Lab: P9. 15

  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

  16. 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

  17. 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

More Related