Write a Java class (call it CreditCardNumber) which contains (note that each met
ID: 3902224 • Letter: W
Question
Write a Java class (call it CreditCardNumber) which contains (note that each method below is an instance method-- NOT static):
- private instance variable for a String with 6 digits called issuerID (default is "000000").
- private instance variable for a String with 9 digits called accountNum (default is "999999999").
- a default constructor which will assign the instance variables to the defaults (or leave with the default values if they were assigned in their declarations)
- a constructor which has 2 String parameters (one for issuerID, another for accountNum). First, call the default constructor, throw an exception (throw IllegalArgumentException) if either parameter is null or has incorrect length (6 and 9) for each string, otherwise assign default values.
- accessors, one for issuerID (String), one for accountNum (String),
Explanation / Answer
public class CreditCardNumber { private String issuerID; private String accountNum; public CreditCardNumber() { issuerID = "000000"; accountNum = "999999999"; } public CreditCardNumber(String issuerID, String accountNum) { this(); if(issuerID == null || issuerID.length() != 6 || accountNum == null || accountNum.length() != 9) { throw new IllegalArgumentException(); } this.issuerID = issuerID; this.accountNum = accountNum; } public String getIssuerID() { return issuerID; } public String getAccountNum() { return accountNum; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.