SlideShare a Scribd company logo
1 of 7
Download to read offline
CS4-1
CASESTUDY
4 The SerialNumber Class
In this case study you examine the SerialNumber class, which is used by the Home Soft-
ware Company to validate software serial numbers. A valid software serial number is in
the form LLLLL-DDDD-LLLL, where L indicates an alphabetic letter and D indicates a numeric
digit. For example, WRXTQ-7786-PGVZ is a valid serial number. Notice that a serial number
consists of three groups of characters, delimited by hyphens. Figure CS4-1 shows a UML
diagram for the SerialNumber class.
Figure CS4-1 UML diagram for the SerialNumber class
The fields first, second, and third are used to hold the first, second, and third groups of
characters in a serial number. The valid field is set to true by the constructor to indicate a
valid serial number, or false to indicate an invalid serial number. Table CS4-1 describes
the class’s methods.
CS4-2 Case Study 4 The SerialNumber Class
Table CS4-1 SerialNumber class’s methods
Method Description
Constructor The constructor accepts a string argument that contains a serial number.
The string is tokenized and its tokens are stored in the first, second,
and third fields. The validate method is called.
isValid This method returns the value in the valid field.
validate This method calls the isFirstGroupValid, isSecondGroupValid, and
isThirdGroupValid methods to validate the first, second, and third
fields.
isFirstGroupValid This method returns true if the value stored in the first field is valid.
Otherwise, it returns false.
isSecondGroupValid This method returns true if the value stored in the second field is valid.
Otherwise, it returns false.
isThirdGroupValid This method returns true if the value stored in the third field is valid.
Otherwise, it returns false.
The code for the SerialNumber class is shown in Code Listing CS4-1.
Code Listing CS4-1 (SerialNumber.java)
1 import java.util.StringTokenizer;
2
3 /**
4 * The SerialNumber class takes a software serial number in
5 * the form of LLLLL-DDDD-LLLL where each L is a letter
6 * and each D is a digit. The serial number has three groups
7 * of characters, separated by hyphens. The class extracts
8 * the three groups of characters and validates them.
9 */
10
11 public class SerialNumber
12 {
13 private String first; // First group of characters
14 private String second; // Second group of characters
15 private String third; // Third group of characters
16 private boolean valid; // Flag indicating validity
17
18 /**
19 * The following constructor accepts a serial number as
20 * its String argument. The argument is broken into
21 * three groups and each group is validated.
22 */
23
Case Study 4 The SerialNumber Class CS4-3
24 public SerialNumber(String sn)
25 {
26 // Create a StringTokenizer and initialize
27 // it with a trimmed copy of sn.
28 StringTokenizer st =
29 new StringTokenizer(sn.trim(), "-");
30
31 // Tokenize and validate.
32 if (st.countTokens() != 3)
33 valid = false;
34 else
35 {
36 first = st.nextToken();
37 second = st.nextToken();
38 third = st.nextToken();
39 validate();
40 }
41 }
42
43 /**
44 * The following method returns the valid field.
45 */
46
47 public boolean isValid()
48 {
49 return valid;
50 }
51
52 /**
53 * The following method sets the valid field to true
54 * if the serial number is valid. Otherwise it sets
55 * valid to false.
56 */
57
58 private void validate()
59 {
60 if (isFirstGroupValid() && isSecondGroupValid() &&
61 isThirdGroupValid())
62 valid = true;
63 else
64 valid = false;
65 }
66
67 /**
68 * The following method validates the first group of
69 * characters. If the group is valid, it returns
70 * true. Otherwise it returns false.
71 */
CS4-4 Case Study 4 The SerialNumber Class
72
73 private boolean isFirstGroupValid()
74 {
75 boolean groupGood = true; // Flag
76 int i = 0; // Loop control variable
77
78 // Check the length of the group.
79 if (first.length() != 5)
80 groupGood = false;
81
82 // See if each character is a letter.
83 while (groupGood && i < first.length())
84 {
85 if (!Character.isLetter(first.charAt(i)))
86 groupGood = false;
87 i++;
88 }
89
90 return groupGood;
91 }
92
93 /**
94 * The following method validates the second group
95 * of characters. If the group is valid, it returns
96 * true. Otherwise it returns false.
97 */
98
99 private boolean isSecondGroupValid()
100 {
101 boolean groupGood = true; // Flag
102 int i = 0; // Loop control variable
103
104 // Check the length of the group.
105 if (second.length() != 4)
106 groupGood = false;
107
108 // See if each character is a digit.
109 while (groupGood && i < second.length())
110 {
111 if (!Character.isDigit(second.charAt(i)))
112 groupGood = false;
113 i++;
114 }
115
116 return groupGood;
117 }
118
Case Study 4 The SerialNumber Class CS4-5
119 /**
120 * The following method validates the third group of
121 * characters. If the group is valid, it returns
122 * true. Otherwise it returns false.
123 */
124
125 private boolean isThirdGroupValid()
126 {
127 boolean groupGood = true; // Flag
128 int i = 0; // Loop control variable
129
130 // Check the length of the group.
131 if (third.length() != 4)
132 groupGood = false;
133
134 // See if each character is a letter.
135 while (groupGood && i < third.length())
136 {
137 if (!Character.isLetter(third.charAt(i)))
138 groupGood = false;
139 i++;
140 }
141
142 return groupGood;
143 }
144 }
Let’s take a closer look at the constructor, in lines 24 through 41. The following statement, in
lines 28 and 29, creates a StringTokenizer object, using the hyphen character as a delimiter:
StringTokenizer st =
new StringTokenizer(sn.trim(), "-");
Notice that we call the argument’s trim method to remove any leading and/or trailing
whitespace characters. This is important because whitespace characters are not used as
delimiters in this code. If the argument contains leading whitespace characters, they will be
included as part of the first token. Trailing whitespace characters will be included as part
of the last token.
Next, the if statement in lines 32 through 40 executes:
if (st.countTokens() != 3)
valid = false;
else
{
first = st.nextToken();
second = st.nextToken();
third = st.nextToken();
validate();
}
CS4-6 Case Study 4 The SerialNumber Class
A valid serial number must have three groups of characters, so the if statement determines
whether the string has three tokens. If not, the valid field is set to false. Otherwise, the
three tokens are extracted and assigned to the first, second, and third fields. Last, the
validate method is called. The validate method calls the isFirstGroupValid,
isSecondGroupValid, and isThirdGroupValid methods to validate the three groups of
characters. In the end, the valid field will be set to true if the serial number is valid, or
false otherwise. The program in Code Listing CS4-2 demonstrates the class.
Code Listing CS4-2 (SerialNumberTester.java)
1 /**
2 * This program demonstrates the SerialNumber class.
3 */
4
5 public class SerialNumberTester
6 {
7 public static void main(String[] args)
8 {
9 String serial1 = "GHTRJ-8975-AQWR"; // Valid
10 String serial2 = "GHT7J-8975-AQWR"; // Invalid
11 String serial3 = "GHTRJ-8J75-AQWR"; // Invalid
12 String serial4 = "GHTRJ-8975-AQ2R"; // Invalid
13
14 // Validate serial1.
15
16 SerialNumber sn = new SerialNumber(serial1);
17 if (sn.isValid())
18 System.out.println(serial1 + " is valid.");
19 else
20 System.out.println(serial1 + " is invalid.");
21
22 // Validate serial2.
23
24 sn = new SerialNumber(serial2);
25 if (sn.isValid())
26 System.out.println(serial2 + " is valid.");
27 else
28 System.out.println(serial2 + " is invalid.");
29
30 // Validate serial3.
31
32 sn = new SerialNumber(serial3);
33 if (sn.isValid())
34 System.out.println(serial3 + " is valid.");
35 else
36 System.out.println(serial3 + " is invalid.");
37
Case Study 4 The SerialNumber Class CS4-7
38 // Validate serial4.
39
40 sn = new SerialNumber(serial4);
41 if (sn.isValid())
42 System.out.println(serial4 + " is valid.");
43 else
44 System.out.println(serial4 + " is invalid.");
45 }
46 }
Program Output
GHTRJ-8975-AQWR is valid.
GHT7J-8975-AQWR is invalid.
GHTRJ-8J75-AQWR is invalid.
GHTRJ-8975-AQ2R is invalid.

More Related Content

What's hot

Types of Language in Theory of Computation
Types of Language in Theory of ComputationTypes of Language in Theory of Computation
Types of Language in Theory of ComputationAnkur Singh
 
Algorithm and Programming (Introduction of Algorithms)
Algorithm and Programming (Introduction of Algorithms)Algorithm and Programming (Introduction of Algorithms)
Algorithm and Programming (Introduction of Algorithms)Adam Mukharil Bachtiar
 
Implementation of k-means clustering algorithm in C
Implementation of k-means clustering algorithm in CImplementation of k-means clustering algorithm in C
Implementation of k-means clustering algorithm in CKasun Ranga Wijeweera
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
Compiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfCompiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfkalpana Manudhane
 
Introduction of flex
Introduction of flexIntroduction of flex
Introduction of flexvip_du
 
Ch7 Process Synchronization galvin
Ch7 Process Synchronization galvinCh7 Process Synchronization galvin
Ch7 Process Synchronization galvinShubham Singh
 
5 Macam Metode Dasar Kriptografi
5 Macam Metode Dasar Kriptografi5 Macam Metode Dasar Kriptografi
5 Macam Metode Dasar KriptografiRoziq Bahtiar
 
Automata languages and computation
Automata languages and computationAutomata languages and computation
Automata languages and computationKarthik Velou
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compilerSudhaa Ravi
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automatahafizhamza0322
 
Trigger Database
Trigger DatabaseTrigger Database
Trigger DatabasePutra Andry
 
Bab Iii Kondisi
Bab Iii KondisiBab Iii Kondisi
Bab Iii Kondisiformatik
 
Topik 5 Ekspresi dan Iinput Output
Topik 5 Ekspresi dan Iinput OutputTopik 5 Ekspresi dan Iinput Output
Topik 5 Ekspresi dan Iinput OutputI Komang Agustino
 
UAS Interpersonal Skill
UAS Interpersonal SkillUAS Interpersonal Skill
UAS Interpersonal Skillanzllaa
 
Teori bahasa dan automata7
Teori bahasa dan automata7Teori bahasa dan automata7
Teori bahasa dan automata7Nurdin Al-Azies
 

What's hot (20)

Types of Language in Theory of Computation
Types of Language in Theory of ComputationTypes of Language in Theory of Computation
Types of Language in Theory of Computation
 
Algorithm and Programming (Introduction of Algorithms)
Algorithm and Programming (Introduction of Algorithms)Algorithm and Programming (Introduction of Algorithms)
Algorithm and Programming (Introduction of Algorithms)
 
Implementation of k-means clustering algorithm in C
Implementation of k-means clustering algorithm in CImplementation of k-means clustering algorithm in C
Implementation of k-means clustering algorithm in C
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Compiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfCompiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdf
 
Introduction of flex
Introduction of flexIntroduction of flex
Introduction of flex
 
Ch7 Process Synchronization galvin
Ch7 Process Synchronization galvinCh7 Process Synchronization galvin
Ch7 Process Synchronization galvin
 
5 Macam Metode Dasar Kriptografi
5 Macam Metode Dasar Kriptografi5 Macam Metode Dasar Kriptografi
5 Macam Metode Dasar Kriptografi
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Automata languages and computation
Automata languages and computationAutomata languages and computation
Automata languages and computation
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
Trigger Database
Trigger DatabaseTrigger Database
Trigger Database
 
Bab Iii Kondisi
Bab Iii KondisiBab Iii Kondisi
Bab Iii Kondisi
 
Topik 5 Ekspresi dan Iinput Output
Topik 5 Ekspresi dan Iinput OutputTopik 5 Ekspresi dan Iinput Output
Topik 5 Ekspresi dan Iinput Output
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
UAS Interpersonal Skill
UAS Interpersonal SkillUAS Interpersonal Skill
UAS Interpersonal Skill
 
Teori bahasa dan automata7
Teori bahasa dan automata7Teori bahasa dan automata7
Teori bahasa dan automata7
 

Similar to Serial number java_code

Analyzing the Quake III Arena GPL project
Analyzing the Quake III Arena GPL projectAnalyzing the Quake III Arena GPL project
Analyzing the Quake III Arena GPL projectPVS-Studio
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfsuresh640714
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptxMrhaider4
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptxFolkAdonis
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#Umar Farooq
 
CS244 _Lec8_Generics_innerclasses_Lambda.pptx
CS244 _Lec8_Generics_innerclasses_Lambda.pptxCS244 _Lec8_Generics_innerclasses_Lambda.pptx
CS244 _Lec8_Generics_innerclasses_Lambda.pptxNadeemEzat
 
I am working on java programming that converts zipcode to barcode an.pdf
I am working on java programming that converts zipcode to barcode an.pdfI am working on java programming that converts zipcode to barcode an.pdf
I am working on java programming that converts zipcode to barcode an.pdfthangarajarivukadal
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with examplepranav kumar verma
 
Can someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdfCan someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdfAmansupan
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 

Similar to Serial number java_code (19)

LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
Analyzing the Quake III Arena GPL project
Analyzing the Quake III Arena GPL projectAnalyzing the Quake III Arena GPL project
Analyzing the Quake III Arena GPL project
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdf
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Sas array statement
Sas array statementSas array statement
Sas array statement
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
 
Array assignment
Array assignmentArray assignment
Array assignment
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
C# basics
C# basicsC# basics
C# basics
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Ch08
Ch08Ch08
Ch08
 
CS244 _Lec8_Generics_innerclasses_Lambda.pptx
CS244 _Lec8_Generics_innerclasses_Lambda.pptxCS244 _Lec8_Generics_innerclasses_Lambda.pptx
CS244 _Lec8_Generics_innerclasses_Lambda.pptx
 
I am working on java programming that converts zipcode to barcode an.pdf
I am working on java programming that converts zipcode to barcode an.pdfI am working on java programming that converts zipcode to barcode an.pdf
I am working on java programming that converts zipcode to barcode an.pdf
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
 
Can someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdfCan someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdf
 
Ch5 array nota
Ch5 array notaCh5 array nota
Ch5 array nota
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 

More from Shyam Sarkar

Scry analytics article on data analytics outsourcing, nov. 18, 2014
Scry analytics article on data analytics outsourcing, nov. 18, 2014Scry analytics article on data analytics outsourcing, nov. 18, 2014
Scry analytics article on data analytics outsourcing, nov. 18, 2014Shyam Sarkar
 
Tagores russiar chithi_paper
Tagores russiar chithi_paperTagores russiar chithi_paper
Tagores russiar chithi_paperShyam Sarkar
 
Decision tree handson
Decision tree handsonDecision tree handson
Decision tree handsonShyam Sarkar
 
Cancer genomics big_datascience_meetup_july_14_2014
Cancer genomics big_datascience_meetup_july_14_2014Cancer genomics big_datascience_meetup_july_14_2014
Cancer genomics big_datascience_meetup_july_14_2014Shyam Sarkar
 
Technology treasury-management-2013
Technology treasury-management-2013Technology treasury-management-2013
Technology treasury-management-2013Shyam Sarkar
 
UN-Creative economy-report-2013
UN-Creative economy-report-2013UN-Creative economy-report-2013
UN-Creative economy-report-2013Shyam Sarkar
 
Local exchanges for_sm_es
Local exchanges for_sm_esLocal exchanges for_sm_es
Local exchanges for_sm_esShyam Sarkar
 
Innovative+agricultural+sme+finance+models
Innovative+agricultural+sme+finance+modelsInnovative+agricultural+sme+finance+models
Innovative+agricultural+sme+finance+modelsShyam Sarkar
 
Forrester big data_predictive_analytics
Forrester big data_predictive_analyticsForrester big data_predictive_analytics
Forrester big data_predictive_analyticsShyam Sarkar
 
Stock markets and_human_genomics
Stock markets and_human_genomicsStock markets and_human_genomics
Stock markets and_human_genomicsShyam Sarkar
 
Cancer genome repository_berkeley
Cancer genome repository_berkeleyCancer genome repository_berkeley
Cancer genome repository_berkeleyShyam Sarkar
 
Big security for_big_data
Big security for_big_dataBig security for_big_data
Big security for_big_dataShyam Sarkar
 
Renewable energy report
Renewable energy reportRenewable energy report
Renewable energy reportShyam Sarkar
 
Wef big databigimpact_briefing_2012
Wef big databigimpact_briefing_2012Wef big databigimpact_briefing_2012
Wef big databigimpact_briefing_2012Shyam Sarkar
 
Green technology report_2012
Green technology report_2012Green technology report_2012
Green technology report_2012Shyam Sarkar
 
Portfolio investment opportuities in india
Portfolio investment opportuities in indiaPortfolio investment opportuities in india
Portfolio investment opportuities in indiaShyam Sarkar
 

More from Shyam Sarkar (20)

Scry analytics article on data analytics outsourcing, nov. 18, 2014
Scry analytics article on data analytics outsourcing, nov. 18, 2014Scry analytics article on data analytics outsourcing, nov. 18, 2014
Scry analytics article on data analytics outsourcing, nov. 18, 2014
 
Tagores russiar chithi_paper
Tagores russiar chithi_paperTagores russiar chithi_paper
Tagores russiar chithi_paper
 
Decision tree handson
Decision tree handsonDecision tree handson
Decision tree handson
 
Cancer genomics big_datascience_meetup_july_14_2014
Cancer genomics big_datascience_meetup_july_14_2014Cancer genomics big_datascience_meetup_july_14_2014
Cancer genomics big_datascience_meetup_july_14_2014
 
Technology treasury-management-2013
Technology treasury-management-2013Technology treasury-management-2013
Technology treasury-management-2013
 
UN-Creative economy-report-2013
UN-Creative economy-report-2013UN-Creative economy-report-2013
UN-Creative economy-report-2013
 
Local exchanges for_sm_es
Local exchanges for_sm_esLocal exchanges for_sm_es
Local exchanges for_sm_es
 
Innovative+agricultural+sme+finance+models
Innovative+agricultural+sme+finance+modelsInnovative+agricultural+sme+finance+models
Innovative+agricultural+sme+finance+models
 
Religionofman
ReligionofmanReligionofman
Religionofman
 
Forrester big data_predictive_analytics
Forrester big data_predictive_analyticsForrester big data_predictive_analytics
Forrester big data_predictive_analytics
 
Stock markets and_human_genomics
Stock markets and_human_genomicsStock markets and_human_genomics
Stock markets and_human_genomics
 
Cancer genome repository_berkeley
Cancer genome repository_berkeleyCancer genome repository_berkeley
Cancer genome repository_berkeley
 
Big security for_big_data
Big security for_big_dataBig security for_big_data
Big security for_big_data
 
Renewable energy report
Renewable energy reportRenewable energy report
Renewable energy report
 
Nasa on biofuel
Nasa on biofuelNasa on biofuel
Nasa on biofuel
 
Wef big databigimpact_briefing_2012
Wef big databigimpact_briefing_2012Wef big databigimpact_briefing_2012
Wef big databigimpact_briefing_2012
 
World bank report
World bank reportWorld bank report
World bank report
 
Green technology report_2012
Green technology report_2012Green technology report_2012
Green technology report_2012
 
Portfolio investment opportuities in india
Portfolio investment opportuities in indiaPortfolio investment opportuities in india
Portfolio investment opportuities in india
 
Tapan 29 jun
Tapan 29 junTapan 29 jun
Tapan 29 jun
 

Recently uploaded

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...masabamasaba
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 

Recently uploaded (20)

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 

Serial number java_code

  • 1. CS4-1 CASESTUDY 4 The SerialNumber Class In this case study you examine the SerialNumber class, which is used by the Home Soft- ware Company to validate software serial numbers. A valid software serial number is in the form LLLLL-DDDD-LLLL, where L indicates an alphabetic letter and D indicates a numeric digit. For example, WRXTQ-7786-PGVZ is a valid serial number. Notice that a serial number consists of three groups of characters, delimited by hyphens. Figure CS4-1 shows a UML diagram for the SerialNumber class. Figure CS4-1 UML diagram for the SerialNumber class The fields first, second, and third are used to hold the first, second, and third groups of characters in a serial number. The valid field is set to true by the constructor to indicate a valid serial number, or false to indicate an invalid serial number. Table CS4-1 describes the class’s methods.
  • 2. CS4-2 Case Study 4 The SerialNumber Class Table CS4-1 SerialNumber class’s methods Method Description Constructor The constructor accepts a string argument that contains a serial number. The string is tokenized and its tokens are stored in the first, second, and third fields. The validate method is called. isValid This method returns the value in the valid field. validate This method calls the isFirstGroupValid, isSecondGroupValid, and isThirdGroupValid methods to validate the first, second, and third fields. isFirstGroupValid This method returns true if the value stored in the first field is valid. Otherwise, it returns false. isSecondGroupValid This method returns true if the value stored in the second field is valid. Otherwise, it returns false. isThirdGroupValid This method returns true if the value stored in the third field is valid. Otherwise, it returns false. The code for the SerialNumber class is shown in Code Listing CS4-1. Code Listing CS4-1 (SerialNumber.java) 1 import java.util.StringTokenizer; 2 3 /** 4 * The SerialNumber class takes a software serial number in 5 * the form of LLLLL-DDDD-LLLL where each L is a letter 6 * and each D is a digit. The serial number has three groups 7 * of characters, separated by hyphens. The class extracts 8 * the three groups of characters and validates them. 9 */ 10 11 public class SerialNumber 12 { 13 private String first; // First group of characters 14 private String second; // Second group of characters 15 private String third; // Third group of characters 16 private boolean valid; // Flag indicating validity 17 18 /** 19 * The following constructor accepts a serial number as 20 * its String argument. The argument is broken into 21 * three groups and each group is validated. 22 */ 23
  • 3. Case Study 4 The SerialNumber Class CS4-3 24 public SerialNumber(String sn) 25 { 26 // Create a StringTokenizer and initialize 27 // it with a trimmed copy of sn. 28 StringTokenizer st = 29 new StringTokenizer(sn.trim(), "-"); 30 31 // Tokenize and validate. 32 if (st.countTokens() != 3) 33 valid = false; 34 else 35 { 36 first = st.nextToken(); 37 second = st.nextToken(); 38 third = st.nextToken(); 39 validate(); 40 } 41 } 42 43 /** 44 * The following method returns the valid field. 45 */ 46 47 public boolean isValid() 48 { 49 return valid; 50 } 51 52 /** 53 * The following method sets the valid field to true 54 * if the serial number is valid. Otherwise it sets 55 * valid to false. 56 */ 57 58 private void validate() 59 { 60 if (isFirstGroupValid() && isSecondGroupValid() && 61 isThirdGroupValid()) 62 valid = true; 63 else 64 valid = false; 65 } 66 67 /** 68 * The following method validates the first group of 69 * characters. If the group is valid, it returns 70 * true. Otherwise it returns false. 71 */
  • 4. CS4-4 Case Study 4 The SerialNumber Class 72 73 private boolean isFirstGroupValid() 74 { 75 boolean groupGood = true; // Flag 76 int i = 0; // Loop control variable 77 78 // Check the length of the group. 79 if (first.length() != 5) 80 groupGood = false; 81 82 // See if each character is a letter. 83 while (groupGood && i < first.length()) 84 { 85 if (!Character.isLetter(first.charAt(i))) 86 groupGood = false; 87 i++; 88 } 89 90 return groupGood; 91 } 92 93 /** 94 * The following method validates the second group 95 * of characters. If the group is valid, it returns 96 * true. Otherwise it returns false. 97 */ 98 99 private boolean isSecondGroupValid() 100 { 101 boolean groupGood = true; // Flag 102 int i = 0; // Loop control variable 103 104 // Check the length of the group. 105 if (second.length() != 4) 106 groupGood = false; 107 108 // See if each character is a digit. 109 while (groupGood && i < second.length()) 110 { 111 if (!Character.isDigit(second.charAt(i))) 112 groupGood = false; 113 i++; 114 } 115 116 return groupGood; 117 } 118
  • 5. Case Study 4 The SerialNumber Class CS4-5 119 /** 120 * The following method validates the third group of 121 * characters. If the group is valid, it returns 122 * true. Otherwise it returns false. 123 */ 124 125 private boolean isThirdGroupValid() 126 { 127 boolean groupGood = true; // Flag 128 int i = 0; // Loop control variable 129 130 // Check the length of the group. 131 if (third.length() != 4) 132 groupGood = false; 133 134 // See if each character is a letter. 135 while (groupGood && i < third.length()) 136 { 137 if (!Character.isLetter(third.charAt(i))) 138 groupGood = false; 139 i++; 140 } 141 142 return groupGood; 143 } 144 } Let’s take a closer look at the constructor, in lines 24 through 41. The following statement, in lines 28 and 29, creates a StringTokenizer object, using the hyphen character as a delimiter: StringTokenizer st = new StringTokenizer(sn.trim(), "-"); Notice that we call the argument’s trim method to remove any leading and/or trailing whitespace characters. This is important because whitespace characters are not used as delimiters in this code. If the argument contains leading whitespace characters, they will be included as part of the first token. Trailing whitespace characters will be included as part of the last token. Next, the if statement in lines 32 through 40 executes: if (st.countTokens() != 3) valid = false; else { first = st.nextToken(); second = st.nextToken(); third = st.nextToken(); validate(); }
  • 6. CS4-6 Case Study 4 The SerialNumber Class A valid serial number must have three groups of characters, so the if statement determines whether the string has three tokens. If not, the valid field is set to false. Otherwise, the three tokens are extracted and assigned to the first, second, and third fields. Last, the validate method is called. The validate method calls the isFirstGroupValid, isSecondGroupValid, and isThirdGroupValid methods to validate the three groups of characters. In the end, the valid field will be set to true if the serial number is valid, or false otherwise. The program in Code Listing CS4-2 demonstrates the class. Code Listing CS4-2 (SerialNumberTester.java) 1 /** 2 * This program demonstrates the SerialNumber class. 3 */ 4 5 public class SerialNumberTester 6 { 7 public static void main(String[] args) 8 { 9 String serial1 = "GHTRJ-8975-AQWR"; // Valid 10 String serial2 = "GHT7J-8975-AQWR"; // Invalid 11 String serial3 = "GHTRJ-8J75-AQWR"; // Invalid 12 String serial4 = "GHTRJ-8975-AQ2R"; // Invalid 13 14 // Validate serial1. 15 16 SerialNumber sn = new SerialNumber(serial1); 17 if (sn.isValid()) 18 System.out.println(serial1 + " is valid."); 19 else 20 System.out.println(serial1 + " is invalid."); 21 22 // Validate serial2. 23 24 sn = new SerialNumber(serial2); 25 if (sn.isValid()) 26 System.out.println(serial2 + " is valid."); 27 else 28 System.out.println(serial2 + " is invalid."); 29 30 // Validate serial3. 31 32 sn = new SerialNumber(serial3); 33 if (sn.isValid()) 34 System.out.println(serial3 + " is valid."); 35 else 36 System.out.println(serial3 + " is invalid."); 37
  • 7. Case Study 4 The SerialNumber Class CS4-7 38 // Validate serial4. 39 40 sn = new SerialNumber(serial4); 41 if (sn.isValid()) 42 System.out.println(serial4 + " is valid."); 43 else 44 System.out.println(serial4 + " is invalid."); 45 } 46 } Program Output GHTRJ-8975-AQWR is valid. GHT7J-8975-AQWR is invalid. GHTRJ-8J75-AQWR is invalid. GHTRJ-8975-AQ2R is invalid.