With Comment and JAVA Program 2 Create a class with a method that accepts a char
ID: 3860015 • Letter: W
Question
With Comment and JAVA Program 2
Create a class with a method that accepts a charge account number as its argument. The method should determine whether the number is valid by comparing it to the following list of valid charge account numbers: These numbers should be stored in an array. Use a sequential search to locate the number passed as an argument. If the number is in the array, the method should return true, indicating the number is valid. If the number is not in the array, the method should return false, indicating the number is invalid. Write a program that tests the class by asking the user to enter a charge account number. The program should display a message indicating whether the number is valid or invalid.Explanation / Answer
ChargeAccountValidation.java
public class ChargeAccountValidation {
//Declaring an array which holds account numbers
int accNos[] = {
5358845,
4520125,
7895122,
8777541,
8451277,
1302850,
8080152,
4562555,
5552021,
5050552,
7825877,
1250255,
1005231,
6545231,
3852085,
7576651,
7881200,
4581002
};
//Zero argumented constructor
public ChargeAccountValidation() {
}
//This method will check whether the user entered number is valid or not
public boolean isValid(int accNo) {
for (int i = 0; i < accNos.length; i++) {
if (accNos[i] == accNo)
return true;
}
return false;
}
}
_____________________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variable
int accNo;
//Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Getting the account number entered by the user
System.out.print("Enter the Account Number :");
accNo = sc.nextInt();
//Creating an Object
ChargeAccountValidation cav = new ChargeAccountValidation();
/* calling the method by passing the user
* entered account number as argument
* */
boolean bool = cav.isValid(accNo);
//Based on the boolean value display whether the number is valid or not
if (bool)
System.out.println("** The Number is Valid **");
else
System.out.println("** The Number is Valid **");
}
}
________________________
Output:
Enter the Account Number :1005231
** The Number is Valid **
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.