/**
* Copyright (C) 2022 by Martin Robillard. See https://codesample.info/about.html
*/
package e2.chapter8;
public class Driver
{
/**
* Sample use of different visitors.
*/
public static void main(String[] args)
{
CardSource root = createSampleSource();
// Basic printing
root.accept(new PrintVisitor());
// Containment checking
ChecksContainmentVisitor visitor = new ChecksContainmentVisitor(Card.get(Rank.ACE, Suit.CLUBS));
root.accept(visitor);
System.out.println(visitor.contains());
CardSequence sequence = new CardSequence(Card.get(Rank.EIGHT, Suit.DIAMONDS));
visitor.reset();
sequence.accept(visitor);
System.out.println(visitor.contains());
// Counts cards
CountingVisitor visitor2 = new CountingVisitor();
root.accept(visitor2);
System.out.println(visitor2.getCount());
visitor2.reset();
sequence.accept(visitor2);
System.out.println(visitor2.getCount());
// Fancy printing
root.accept(new StructurePrinterVisitor());
sequence.accept(new StructurePrinterVisitor());
// Defining an anonymous visitor which prints the number
// of cards in card sequences found embedded in the source.
CardSourceVisitor visitor3 = new AbstractCardSourceVisitor()
{
@Override
public void visitCardSequence(CardSequence pSequence)
{
System.out.println(pSequence.size() + " cards");
}
};
root.accept(visitor3);
}
private static CardSource createSampleSource()
{
CardSequence sequence = new CardSequence(Card.get(Rank.ACE, Suit.CLUBS), Card.get(Rank.ACE, Suit.DIAMONDS),
Card.get(Rank.ACE, Suit.HEARTS), Card.get(Rank.ACE, Suit.HEARTS));
CompositeCardSource composite = new CompositeCardSource(new Deck(), sequence);
return new CompositeCardSource(new Deck(), composite);
}
}
This call will print the following:
SIX of SPADES
THREE of HEARTS
FOUR of SPADES
KING of CLUBS
QUEEN of CLUBS
QUEEN of HEARTS
TWO of SPADES
ACE of SPADES
THREE of SPADES
SEVEN of SPADES
SIX of DIAMONDS
JACK of SPADES
FOUR of DIAMONDS
KING of HEARTS
NINE of SPADES
FOUR of CLUBS
TWO of HEARTS
JACK of CLUBS
QUEEN of SPADES
NINE of CLUBS
EIGHT of CLUBS
TWO of DIAMONDS
NINE of HEARTS
FIVE of CLUBS
QUEEN of DIAMONDS
TEN of DIAMONDS
JACK of DIAMONDS
TEN of CLUBS
ACE of DIAMONDS
KING of SPADES
JACK of HEARTS
SIX of HEARTS
ACE of CLUBS
ACE of HEARTS
FOUR of HEARTS
EIGHT of SPADES
THREE of DIAMONDS
TEN of HEARTS
SEVEN of CLUBS
FIVE of SPADES
FIVE of HEARTS
SEVEN of HEARTS
FIVE of DIAMONDS
KING of DIAMONDS
NINE of DIAMONDS
THREE of CLUBS
EIGHT of DIAMONDS
TWO of CLUBS
SIX of CLUBS
EIGHT of HEARTS
TEN of SPADES
SEVEN of DIAMONDS
TWO of CLUBS
TEN of DIAMONDS
ACE of DIAMONDS
THREE of DIAMONDS
KING of CLUBS
FOUR of DIAMONDS
FIVE of DIAMONDS
FIVE of SPADES
FOUR of CLUBS
FIVE of CLUBS
NINE of HEARTS
NINE of CLUBS
FIVE of HEARTS
QUEEN of CLUBS
SIX of SPADES
TEN of HEARTS
TWO of HEARTS
KING of DIAMONDS
THREE of HEARTS
SEVEN of CLUBS
TWO of SPADES
NINE of DIAMONDS
EIGHT of HEARTS
QUEEN of SPADES
JACK of HEARTS
KING of HEARTS
NINE of SPADES
FOUR of HEARTS
THREE of CLUBS
JACK of CLUBS
SEVEN of SPADES
EIGHT of DIAMONDS
TEN of SPADES
KING of SPADES
QUEEN of HEARTS
SIX of CLUBS
ACE of CLUBS
SIX of HEARTS
FOUR of SPADES
ACE of SPADES
THREE of SPADES
SIX of DIAMONDS
EIGHT of SPADES
JACK of SPADES
QUEEN of DIAMONDS
TEN of CLUBS
TWO of DIAMONDS
SEVEN of HEARTS
JACK of DIAMONDS
SEVEN of DIAMONDS
EIGHT of CLUBS
ACE of HEARTS
ACE of CLUBS
ACE of DIAMONDS
ACE of HEARTS
ACE of HEARTS
This call will print the following:
SIX of SPADES
THREE of HEARTS
FOUR of SPADES
KING of CLUBS
QUEEN of CLUBS
QUEEN of HEARTS
TWO of SPADES
ACE of SPADES
THREE of SPADES
SEVEN of SPADES
SIX of DIAMONDS
JACK of SPADES
FOUR of DIAMONDS
KING of HEARTS
NINE of SPADES
FOUR of CLUBS
TWO of HEARTS
JACK of CLUBS
QUEEN of SPADES
NINE of CLUBS
EIGHT of CLUBS
TWO of DIAMONDS
NINE of HEARTS
FIVE of CLUBS
QUEEN of DIAMONDS
TEN of DIAMONDS
JACK of DIAMONDS
TEN of CLUBS
ACE of DIAMONDS
KING of SPADES
JACK of HEARTS
SIX of HEARTS
ACE of CLUBS
ACE of HEARTS
FOUR of HEARTS
EIGHT of SPADES
THREE of DIAMONDS
TEN of HEARTS
SEVEN of CLUBS
FIVE of SPADES
FIVE of HEARTS
SEVEN of HEARTS
FIVE of DIAMONDS
KING of DIAMONDS
NINE of DIAMONDS
THREE of CLUBS
EIGHT of DIAMONDS
TWO of CLUBS
SIX of CLUBS
EIGHT of HEARTS
TEN of SPADES
SEVEN of DIAMONDS
TWO of CLUBS
TEN of DIAMONDS
ACE of DIAMONDS
THREE of DIAMONDS
KING of CLUBS
FOUR of DIAMONDS
FIVE of DIAMONDS
FIVE of SPADES
FOUR of CLUBS
FIVE of CLUBS
NINE of HEARTS
NINE of CLUBS
FIVE of HEARTS
QUEEN of CLUBS
SIX of SPADES
TEN of HEARTS
TWO of HEARTS
KING of DIAMONDS
THREE of HEARTS
SEVEN of CLUBS
TWO of SPADES
NINE of DIAMONDS
EIGHT of HEARTS
QUEEN of SPADES
JACK of HEARTS
KING of HEARTS
NINE of SPADES
FOUR of HEARTS
THREE of CLUBS
JACK of CLUBS
SEVEN of SPADES
EIGHT of DIAMONDS
TEN of SPADES
KING of SPADES
QUEEN of HEARTS
SIX of CLUBS
ACE of CLUBS
SIX of HEARTS
FOUR of SPADES
ACE of SPADES
THREE of SPADES
SIX of DIAMONDS
EIGHT of SPADES
JACK of SPADES
QUEEN of DIAMONDS
TEN of CLUBS
TWO of DIAMONDS
SEVEN of HEARTS
JACK of DIAMONDS
SEVEN of DIAMONDS
EIGHT of CLUBS
ACE of HEARTS
ACE of CLUBS
ACE of DIAMONDS
ACE of HEARTS
ACE of HEARTS
The two print statements will print true
then false
, respectively.
The two print statements will print true
then false
, respectively.
The two print statements will print 108
then 1
, respectively.
The two print statements will print 108
then 1
, respectively.
For root
, the fancy printer will output
Composite
Deck
Composite
Deck
CardSequence
For sequence
, the fancy printer will output
CardSequence
For root
, the fancy printer will output
Composite
Deck
Composite
Deck
CardSequence
For sequence
, the fancy printer will output
CardSequence
This call will write 4 cards
to the output stream.
This call will write 4 cards
to the output stream.
System
class contains several useful class fields and methods. It cannot be instantiated. Among the facilities provided by the System
class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
System
class contains several useful class fields and methods. It cannot be instantiated. Among the facilities provided by the System
class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
Console.charset()
if the Console
exists, stdout.encoding otherwise.
Console.charset()
if the Console
exists, stdout.encoding otherwise.
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data)
See the println
methods in class PrintStream
.
print(boolean)
and then println()
.
print(boolean)
and then println()
.
x
- The boolean
to be printed
print(int)
and then println()
.
print(int)
and then println()
.
x
- The int
to be printed.
print(String)
and then println()
.
print(String)
and then println()
.
x
- The String
to be printed.
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2, 3); String d = cde.substring(1, 2);
The class String
includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character
class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. For additional information on string concatenation and conversion, see The Java Language Specification.
Unless otherwise noted, passing a null
argument to a constructor or method in this class will cause a NullPointerException
to be thrown.
A String
represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character
class for more information). Index values refer to char
code units, so a supplementary character uses two positions in a String
.
The String
class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char
values).
Unless otherwise noted, methods for comparing Strings do not take locale into account. The Collator
class provides methods for finer-grain, locale-sensitive String comparison.
javac
compiler may implement the operator with StringBuffer
, StringBuilder
, or java.lang.invoke.StringConcatFactory
depending on the JDK version. The implementation of string conversion is typically through the method toString
, defined by Object
and inherited by all classes in Java.