⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paritychecker.java

📁 本程序可以把二进制代码通过奇偶变换的方法改变
💻 JAVA
字号:
class ParityChecker{
	String bits;
	int parity;
	
	public ParityChecker(String bits, String parity){
		this.parity = Integer.parseInt(parity);
		this.bits = bits;
	}
	
	public boolean check(){
		if(bits.length() == 7){
			for(int i=0; i<7; i++){
				if(bits.charAt(i) != '0' && bits.charAt(i) != '1'){
					System.out.println("Warning: Please enter a binary number!");
					return false;
				}
			}
			if(parity != 0 && parity != 1){
				System.out.println("Warning: Please enter the type of parity(0 for even, 1 for odd)!");
				return false;
			}
			return true;
		}
		else{
			System.out.println("Warning: Please enter the binary number in 7 bits!");
			return false;
		}
	}
	
	public String calculateParity(int p){
		int numOf1 = 0;
		
		for(int i=0; i<7; i++){
			if(bits.charAt(i) == '1')
				numOf1 ++;
		}
		
		if(numOf1 % 2 == p)
			return "0";
		else
			return "1";
	}
	
	public void printResult(){
		String result;		
		if(parity == 0)
			result = calculateParity(0) + bits;		
		else
			result = calculateParity(1) + bits;			
    System.out.println("The code with a parity bit added is " + result);	  
}
	
	public static void main(String args[]){		
		ParityChecker pc = new ParityChecker(args[0],args[1]);
		if(pc.check())
			pc.printResult();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -