© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.

Slides:



Advertisements
Similar presentations
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
1 Outline Chapter 4 Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Objectives –Display a date and time. –Use a JPasswordField.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 27 - Phone Book Application Introducing Multimedia.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Shipping Hub Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 11.1 Test-Driving the Security Panel Application.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 11.1 Test-Driving the Interest Calculator Application 11.2 Essentials of Counter-Controlled Repetition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement Outline 9.1 Test-Driving the Car Payment Calculator Application.
Java Programming: From Problem Analysis to Program Design,
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator Application 8.2 while Repetition Statement 8.3 Increment and Decrement Operators 8.4 Constructing the Car Payment Calculator Application 8.5Wrap-Up Tutorial 8 – Car Payment Calculator Application Introducing the while Repetition Statement

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Use the while repetition statement to repeatedly execute statements in an application. –Use counter-controlled repetition. –Use the increment and decrement operators. –Display information in JTextArea s.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Car Payment Calculator Application

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 Figure 8.1 Car Payment Calculator application before data has been entered. 8.1 Test-Driving the Car Payment Calculator Application (Cont.) JTextArea component

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 Figure 8.2 Car Payment Calculator application after data has been entered. 8.1 Test-Driving the Car Payment Calculator Application (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 Figure 8.3 Car Payment Calculator application displaying calculation results. 8.1 Test-Driving the Car Payment Calculator Application (Cont.) Results displayed in tabular format

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved while Repetition Statement While there are still items on my shopping list Purchase next item Cross it off my list Repitition statement – repeats actions, depending on the value of a condition Loop-continuation condition –Loop executes while condition remains true Pseudocode int product = 3; while ( product <= 50 ) { product *= 3; } Find the first power of 3 greater than 50

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved while Repetition Statement (Cont.) Figure 8.4 while repetition statement UML activity diagram.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Increment and Decrement Operators Unary increment operator (++) –preincrement –postincrement Unary decrement operator (--) –predecrement –postdecrement

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Increment and Decrement Operators (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application When the user clicks the Calculate JButton Initialize loan length to two years Clear the JTextArea of any previous text Get car price, down payment and annual interest rate Calculate loan amount Calculate monthly interest rate While loan length is less than or equal to five years Calculate number of months Calculate monthly payment based on loan amount, monthly interest rate and loan length in months Display result Increment loan length in years by one year

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.7 Customize a JTextArea component. Customizing paymentsJTextArea

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.8 JTextArea added to Car Payment Calculator application’s JFrame. JTextArea component

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.9 Clearing the contents of a JTextArea. Set JTextArea ’ s text

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.10 Adding a header to a JTextArea. Append text to JTextArea Use the append method to add text to a JTextArea Escape character ( \ ) Combines with the next character to form an escape sequence \t – tab character

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.11 Header displayed in the JTextArea.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.12 Variables for the Car Payment Calculator application. Declaring variables to store the length of the loan and result

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.13 Retrieving input in the Car Payment Calculator application. Variables to store user input

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.14 Determining amount borrowed and monthly interest rate. Calculate loan amount and monthly interest

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.15 Declaring DecimalFormat currency for displaying the result in currency format. Create DecimalFormat for dollar amounts

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.16 Adding the while statement. Insert while statement Counter-controlled repetition –Counter controls the number of times a set of statements will execute –Also known as definite repetition because number of repetitions is known prior to execution

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.17 Converting the loan duration from years to months. Determine number of months in loan period months is set to a different value, depending on which iteration of the loop is being executed

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.18 Method calculateMonthlyPayment returns monthly payment. Calculate monthly payment

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.19 Displaying the number of months and the amount of each monthly payment. Display monthly payment String-concatenation operator Escape sequence \n – newline character

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.20 Incrementing the counter. Increment the years counter The counter is incremented at the end of the loop so the loop will eventually end

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Car Payment Calculator Application (Cont.) Figure 8.21 Running the completed application.

 2004 Prentice Hall, Inc. All rights reserved. Outline 28 CarPayment.java (1 of 8) 1 // Tutorial 8: CarPayment.java 2 // Calculate different billing plans for a car loan. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import java.text.DecimalFormat; 7 8 public class CarPayment extends JFrame 9 { 10 // JLabel and JTextfield for price 11 private JLabel priceJLabel; 12 private JTextField priceJTextField; // JLabel and JTextfield for down payment 15 private JLabel downPaymentJLabel; 16 private JTextField downPaymentJTextField; // JLabel and JTextfield for interest 19 private JLabel interestJLabel; 20 private JTextField interestJTextField; // JButton to initiate calculation 23 private JButton calculateJButton; 24

 2004 Prentice Hall, Inc. All rights reserved. Outline 29 CarPayment.java (2 of 8) 25 // JTextArea to display results 26 private JTextArea paymentsJTextArea; // no-argument constructor 29 public CarPayment() 30 { 31 createUserInterface(); 32 } // create and position GUI components; register event handlers 35 private void createUserInterface() 36 { 37 // get content pane and set layout to null 38 Container contentPane = getContentPane(); 39 contentPane.setLayout( null ); // set up priceJLabel 42 priceJLabel = new JLabel(); 43 priceJLabel.setBounds( 40, 24, 80, 21 ); 44 priceJLabel.setText( "Price:" ); 45 contentPane.add( priceJLabel ); 46

 2004 Prentice Hall, Inc. All rights reserved. Outline 30 CarPayment.java (3 of 8) 47 // set up priceJTextField 48 priceJTextField = new JTextField(); 49 priceJTextField.setBounds( 184, 24, 56, 21 ); 50 priceJTextField.setHorizontalAlignment( JTextField.RIGHT ); 51 contentPane.add( priceJTextField ); // set up downPaymentJLabel 54 downPaymentJLabel = new JLabel(); 55 downPaymentJLabel.setBounds( 40, 56, 96, 21 ); 56 downPaymentJLabel.setText( "Down payment:" ); 57 contentPane.add( downPaymentJLabel ); // set up downPaymentJTextField 60 downPaymentJTextField = new JTextField(); 61 downPaymentJTextField.setBounds( 184, 56, 56, 21 ); 62 downPaymentJTextField.setHorizontalAlignment( 63 JTextField.RIGHT ); 64 contentPane.add( downPaymentJTextField ); // set up interestJLabel 67 interestJLabel = new JLabel(); 68 interestJLabel.setBounds( 40, 88, 120, 21 ); 69 interestJLabel.setText( "Annual interest rate:" ); 70 contentPane.add( interestJLabel ); 71

 2004 Prentice Hall, Inc. All rights reserved. Outline 31 CarPayment.java (4 of 8) 72 // set up interestJTextField 73 interestJTextField = new JTextField(); 74 interestJTextField.setBounds( 184, 88, 56, 21 ); 75 interestJTextField.setHorizontalAlignment( JTextField.RIGHT ); 76 contentPane.add( interestJTextField ); // set up calculateJButton and register its event handler 79 calculateJButton = new JButton(); 80 calculateJButton.setBounds( 92, 128, 94, 24 ); 81 calculateJButton.setText( "Calculate" ); 82 contentPane.add( calculateJButton ); 83 calculateJButton.addActionListener( new ActionListener() // anonymous inner class 86 { 87 // event handler called when user clicks calculateJButton 88 public void actionPerformed( ActionEvent event ) 89 { 90 calculateJButtonActionPerformed( event ); 91 } } // end anonymous inner class ); // end call to addActionListener 96

 2004 Prentice Hall, Inc. All rights reserved. Outline 32 CarPayment.java (5 of 8) 97 // set up paymentsJTextArea 98 paymentsJTextArea = new JTextArea(); 99 paymentsJTextArea.setBounds( 28, 168, 232, 90 ); 100 paymentsJTextArea.setEditable( false ); 101 contentPane.add( paymentsJTextArea ); // set properties of window 104 setTitle( "Car Payment Calculator" ); // set window's title 105 setSize( 288, 302 ); // set window's size 106 setVisible( true ); // display window } // end method createUserInterface // method called when user clicks calculateJButton 111 private void calculateJButtonActionPerformed( ActionEvent event ) 112 { 113 int years = 2; // repetition counter 114 int months; // payment period 115 double monthlyPayment; // monthly payment // clear JTextArea 118 paymentsJTextArea.setText( "" ); 119 Customizing the JTextArea Declaring variables Clearing JTextArea

 2004 Prentice Hall, Inc. All rights reserved. Outline 33 CarPayment.java (6 of 8) 120 // add header JTextArea 121 paymentsJTextArea.append( "Months\tMonthly Payments" ); // retrieve user input 124 int price = Integer.parseInt( priceJTextField.getText() ); 125 int downPayment = 126 Integer.parseInt( downPaymentJTextField.getText() ); 127 double interest = 128 Double.parseDouble( interestJTextField.getText() ); // calculate loan amount and monthly interest 131 int loanAmount = price - downPayment; 132 double monthlyInterest = interest / 1200; // format to display monthlyPayment in currency format 135 DecimalFormat currency = new DecimalFormat( "$0.00" ); // while years is less than or equal to five years 138 while ( years <= 5 ) 139 { 140 // calculate payment period 141 months = 12 * years; 142 Adding header to JTextArea Obtaining user input Calculating loan amount and monthly interest Declaring DecimalFormat

 2004 Prentice Hall, Inc. All rights reserved. Outline 34 CarPayment.java (7 of 8) 143 // get monthlyPayment 144 monthlyPayment = calculateMonthlyPayment( 145 monthlyInterest, months, loanAmount ); // insert result into JTextArea 148 paymentsJTextArea.append( "\n" + months + "\t" currency.format( monthlyPayment ) ); years++; // increment counter } // end while } // end method calculateJButtonActionPerformed // calculate monthlyPayment 158 private double calculateMonthlyPayment( double monthlyInterest, 159 int months, int loanAmount ) 160 { 161 double base = Math.pow( 1 + monthlyInterest, months ); 162 return loanAmount * monthlyInterest / ( 1 - ( 1 / base ) ); 163 } 164 Calling method calculateMonthl yPayment to get monthly payment Incrementing counter

 2004 Prentice Hall, Inc. All rights reserved. Outline 35 CarPayment.java (8 of 8) 165 // main method 166 public static void main( String [] args ) 167 { 168 CarPayment application = new CarPayment(); 169 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class CarPayment