1 / 16

JAVA Useful Classes and Techniques

JAVA Useful Classes and Techniques. CMSC 331. Enums. An enum type is a type whose fields consist of a fixed set of constants. Common examples: directions (values of NORTH, SOUTH, EAST, and WEST) days of the week

Download Presentation

JAVA Useful Classes and Techniques

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. JAVAUseful Classes and Techniques CMSC 331

  2. Enums • An enum type is a type whose fields consist of a fixed set of constants. Common examples: • directions (values of NORTH, SOUTH, EAST, and WEST) • days of the week • In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type: public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

  3. Using Enums public class EnumTest { Day day; public EnumTest(Day day) { this.day = day; } public void tellItLikeItIs() { switch (day) { case MONDAY: System.out.println("Mondays stink."); case SATURDAY: case SUNDAY: System.out.println("Weekends are best."); break; default: System.out.println("Midweek days are so-so."); break; } }

  4. Using Enums (Cont) public static void main(String[] args) { EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellItLikeItIs(); EnumTest seventhDay = new EnumTest(Day.SUNDAY); seventhDay.tellItLikeItIs(); }

  5. C+ printf i/o for (Planet p : Planet.values()) { System.out.printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass)); } New style for loop Defining an Enum 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); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } private double mass() { return mass; } private double radius() { return radius; } // universal gravitational constant (m3 kg-1 s-2) public static final double G = 6.67300E-11; double surfaceGravity() { return G * mass / (radius * radius); } double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); } } • In Java enums declaration define a class (called an enum type). • The enum class body can include methods and other fields. Constructor Method

  6. What Are Regular Expressions? • Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set. • They can be used to search text and for pther purposes • You must learn a specific syntax to create regular expressions — one that goes beyond the normal syntax of the Java programming language. • Regular expressions regular are supported by the java.util.regex API • There are many variants for the syntax of regular expressions such as grep, Perl, Tcl, Python, PHP, and awk. The regular expression syntax in the java.util.regex API is most similar to that found in Perl

  7. Java Regex classes • The java.util.regex package primarily 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 first invoke one of its public static compile methods, which will then return a Pattern object. These methods accept a regular expression as the first argument; the first few lessons of this trail will teach you the required syntax. • 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 invoking the matcher method on a Pattern object. • A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.

  8. REGEXP Example import java.io.Console; import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexTestHarness { public static void main(String[] args){ Console console = System.console(); if (console == null) { System.err.println("No console."); System.exit(1); } while (true) { Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: ")); Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: ")); boolean found = false; while (matcher.find()) { console.format("I found the text \"%s\" starting at " + "index %d and ending at index %d.%n", matcher.group(), matcher.start(), matcher.end()); found = true; } if(!found){ console.format("No match found.%n"); } } } } Compile the REGEX pattern Get matcher from static method on Pattern class Seed the matcher with the string that you want to find matches Find parts of the string that match the REGEXP Find the group ofchars that matched (as a String) and where those chars started and ended in the string to be matched

  9. REGEXP Character Classes Enter your regex: [bcr]at Enter input string to search: bat I found the text "bat" starting at index 0 and ending at index 3. Enter your regex: [bcr]at Enter input string to search: cat I found the text "cat" starting at index 0 and ending at index 3. Enter your regex: [bcr]at Enter input string to search: rat I found the text "rat" starting at index 0 and ending at index 3. Enter your regex: [bcr]at Enter input string to search: hat No match found

  10. Negation and Ranges Enter your regex: [^bcr]at Enter input string to search: bat No match found. Enter your regex: [^bcr]at Enter input string to search: cat No match found. Enter your regex: [a-c] Enter input string to search: a I found the text "a" starting at index 0 and ending at index 1. Enter your regex: foo[1-5] Enter input string to search: foo1 I found the text "foo1" starting at index 0 and ending at index 4. Enter your regex: foo[^1-5] Enter input string to search: foo1 No match found.

  11. Predefined Character Classes Any character (may or may not match line terminators) \d A digit: [0-9] \D A non-digit: [^0-9] \s A whitespace character: [ \t\n\x0B\f\r] \S A non-whitespace character: [^\s] \w A word character: [a-zA-Z_0-9] \W A non-word character: [^\w]

  12. Other functionality • There is a lot more to the regular expression functionality than what is shown here. • Consult the reference given at the back of the lecture as well as the Java API for more details

  13. Matcher Methods • A matcher is created from a pattern by invoking the pattern's matcher method. Once created, a matcher can be used to perform three different kinds of match operations: • The matches method attempts to match the entire input sequence against the pattern. • The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern. • The find method scans the input sequence looking for the next subsequence that matches the pattern

  14. public boolean lookingAt() • Attempts to match the input sequence, starting at the beginning, against the pattern. • Like the matches method, this method always starts at the beginning of the input sequence; unlike that method, it does not require that the entire input sequence be matched. • If the match succeeds then more information can be obtained via the start, end, and group methods. • Returns: • true if, and only if, a prefix of the input sequence matches this matcher's pattern

  15. References • http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html • http://java.sun.com/docs/books/tutorial/essential/regex/intro.html

More Related