0% found this document useful (0 votes)
16 views18 pages

Slot 3 Review

The document provides a review of key programming concepts including classes, methods, OOP principles, regular expressions, exception handling, and basic I/O in Java. It details the structure and usage of regular expressions, including metacharacters, quantifiers, and boundary matchers, as well as the distinction between checked and unchecked exceptions. Additionally, it covers file handling for text and binary files.

Uploaded by

kanyken1210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views18 pages

Slot 3 Review

The document provides a review of key programming concepts including classes, methods, OOP principles, regular expressions, exception handling, and basic I/O in Java. It details the structure and usage of regular expressions, including metacharacters, quantifiers, and boundary matchers, as well as the distinction between checked and unchecked exceptions. Additionally, it covers file handling for text and binary files.

Uploaded by

kanyken1210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SLOT 3 REVIEW

CONTENT

• Class & Class Components


• Variables & Methods
• OOP Concepts: Abstraction, Encapsulation, Inheritance, Polymorphism
• Collections
• Regular expressions
• Exception handling
• Basic I/O

2
REGULAR
EXPRESSIONS

3
REGULAR EXPRESSIONS

• A regular expression is simply text.


• It may contain characters or character combinations that have special meaning.
o These are called metacharacters.
• These combinations are interpreted by a regular expression pattern processor.
• Some common uses of regular expressions.
o Verify something is formatted correctly.
o Find occurrences of patterns in text.
o Replace matching occurrences of patterns in text.
o Extract matching occurrences from the text.
o Split your text by a pattern.

4
REGULAR EXPRESSIONS in JAVA

• There are classes with methods that take regular expression strings or patterns as
parameters.
o String, Scanner, Formatter, DateTimeFormatter, Duration.

String's methods which use regular expressions

Result Method Name


boolean matches(String regex)
String replaceAll(String regex, String replacement)
String replaceFirst(String regex, String replacement)
String[] split(String regex)
String[] split(String regex, int limit)
5
REGULAR EXPRESSION CONSTRUCTS

A regular expression can be made up of combinations of the following:


• Literals – have no additional meaning & are a one-to-one match.
• Character classes – some are predefined, others you can define.
• Quantifiers – metacharacters which identify the number of occurrences of a
character class or literal, required to make a match.
• Boundary matchers, or anchors – specify the position in the text (e.g., at the start
of the text or the end).
• Groups – identify and allow for the capturing of subexpressions.

6
REGULAR EXPRESSION CONSTRUCTS

Some common metacharacters that fall into these categories

Type Examples
Character Classes . [abc] [a-g] [A-Z] [0-9] [^abc] \d \s \w
Quantifiers * + ?
Boundary matchers ^ $ \b
(or anchors)
Groups ()

You can find these examples and more by looking at Java's Pattern Class API

7
QUANTIFIERS

Six different quantifiers regular expressions

Quantifier Meaning Pattern Example Match Examples


* pattern appears zero or more times b* empty string, b, bb, bbb
+ pattern appears one or more times b+ b, bb, bbb
? pattern appears zero or one time colou?r color, colour
{n} pattern must appear exactly n times b{3} bbb
{ n, } pattern must appear at least n times b{2,} bb, bbb, bbbb
{ n, m } pattern must appear at least n but not b{3, 4} bbb, bbbb
more than m times

8
BOUNDARY MATCHERS

Three common boundary matchers or anchors.

Metacharacter Meaning Pattern Example Match Notes


^ matches to start of text "^." Matches first character
in a string
$ matches to end of text ".$" Matches last character
in a string
\b matches to word "\\b" Matches first word in a
string.

9
EXAMPLE

public static void findAndDisplayResult(String type, Matcher matcher){


Pattern phonePattern =
System.out.println("\n" + type);
Pattern.compile("\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}");
boolean found = false;
while (matcher.find()) {
System.out.printf("I found the text" +
" \"%s\" starting at " +
"index %d and ending at index %d.%n",
String input = "ababab";
matcher.group(),
matcher.start(),
// Greedy quantifier
matcher.end());
Pattern greedyPattern = Pattern.compile(".*b");
found = true;
Matcher
} greedyMatcher = greedyPattern.matcher(input);
findAndDisplayResult("Greedy
if(!found){ quantifier", greedyMatcher);
System.out.println("No match found.%n");
}
}

10
EXCEPTION
HANDLING

11
TYPES OF EXCEPTION

• Exceptions are unexpected events or errors that occur during the execution of a
program. Two main types of exceptions in Java: checked exceptions and
unchecked exceptions.
1. Checked Exceptions:
1. These are exceptions that are checked at compile time. They are subclasses of
Exception but not subclasses of RuntimeException.
2. Examples: IOException, SQLException
2. Unchecked Exceptions:
1. These are exceptions that are not checked at compile time. They are subclasses of
RuntimeException.
2. Examples: ArithmeticException, NullPointerException

12
CHECKED EXCEPTIONS

• A Checked exception must be caught, or specified in the containing method's


throws clause
• Two options:
1. Wrap the statement that throws a checked exception, in a try-catch block, and
then handle the situation in the catch block.
2. Or, alternately, you can change the method signature, declaring a throws clause,
and specifying this exception type.

13
finally CLAUSE

• A finally clause is used in conjunction with a try statement.


• A traditional try statement requires either a catch or a finally clause, or can include
both.
• The finally clause is always declared after the catch block if one is declared.
• The finally block's code is always executed, regardless of what happens in the try
or catch blocks.
• The finally block does not have access to either the try or catch block's local
variables.

14
CheckedExample.java
Sample code
CheckedExample.java

15
FILE I/O

16
TEXT FILES

17
BINARY FILES

18

You might also like