java语言程序设计 第十二章(12.7、12.8、12.9)

来源:互联网 发布:淘宝网有电脑客户端吗 编辑:程序博客网 时间:2024/06/05 20:35

程序小白,希望和大家多交流,共同学习
这里写图片描述

import java.util.Scanner;public class BinaryToDex{    public static void main(String [] args)    {        Scanner input = new Scanner(System.in);        try        {            System.out.print("Enter a binary string: ");            String str = input.next();            System.out.println("The binary value for decimal number " + str + " is " + bin2Dex(str));        }        catch (NumberFormatException ex)        {            System.out.println(ex.getMessage() + " not a decimal string");        }    }    public static int bin2Dex(String str) throws NumberFormatException    {        int result = 0;        for (int i = str.length() - 1; i >= 0; i--)        {            if (!(str.charAt(i) == '0' || str.charAt(i) == '1'))            {                throw new NumberFormatException(str);            }            else                result += Math.pow(2, (str.length() - i - 1)) * (str.charAt(i) - '0');        }        return result;    }}

这里写图片描述

public class HexFormatException extends Exception{    String hexString;    public HexFormatException(String hexString)    {        super("HexFormatException: " + hexString);        this.hexString = hexString;    }    public String getHexString()    {        return hexString;    }}
import java.util.Scanner;public class HexToDecUseHexFormatException{    String hexString;    public HexToDecUseHexFormatException(String hexString)        throws HexFormatException    {        setHexString(hexString);    }    public void setHexString(String hexString)        throws HexFormatException    {        for (int i = 0; i < hexString.length(); i++)        {            if (judge(hexString.charAt(i)) == -1)            {                throw new HexFormatException(hexString + " is not a hex string");            }        }        this.hexString = hexString;    }    public String getHexString()    {        return hexString;    }    private int judge(char ch)    {        if ((ch >= '0') && (ch <= '9'))        {            return (ch - '0');        }        else if ((ch >= 'A') && (ch <= 'F'))        {            return (ch - 'A' + 10);        }        else            return -1;    }    public int hexToDec()    {        int result = 0;        for (int i = hexString.length() - 1; i >= 0; i--)        {            result += judge(hexString.charAt(i)) * (Math.pow(16, (hexString.length() - i - 1)));        }        return result;    }}
import java.util.Scanner;public class TestHexFormatException{    public static void main(String [] args)    {        Scanner input = new Scanner(System.in);        try        {            System.out.print("Enter a hex string: ");            HexToDecUseHexFormatException hex =                 new HexToDecUseHexFormatException(input.next());            System.out.println("The decimal value for hex number "                 + hex.getHexString() + " is " + hex.hexToDec());        }        catch (HexFormatException ex)        {            System.out.println(ex.getMessage());        }    }}

这里写图片描述

public class  BinaryFormatException extends Exception{    String binString;    public BinaryFormatException(String binString)    {        super(binString);        this.binString = binString;    }    public String getBinString()    {        return binString;    }}
public class Bin2DecUseBinaryFormatException{    String binString;    public  Bin2DecUseBinaryFormatException(String binString)        throws BinaryFormatException    {        setBinString(binString);    }    public void setBinString(String binString)        throws BinaryFormatException    {        for (int i = 0; i <  binString.length(); i++)        {            if ((binString.charAt(i) != '0') && (binString.charAt(i) != '1'))            {                throw new BinaryFormatException(binString + " is not a binary string");            }        }        this.binString = binString;    }    public String getBinString()    {        return binString;    }    public int bin2Dec()    {        int result = 0;        for (int i = binString.length() - 1; i >= 0; i--)        {            result += (binString.charAt(i) - '0') * Math.pow(2, binString.length() - i - 1);        }        return result;    }}
import java.util.Scanner;public class TestBin2DecUseBinaryFormatException{    public static void main(String [] args)    {        Scanner input = new Scanner(System.in);        try        {            System.out.print("Enter a bin string: ");            Bin2DecUseBinaryFormatException bin = new                 Bin2DecUseBinaryFormatException(input.next());            System.out.println("The binary value for decimal number " +                bin.getBinString() + " is " + bin.bin2Dec());        }        catch (BinaryFormatException ex)        {            System.out.println(ex.getMessage());        }    }}
阅读全文
0 0
原创粉丝点击