Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the test problem I have and im stuck on the last part the testIt group i

ID: 3596056 • Letter: T

Question

This is the test problem I have and im stuck on the last part the testIt group in bold I have tried to pass a string in to it but I know im doing this wrong. Any feedback on how to get the last part to print the way I want would be greatly appreciated.

For this project, you will create the SerialNumber class, which is used by the Home Software 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 1 shows a UML diagram for the SerialNumber class.

Figure 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 1 describes the class’s methods.

Table 1 SerialNumber class’s methods

Method

Constructor

isValid validate

isFirstGroupValid

isSecondGroupValid

isThirdGroupValid

Description

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.

This method returns the value in the valid field.

This method calls the isFirstGroupValid, isSecondGroupValid, and isThirdGroupValid methods to validate the first, second, and third fields.

This method returns true if the value stored in the first field is valid. Otherwise, it returns false.

This method returns true if the value stored in the second field is valid. Otherwise, it returns false.

This method returns true if the value stored in the third field is valid. Otherwise, it returns false.

Your finished application should produce the following output:

Program Output

GHTRJ-8975-AQWR is valid. GHT7J-8975-AQWR is invalid. GHTRJ-8J75-AQWR is invalid. GHTRJ-8975-AQ2R is invalid.

Your task is to complete the missing code needed in each of these classes to produce the required output. You will see stub methods have been created for you.

This is my code so far for the SerialNumber.java

and this is the

Explanation / Answer

Your code was having alot of issues. So I have corrected the code and its working fine now. Please let me know if you still face any issue: -

SerialNumber.java

import java.util.StringTokenizer;

/**

* The SerialNumber class takes a software serial number in the form of

* LLLLL-DDDD-LLLL where each L is a letter and each D is a digit. The serial

* number has three groups of characters, separated by hyphens. The class

* extracts the three groups of characters and validates them.

*/

public class SerialNumber {

private String first; // First group of characters

private String second; // Second group of characters

private String third; // Third group of characters

private boolean valid; // Flag indicating validity

/**

* The constructor breaks a serial number into three groups and each group

* is validated.

*

* @param sn

* A serial number.

*/

public SerialNumber(String sn) {

// Create a StringTokenizer and initialize it with

// a trimmed copy of sn.

StringTokenizer st = new StringTokenizer(sn.trim(), "-");

// Tokenize and validate.

if (st.countTokens() != 3)

valid = false;

else {

// for (str : st) strArr[i] = str; i++;

first = st.nextToken();

second = st.nextToken();

third = st.nextToken();

validate();

}

}

/**

* The isValid method returns a boolean value indicating whether the serial

* number is valid.

*

* @return true if the serial number is valid, otherwise false.

*/

public boolean isValid() {

return valid;

}

/**

* The validate method sets the valid field to true if the serial number is

* valid. Otherwise it sets valid to false.

*/

private void validate() {

if (isFirstGroupValid() && isSecondGroupValid() && isThirdGroupValid())

valid = true;

else

valid = false;

}

/**

* The isFirstGroupValid method validates the first group of characters.

*

* @return true if the group is valid, otherwise false.

*/

private boolean isFirstGroupValid() {

boolean groupGood = true; // Flag

int i = 0; // Loop control variable

// Check the length of the group.

if (first.length() != 5)

groupGood = false;

// See if each character is a letter.

while (groupGood && i < first.length()) {

if (!Character.isLetter(first.charAt(i)))

groupGood = false;

i++;

}

return groupGood;

}

/**

* The isSecondGroupValid method validates the second group of characters.

*

* @return true if the group is valid, otherwise false.

*/

private boolean isSecondGroupValid() {

boolean groupGood = true; // Flag

int i = 0; // Loop control variable

// Check the length of the group.

if (second.length() != 4)

groupGood = false;

// See if each character is a digit.

while (groupGood && i < second.length()) {

if (!Character.isDigit(second.charAt(i)))

groupGood = false;

i++;

}

return groupGood;

}

/**

* The isThirdGroupValid method validates the third group of characters.

*

* @return true if the group is valid, otherwise false.

*/

private boolean isThirdGroupValid() {

boolean groupGood = true; // Flag

int i = 0; // Loop control variable

// Check the length of the group.

if (third.length() != 4)

groupGood = false;

// See if each character is a letter.

while (groupGood && i < third.length()) {

if (!Character.isLetter(third.charAt(i)))

groupGood = false;

i++;

}

return groupGood;

}

}

SerialNumberTester.java

/**

* This program demonstrates the SerialNumber class.

*/

public class SerialNumberTester {

public static void main(String[] args) {

String serial1 = "GHTRJ-8975-AQWR"; // Valid

String serial2 = "GHT7J-8975-AQWR"; // Invalid

String serial3 = "GHTRJ-8J75-AQWR"; // Invalid

String serial4 = "GHTRJ-8975-AQ2R"; // Invalid

testIt(serial1);

testIt(serial2);

testIt(serial3);

testIt(serial4);

}

/**

* This method takes a Serial and reports whether or not it is

* a valid serial number.

*

* @param sn

*/

private static void testIt(String serial) {

// TODO: Implement your code for this method here

SerialNumber ser = new SerialNumber(serial);

if (ser.isValid()) {

System.out.println(serial + " is valid");

} else {

System.out.println(serial + " is invalid");

}

}

}

Output

GHTRJ-8975-AQWR is valid
GHT7J-8975-AQWR is invalid
GHTRJ-8J75-AQWR is invalid
GHTRJ-8975-AQ2R is invalid

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote