Download presentation
Presentation is loading. Please wait.
1
Dialogues and Wrapper Classes
Chapter 2 - Extra 2/11/16 1 1 1
2
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
3
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
4
JOptionPane Class JOptionPane class allows you to quickly display a dialogue box. Must be imported: import javax.swing.JOptionPane;
5
Message Dialogue ShowMessageDialog method displays a message dialogue.
JoptionPane.showMessageDialog(null, ”Eggs aren't supposed to be green.”);
6
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
7
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.”);
8
import javax.swing.JOptionPane;
import java.text.DecimalFormat; public class AcreConversion { public static void main(String[] args) { final double SQ_FT_TO_ACRES = ; 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"); }
9
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); }
10
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
11
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.
12
Class DecimalFormat Class DecimalFormat enables control of output
In java.text package. Specify a string pattern to describe desired format double val = ; 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
13
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
14
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
15
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.
16
Wrapper classes Integer for the int type Double for the double type
Mostly used for the methods, like Double.parsedouble( )
17
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;
18
Unboxing Opposite of autoboxing. Converting wrapper class object to a primitive type. Integer myInt = 5;//Autobox 5 int primitiveNo; primitiveNo = myInt;//Unbox object
19
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);
20
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.