Dialogues and Wrapper Classes Chapter 2 - Extra 2/11/16 1 1 1
Today's Topics Use Decimal Format to control output. Use Dialog Boxes for messages and input. Use wrapper classes to convert primitives to objects Autoboxing and unboxing saves conversion work. 2 2 2
Dialogue Boxes Message Dialogue Displays message and OK button to close it. Input Dialogue Prompts user for input. Provides text box for input. OK and Cancel buttons to control it. 3 3 3
JOptionPane Class JOptionPane class allows you to quickly display a dialogue box. Must be imported: import javax.swing.JOptionPane;
Message Dialogue ShowMessageDialog method displays a message dialogue. JoptionPane.showMessageDialog(null, ”Eggs aren't supposed to be green.”);
import javax.swing.JOptionPane; public class EggsMessage { public static void main(String[] args) { JoptionPane.showMessageDialog(null, "Eggs aren't supposed to be green."); } 6 6 6
Input Dialog Inputs data as String, so we need to use a String variable to store it. String name; name = JoptionPane.showInputDialogue (“Enter your name.”);
import javax.swing.JOptionPane; import java.text.DecimalFormat; public class AcreConversion { public static void main(String[] args) { final double SQ_FT_TO_ACRES = 43560.0; String strSqFt; //Input the square feet strSqFt = JOptionPane.showInputDialog("Input Square Feet."); //Convert sq ft to number double sqFt = Double.parseDouble(strSqFt); //Figure acres double acres = sqFt/SQ_FT_TO_ACRES; //Output acres DecimalFormat formatter = new DecimalFormat("00.##"); JOptionPane.showMessageDialog(null, formatter.format(acres) + " acres"); }
import javax.swing.JOptionPane; public class AskForName { public static void main(String[] args) { String name; name = JOptionPane.showInputDialog("Enter your name."); JOptionPane.showMessageDialog(null,"Hey, " + Name + "!"); System.exit(0); }
Numeric Input Input dialog only handles String. Numeric values must be converted. Wrapper classes have methods to convert from String to numeric type. Integer.parseInt(“123”) → 123 Double.parseDouble(“4.56”) → 4.56
Write a Program An acre of land is equivalent to 43,560 square feet of land. Write a program that calculates the number of acres in a tract of land. Use an input dialog to ask the user for the square footage of the land. Use an output dialog to output the result. Could use DecimalFormat class to control output.
Class DecimalFormat Class DecimalFormat enables control of output In java.text package. Specify a string pattern to describe desired format double val = 1.23456; DecimalFormat formatter = new DecimalFormat("0.000"); System.out.println(formatter.format(val)); Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Class DecimalFormat 0 – digit, leading and trailing zeros # -- digit, no leading and trailing zeros % -- percentage $ -- at beginning displays dollar sign Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Class DecimalFormat Figure 4-7 Examples of patterns used with class DecimalFormat Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
More on Wrapper Classes A wrapper class is a class that is “wrapped around” a primitive data type. Allows you to create objects instead of primitive variables. Not used a lot.
Wrapper classes Integer for the int type Double for the double type Mostly used for the methods, like Double.parsedouble( )
Autoboxing Can create objects of a wrapper class by passing a primitive to a constructor Integer number = new Integer(7); Another way is to initialize object with the number, autoboxing happens. Integer number; Number = 7;
Unboxing Opposite of autoboxing. Converting wrapper class object to a primitive type. Integer myInt = 5;//Autobox 5 int primitiveNo; primitiveNo = myInt;//Unbox object
Wrapper Classes ArrayList, for example, only takes objects. ArrayList<Integer> list = new ArrayList<Integer>; Can wrap ints to put them on the list. Integer num5 = new Integer(5); list.add(num5); Not really necessary because autoboxing takes care of the conversion. list.add(5);
Participation Write a java program to use an input dialog to ask the user for his/her current annual salary. Give them a 5% raise. Use a message dialog to output the new salary. Use a DecimalFormat to control the look of the output.