160 likes | 298 Views
Tips for P2. CMSC 331 Summer 2006 Shon Vick. Enums – New to 1.5. What’s wrong with the 1.4 approach?. // int Enum Pattern - has severe problems! public static final int SEASON_WINTER = 0; public static final int SEASON_SPRING = 1; public static final int SEASON_SUMMER = 2;
E N D
Tips for P2 CMSC 331 Summer 2006 Shon Vick
Enums – New to 1.5 • What’s wrong with the 1.4 approach? // int Enum Pattern - has severe problems! public static final int SEASON_WINTER = 0; public static final int SEASON_SPRING = 1; public static final int SEASON_SUMMER = 2; public static final int SEASON_FALL = 3; • Not typesafe • No namespace • Brittleness • Printed values are uninformative
Example import java.util.*; public class Card { public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } private final Rank rank; private final Suit suit; private Card(Rank rank, Suit suit) { this.rank = rank; this.suit = suit; } … }
Example (Cont) public Rank rank() { return rank; } public Suit suit() { return suit; } public String toString() { return rank + " of " + suit; } private static final List<Card> protoDeck = new ArrayList<Card>(); // Initialize prototype deck static { for (Suit suit : Suit.values()) for (Rank rank : Rank.values()) protoDeck.add(new Card(rank, suit)); } public static ArrayList<Card> newDeck() { return new ArrayList<Card>(protoDeck); // Return copy of prototype deck } } Static Initialization Notice for loop over values
Enums are like classes (somewhat) public enum Planet { MERCURY (3.303e+23, 2.4397e6), VENUS (4.869e+24, 6.0518e6), EARTH (5.976e+24, 6.37814e6), MARS (6.421e+23, 3.3972e6), JUPITER (1.9e+27, 7.1492e7), SATURN (5.688e+26, 6.0268e7), URANUS (8.686e+25, 2.5559e7), NEPTUNE (1.024e+26, 2.4746e7), PLUTO (1.27e+22, 1.137e6); … }
Rest of class … private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } public double mass() { return mass; } public double radius() { return radius; } // universal gravitational constant (m3 kg-1 s-2) public static final double G = 6.67300E-11; public double surfaceGravity() { return G * mass / (radius * radius); } public double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); } } MERCURY (3.303e+23, 2.4397e6)
Regular Expressions • Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set. • Regular expressions range can be simple or complex but for P2 they are simple
Basic Classes • The java.util.regex package consists of three classes: Pattern, Matcher, and PatternSyntaxException. • A Pattern object is a compiled representation of a regular expression. The Pattern class provides no public constructors. To create a pattern, you must call one of its public static compile methodsthese methods since they accept a regular expression (a string) as their first argument. • A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by calling the public matcher method on a Pattern object. • A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern
A Simple Example import java.io.*; import java.util.regex.*; public final class RegexTestHarness { private static String REGEX; private static String INPUT; private static BufferedReader br; private static Pattern pattern; private static Matcher matcher; private static boolean found; public static void main(String[] argv) { initResources(); processTest(); closeResources(); } ..
Continuing private static void initResources() { try { br = new BufferedReader(new FileReader("regex.txt")); } catch (FileNotFoundException fnfe) { System.out.println("Cannot locate input file! "+fnfe.getMessage()); System.exit(0); } try { REGEX = br.readLine(); INPUT = br.readLine(); } catch (IOException ioe) {} pattern = Pattern.compile(REGEX); matcher = pattern.matcher(INPUT); System.out.println("Current REGEX is: "+REGEX); System.out.println("Current INPUT is: "+INPUT); }
Finally private static void processTest() { while(matcher.find()) { System.out.println("I found the text \"" + matcher.group() + "\" starting at index " + matcher.start() + " and ending at index " + matcher.end() + "."); found = true; } if(!found){ System.out.println("No match found."); } } private static void closeResources() { try{ br.close(); }catch(IOException ioe){} }
Character Classes See http://java.sun.com/docs/books/tutorial/extra/regex/char_classes.html
Predefined Character Classes (See http://java.sun.com/docs/books/tutorial/extra/regex/pre_char_classes.html)
Quantifiers How would I encode a char following by any number of chars or digits? (See http://java.sun.com/docs/books/tutorial/extra/regex/quant.html)
So what? public enum TokenType { NUMBER (Pattern.compile(REnumber)), ID (Pattern.compile(REnumber(Reid)) COS (Pattern.compile(REnumber(RE cos), SIN (Pattern.compile(REnumber(RE sin)), …. String pattern; Token (Pattern pattern) { this. Pattern = pattern; } Token makeToken (String input) { //… } } public enum TokenType { NUMBER (REnumber), ID (REid) , COS (RE cos), SIN (RE sin), …. String pattern; Token (String pattern) { this. Pattern = pattern; } Token makeToken (String input) { //… } }
References • http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html • http://java.sun.com/docs/books/tutorial/extra/regex/index.html