Exercice n° 2 (= 8 points):
Saisie d'un entier du clavier : Scanner sc = new Scanner(System.in); int n sc.nextInt(); L'opérateur modulo en java :
Based on the given exercise instruction, let's develop the Java classes as requested.
CodeaBarre
class with specifications provided.import java.util.Scanner; public class CodeaBarre { private int[] code = new int[13 public void remplirCode() { Scanner sc = new Scanner(System.in); for (int i =0; i < code.length; i++) { do { System.out.println("Enter digit " + (i + 1) + " of the barcode (0-9): "); [i] = sc.nextInt(); } while (code[i < 0 || code[i] > 9); } } public void paysCode() { if (code[0] ==6 && code[1] == 1 && code[2] == 9) { System.out("Ce produit est Tunisien"); } else { System.out.println("Ce produit est étranger"); } } public void verifCode() { sum = 0; for (int i = 0; i < code.length - 1; i++) { += code[i]; } boolean isValid = (sum 2 == code[code.length - 1] % 2); System.out.println(isValid); } }
TestCode
which utilizes the CodeaBarre
class for creating filling, testing the validation, and determining whether the barcode is Tunisian or not.public class TestCode { public static main(String[] args) { CodeaBarre myCode = new CodeaBarre(); // Fill the code my.remplirCode(); // Test if the product Tunisian myCode.paysCode(); // Check if the code is valid myCode.verifCode(); } }
This pair of classes fulfills the given in the exercise. The class CodeaBarre
has methods to fill the barcode from input, check the country origin, and verify the barcode's. The TestCode
class uses an instance of `CodeaBarre to prompt the user for input and then determine if the barcode meets the specified conditions.
Note: The exercise description did not provide details for when or how to use the operator modulo in Java, referred to %
, but in the verifCode()
method, it is used to determine the parity of the sums the final digit to check the validity of the barcode.