Download presentation

Presentation is loading. Please wait.

Published byEsther Michaels Modified over 2 years ago

1
Methods

2
Read two numbers, price and paid (via JOptiondialog) Calculate change; s = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s); s = JOptionPane.showInputDialog("Enter paid amount”); Double paid = Double.parseDouble(s); Double change = paid – price; JOptionPane.showMessageDialog(null, “change is: “ + change);

3
Is this program segment correct What are the rules? – s may have only digits and at most one decimal point. s = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s); s = JOptionPane.showInputDialog("Enter paid amount”); Double paid = Double.parseDouble(s); Double change = paid – price; JOptionPane.showMessageDialog(null, “change is: “ + change);

4
Is every character of s a digit? – Check the first character If it is not a digit – Stop – not a number If it is a digit – So far so good Check the next character the same way … How many times do you do this for? – One time for each character How many characters are in s? – s.length() characters

5
Is every character of s a digit? 1.Start from the first character of s int i=0 2.Check the next character of s if (Character.isDigit(s.charAt(i)) 3.Pick the next character of s i=i+1; 4.Repeat steps 2-3 until you check the last character Repeat as long as i < s.length() for (int i=initialValue; condition; step) { } for (int i=0; i < s.length(); i=i+1) { }

6
Is every character of s a digit? String s1 = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s1); for (int i=0; i < s1.length(); i=i+1) if (!Character.isDigit(s1.charAt(i))) { JOptionPane.showMessageDialog(null, "You must enter an integer value"); System.exit(0); } String s2 = JOptionPane.showInputDialog("Enter paid amount”); Double paid = Double.parseDouble(s2); Double change = paid – price; JOptionPane.showMessageDialog(null, “change is: “ + change);

7
Is every character of s a digit? String s1 = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s1); for (int i=0; i < s1.length(); i=i+1) if (!Character.isDigit(s1.charAt(i))) { JOptionPane.showMessageDialog(null, "You must enter an integer value"); System.exit(0); } String s2 = JOptionPane.showInputDialog("Enter paid amount”); Double paid = Double.parseDouble(s2); for (int i=0; i < s2.length(); i=i+1) if (!Character.isDigit(s2.charAt(i))) { JOptionPane.showMessageDialog(null, "You must enter an integer value"); System.exit(0); } Double change = paid – price; JOptionPane.showMessageDialog(null, “change is: “ + change); Same code!! What if we factor out the common code, give it a name and refer to it by its name when needed?

8
Methods Common and frequently used that perform a well-defined task are factored out into program segments called methods for (int i=0; i < s.length(); i=i+1) if (!Character.isDigit(s.charAt(i))) { JOptionPane.showMessageDialog(null, "You must enter an integer value"); System.exit(0); } Check(String s)

9
Methods void check (String s) { for (int i=0; i < s.length(); i=i+1) if (!Character.isDigit(s.charAt(i))) { JOptionPane.showMessageDialog(null, "You must enter an integer value"); System.exit(0); } String s1 = JOptionPane.showInputDialog("Enter price”); check(s1); Double price = Double.parseDouble(s1); String s2 = JOptionPane.showInputDialog("Enter paid amount”); check(s2); Double paid = Double.parseDouble(s2); Double change = paid – price; JOptionPane.showMessageDialog(null, “change is: “ + change);

10
Methods void check (String s) { for (int i=0; i < s.length(); i=i+1) if (!Character.isDigit(s.charAt(i))) { JOptionPane.showMessageDialog(null, "You must enter an integer value"); System.exit(0); } String s1=“24.87”; Check(s1); String s2=“32.8X7”; Check(s2); 32.8x7

11
Is there at most at one decimal point? 1.Start from the first character of s int i=0 boolean decimalPoint=false; 2.Check the next character of s if (s.charAt(i) == '. ' ) Check if this is the first decimal point If (!decimalPoint) Remember that we have seen a decimalpoint decimalPoint=true; 3.Repeat steps 2-3 until you check the last character Repeat as long as i < s.length()

12
Is there at most at one decimal point? void check (String s) { boolean decimalPoint=false; for (int i=0; i < s.length(); i=i+1) if (s.charAt(i) == '. ') if (!decimalPoint) decimalPoint=true; else { JOptionPane.showMessageDialog(null, "You must enter an integer value"); System.exit(0); }

13
Is every character a digit and Is there at most at one decimal point? void check (String s) { boolean decimalPoint=false; for (int i=0; i < s.length(); i=i+1) if (s.charAt(i) == '. ') if (!decimalPoint) decimalPoint=true; else { JOptionPane.showMessageDialog(null, "You must enter an integer value"); System.exit(0); } void check (String s) { for (int i=0; i < s.length(); i=i+1) if (!Character.isDigit(s.charAt(i))) { JOptionPane.showMessageDialog(null, "You must enter an integer value"); System.exit(0); } Combine the two check methods

14
Where do we place methods? /** args the command line arguments */ public static void main(String[] args) { // TODO code application logic here ….. } public static void check(String s) { ….. } public class Assignment1 { }

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google