Question #54290

This is a UML class diagram using Java syntax for the members. It is a summary of the design. The full details will follow.
AccountNumber
private int[] digits
public AccountNumber(boolean random)
public String toString()
public static AccountNumber fromString(String number)
private int checksum()
This version uses UML syntax for the members.
AccountNumber
-digits : int[]
+AccountNumber(random : boolean)
+toString() : String
+fromString(number : String) : AccountNumber
-checksum() : int
Fields
private int[] digits
Constructors
public AccountNumber(boolean random)
Methods
public String toString()
public static AccountNumber fromString(String number)
private int checksum()
Implement AccountNumber.checksum() and the constructor and test it with a program that generates random, valid account numbers. Use a command line argument to specify how many numbers to generate, as follows:
$javaGenerate 2(2 lines)
24568231
13789425

Expert's answer

import java.util.Arrays;

public class JavaApplication {

public static void main(String[] args) {

int count = Integer.parseInt(args[0]);

for (int i = 0; i < count; i++) {

AccountNumber account = new AccountNumber(true);

System.out.println(account.toString());

}

}

}

class AccountNumber {

private int[] digits = new int[8];

public AccountNumber(boolean random) {

if (random) {

for (int i = 0; i < digits.length; i++) {

digits[i] = (int) (Math.random() * 10);

}

}

}

public String toString() {

String result = "";

for (int i = 0; i < digits.length; i++) {

result = result + digits[i];

}

return result;

}

public void setDigits(int[] digits) {

this.digits = digits;

}

public static AccountNumber fromString(String number) {

int[] arr = new int[number.length()];

for (int i = 0; i < arr.length; i++) {

char digit = number.charAt(i);

arr[i] = Character.getNumericValue(digit);

}

AccountNumber account = new AccountNumber(false);

account.setDigits(arr);

return account;

}

private int checksum() {

int result = 0;

for (int i = 0; i < digits.length; i++) {

result = result + digits[i];

}

return result;

}

}

public class JavaApplication {

public static void main(String[] args) {

int count = Integer.parseInt(args[0]);

for (int i = 0; i < count; i++) {

AccountNumber account = new AccountNumber(true);

System.out.println(account.toString());

}

}

}

class AccountNumber {

private int[] digits = new int[8];

public AccountNumber(boolean random) {

if (random) {

for (int i = 0; i < digits.length; i++) {

digits[i] = (int) (Math.random() * 10);

}

}

}

public String toString() {

String result = "";

for (int i = 0; i < digits.length; i++) {

result = result + digits[i];

}

return result;

}

public void setDigits(int[] digits) {

this.digits = digits;

}

public static AccountNumber fromString(String number) {

int[] arr = new int[number.length()];

for (int i = 0; i < arr.length; i++) {

char digit = number.charAt(i);

arr[i] = Character.getNumericValue(digit);

}

AccountNumber account = new AccountNumber(false);

account.setDigits(arr);

return account;

}

private int checksum() {

int result = 0;

for (int i = 0; i < digits.length; i++) {

result = result + digits[i];

}

return result;

}

}

## Comments

## Leave a comment