Skip to content

Help me fix the error #445

@sandy8669

Description

@sandy8669

import java.util.Random;

import java.time.LocalDate;
public class SP2024_DriverLicenseCandidate_Dahal {
private String candidateName;
private String SSNumber;
private String address;
private char[] keyArray;
private char[] answerArray;

// no-argument constructor
public SP2024_DriverLicenseCandidate_Dahal() {
this.keyArray = new char[25];
this.answerArray = new char[25];
}

// parameterized constructor
public SP2024_DriverLicenseCandidate_Dahal(String candidateName,String SSNumber, String address, char[] answerArray) {
this.candidateName = candidateName;
this.SSNumber = SSNumber;
this.address = address;
this.keyArray = new char[25];
this.answerArray = answerArray;
}

// method to evaluate the test result
public int[] evaluateResult() {
int[] resultArray = new int[25];
for (int i = 0; i < 25; i++) {
if (keyArray[i] == answerArray[i]) {
resultArray[i] = 1;
}
}
return resultArray;
}

// method to generate the Driver License Number
public String generateDriverLicenseNumber() {
Random random = new Random();
StringBuilder licenseNumber = new StringBuilder();
for (int i = 0; i < 8; i++) {
licenseNumber.append(random.nextInt(10));
}
return licenseNumber.toString();
}

// method to convert the resultArray to a string
public String getResultString(int[] resultArray) {

   int correctCount = 0;
   StringBuilder failedQuestions = new StringBuilder();
   for (int i = 0; i < 25; i++) {
       if (resultArray[i] == 0) {
           failedQuestions.append((i + 1)).append(" ");
       } else {
           correctCount++;
       }
   }

   String result = correctCount >= 20 ? "PASSED" : "FAILED";
   String licenseNumber = result.equals("PASSED") ? generateDriverLicenseNumber() : "";
   
    
    String resultString =String.format("DRIVER LICENSE COMPUTER TEST RESULT\n" +
           "Test Date:%s\n" +
           "Candidate: %s %s\n" +
           "SS Number: %s\n" +
           "Address: %s\n" +
           "Result: %s\n" +
           "Number of correct answers: %d\n" +
           "Failed questions: %s\n" +
           "Driver License Number: %s\n"+
           java.time.LocalDate.now() ,candidateName, SSNumber, address, result, correctCount, failedQuestions, licenseNumber);
    return resultString ;

}

// method to convert the answerArray to a string
public String getAnswerString() {
StringBuilder answerString = new StringBuilder();
for (int i = 0; i < 25; i++) {
answerString.append(answerArray[i]).append(" ");
}
return answerString.toString();
}

// toString method to display the test result
public String toString() {
int[] resultArray = evaluateResult();
return getResultString(resultArray);
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions