# Methods. Read two numbers, price and paid (via JOptiondialog) Calculate change; s = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s);

## Presentation on theme: "Methods. Read two numbers, price and paid (via JOptiondialog) Calculate change; s = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s);"— Presentation transcript:

Methods

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

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

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

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) { }

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

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?

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)

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

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); 24.87 String s2=“32.8X7”; Check(s2); 32.8x7

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()

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); }

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

Where do we place methods? /** * @param 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 { }

Download ppt "Methods. Read two numbers, price and paid (via JOptiondialog) Calculate change; s = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s);"

Similar presentations