|
| 1 | +import java.util.Scanner; |
| 2 | + |
| 3 | +public class LineCoding { |
| 4 | + |
| 5 | + // Function for Unipolar NRZ |
| 6 | + static void unipolarNRZ(int[] data) { |
| 7 | + System.out.println("Unipolar NRZ Encoding:"); |
| 8 | + for (int bit : data) { |
| 9 | + if (bit == 1) System.out.print("[+1] "); |
| 10 | + else System.out.print("[0] "); |
| 11 | + } |
| 12 | + System.out.println(); |
| 13 | + } |
| 14 | + |
| 15 | + // Function for Polar NRZ |
| 16 | + static void polarNRZ(int[] data) { |
| 17 | + System.out.println("Polar NRZ Encoding:"); |
| 18 | + for (int bit : data) { |
| 19 | + if (bit == 1) System.out.print("[+1] "); |
| 20 | + else System.out.print("[-1] "); |
| 21 | + } |
| 22 | + System.out.println(); |
| 23 | + } |
| 24 | + |
| 25 | + // Function for Manchester Encoding |
| 26 | + static void manchester(int[] data) { |
| 27 | + System.out.println("Manchester Encoding:"); |
| 28 | + for (int bit : data) { |
| 29 | + if (bit == 1) System.out.print("[+1, -1] "); |
| 30 | + else System.out.print("[-1, +1] "); |
| 31 | + } |
| 32 | + System.out.println(); |
| 33 | + } |
| 34 | + |
| 35 | + // Function for Differential Manchester Encoding |
| 36 | + static void differentialManchester(int[] data) { |
| 37 | + System.out.println("Differential Manchester Encoding:"); |
| 38 | + int current = 1; // Assume starting high (+1) |
| 39 | + for (int bit : data) { |
| 40 | + if (bit == 1) { |
| 41 | + // Transition at beginning |
| 42 | + current = -current; |
| 43 | + } |
| 44 | + // Always transition at mid-bit |
| 45 | + if (current == 1) System.out.print("[+1, -1] "); |
| 46 | + else System.out.print("[-1, +1] "); |
| 47 | + current = -current; // flip for next bit |
| 48 | + } |
| 49 | + System.out.println(); |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + try (Scanner sc = new Scanner(System.in)) { |
| 54 | + // Step 1: Input number of bits |
| 55 | + System.out.print("Enter the number of bits in the data: "); |
| 56 | + int n = sc.nextInt(); |
| 57 | + int[] data = new int[n]; |
| 58 | + |
| 59 | + // Step 2: Input binary sequence |
| 60 | + System.out.println("Enter the binary data bits (0s and 1s): "); |
| 61 | + for (int i = 0; i < n; i++) { |
| 62 | + data[i] = sc.nextInt(); |
| 63 | + if (data[i] != 0 && data[i] != 1) { |
| 64 | + System.out.println("Invalid input. Only 0s and 1s allowed."); |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Step 3: Choose encoding method |
| 70 | + System.out.println("\nChoose a line coding technique:"); |
| 71 | + System.out.println("1. Unipolar NRZ"); |
| 72 | + System.out.println("2. Polar NRZ"); |
| 73 | + System.out.println("3. Manchester"); |
| 74 | + System.out.println("4. Differential Manchester"); |
| 75 | + System.out.print("Enter your choice (1-4): "); |
| 76 | + int choice = sc.nextInt(); |
| 77 | + |
| 78 | + // Step 4: Call respective function |
| 79 | + switch (choice) { |
| 80 | + case 1 -> unipolarNRZ(data); |
| 81 | + case 2 -> polarNRZ(data); |
| 82 | + case 3 -> manchester(data); |
| 83 | + case 4 -> differentialManchester(data); |
| 84 | + default -> System.out.println("Invalid choice. Please select between 1 and 4."); |
| 85 | + } |
| 86 | + |
| 87 | + sc.close(); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments