1 / 7

Java Enums

Java Enums. Why have Choice.Yes and Choice.No ?. Old Way. Enumerated types consist of a set of named values Example: Font has BOLD, ITALIC rather than 1, 2 // From class Font: public static int BOLD = 1; public static int ITALIC = 2;

ednat
Download Presentation

Java Enums

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Enums Why have Choice.Yes and Choice.No?

  2. Old Way • Enumerated types consist of a set of named values • Example: Font has BOLD, ITALIC rather than 1, 2 // From class Font: public static intBOLD = 1; public static intITALIC = 2; • It's easier to remember Font.BOLD than 1 • Font.BOLDin action JButton button = new JButton("Courier 24 point Bold"); button.setFont(new Font("Courier", 24, Font.BOLD));

  3. An int is an int • private static ints are also know as "glorified integers" • This can lead to problems • Can you read the text in the button with a point size of 1? • Since Font.BOLD is just an int you can pass in any other int value, like 2314 • And since the style Font.BOLD is compatible with point size (both int type parameters), you get one (1) point font like above • Swap the arguments to get what was intended see next slide

  4. Would be nice to have different type • Since Font.BOLD is just an int, you can pass in any other int value, like 2314 or -123 • And since the style Font.BOLD is compatible with point size (both int type parameters), you get one (1) point font like above • Swap the arguments to get what was intended button.setFont(new Font("Courier",24, Font.BOLD)); button.setFont(new Font("Courier", Font.BOLD, 24));

  5. New Way • Use the Java enum type like this public enumFontStyle{BOLD, ITALIC}; • The previous attempt to construct a Font would preferable be a compile-time error • With an enum type, we get an improved constructor public Font(String name, FontStyle style, intsize)

  6. An Enum used in GameTree • playerSelectedusing a String parameter could result in code like this public void playerSelected(String yesOrNo) { if (yesOrNo.equals("Yes")) ; // go left else if (yesOrNo.equals("No")) ; // go right else JOptionPane.showMessageDialog(null, "Contact vendor!"); } • Just hope the user never types "Y", "yes" or "y" • and the programmer used equals, not ==

  7. Must use a Choice argument • Here is the actual design of playerSelected /** * Ask the game to update the current node by going left for * Choice.yes or right for Choice.no Example code: * theGame.playerSelected(Choice.Yes); * * @paramyesOrNo */ public voidplayerSelected(Choice yesOrNo) { if(yesOrNo == Choice.Yes) ; // go left if (yesOrNo== Choice.No) ; // go right }

More Related