Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class HexadecimalDemo { public static void main(String[] args) { Hexadeci

ID: 3538131 • Letter: P

Question

public class HexadecimalDemo

{

public static void main(String[] args)

{

Hexadecimal hex1 = new Hexadecimal ("123h");

Hexadecimal hex2 = new Hexadecimal ("123h");

Hexadecimal hex4, hex5, hex6, hex7;

Hexadecimal hex8 = new Hexadecimal(1999);

System.out.println ("Hexadecimal number hex8: " + hex8);

System.out.println ("First Hexadecimal number: " + hex1);

System.out.println ("Second Hexadecimal number: " + hex2);

if (hex1.equals(hex2))

System.out.println ("hex1 and hex2 are equal.");

else

System.out.println ("hex1 and hex2 are NOT equal.");

hex4 = hex1.add(hex2);

hex5 = hex1.subtract(hex2);

hex6 = hex1.multiply(hex2);

hex7 = hex1.divide(hex2);

System.out.println (hex1 + " + " + hex2 + " is: " + hex4);

System.out.println (hex1 + " - " + hex2 + " is: " + hex5);

System.out.println (hex1 + " * " + hex2 + " is: " + hex6);

System.out.println (hex1 + " / " + hex2 + " is: " + hex7);

System.out.println ();

int number = 6;

System.out.println ("using the integer " + number + " as the

argument " +

"to the math operators ");

hex4 = hex1.add(number);

hex5 = hex1.subtract(number);

hex6 = hex1.multiply(number);

hex7 = hex1.divide(number);

System.out.println (hex1 + " + " + number + " is: " + hex4);

System.out.println (hex1 + " - " + number + " is: " + hex5);

System.out.println (hex1 + " * " + number + " is: " + hex6);

System.out.println (hex1 + " / " + number + " is: " + hex7);

String value = "6h";

System.out.println ("using the String " + """ + value + """

+ " as the argument " +

"to the math operators ");

hex4 = hex1.add(value);

hex5 = hex1.subtract(value);

hex6 = hex1.multiply(value);

hex7 = hex1.divide(value);

System.out.println (hex1 + " + " + value + " is: " + hex4);

System.out.println (hex1 + " - " + value + " is: " + hex5);

System.out.println (hex1 + " * " + value + " is: " + hex6);

System.out.println (hex1 + " / " + value + " is: " + hex7);

}

}

Explanation / Answer

public class Hexadecimal {
    String hex;
    public Hexadecimal(String hex){
        //when we try to create object, we have to be make sure the input is valid input or not.
        //if the user enter "abcdefgh", It is invalid, So it will fail to
        //build Hexadecimal object itself.
        try{
            Integer.valueOf(hex, 16);
        }catch(NumberFormatException e){
            System.out.println("Parsing Error : Invalid input....");
        }       
        this.hex = hex;
    }
   
    @Override
    public boolean equals(Object obj) {
        //check the object is Hexadecimal or not
        //if not return false;
        if(!(obj instanceof Hexadecimal)){
            return false;
        }
       
        //if it is Hexadecimal object, we will check the content is equal or not.
        //if user enter "abcd" and "ABCD" output has to be true.
        //so only here converting it to int and checking.
        return Integer.valueOf(hex, 16).equals(Integer.valueOf(((Hexadecimal)obj).toString(), 16));
    }
   
    @Override
    public int hashCode() {
        return Integer.valueOf(hex,16).hashCode();
    }
   
    //This mothod will return the hex value
    public String toString(){
        return hex;
    }
   
    //this method will add the input hexadecimal to present object hexadecimal number.
    //present hex + input hex
    //if input hex is null(I have written like this, if you want to throw NullPointerException if input is null)
    //return present hex
    public Hexadecimal add(Hexadecimal add){
        //check if input is null or not.
        //if null just return present Hexadecimal object.
        if(add == null){
            return this;
        }
        //else we need to convert hex as integer and input as integer
        //and add it and create new Hexadecimal object and return it.
        else{
            int number1 = Integer.parseInt(hex);
            int number2 = Integer.parseInt(add.toString());
            return new Hexadecimal(Integer.toHexString(number1 + number2));
        }
    }
   
    //this method will subtract the input hexadecimal from present object hexadecimal number.
    //present hex - input hex
    //if input hex is null(I have written like this, if you want to throw NullPointerException if input is null)
    //return present hex
    public Hexadecimal subtract(Hexadecimal subtract){
        //check if input is null or not.
        //if null just return present Hexadecimal object.
        if(subtract == null){
            return this;
        }
        //else we need to convert hex as integer and input as integer
        //and subtract it and create new Hexadecimal object and return it.
        else{
            int number1 = Integer.parseInt(hex);
            int number2 = Integer.parseInt(subtract.toString());
            return new Hexadecimal(Integer.toHexString(number1 - number2));
        }
    }
   
    //this method will multiply the input hexadecimal from present object hexadecimal number.
    //present hex * input hex
    //if input hex is null(I have written like this, if you want to throw NullPointerException if input is null)
    //return present hex
    public Hexadecimal multiply(Hexadecimal multiply){
        //check if input is null or not.
        //if null just return present Hexadecimal object.
        if(multiply == null){
            return this;
        }
        //else we need to convert hex as integer and input as integer
        //and multiply it and create new Hexadecimal object and return it.
        else{
            int number1 = Integer.parseInt(hex);
            int number2 = Integer.parseInt(multiply.toString());
            return new Hexadecimal(Integer.toHexString(number1 * number2));
        }
    }
   
    //this method will divide the input hexadecimal from present object hexadecimal number.
    //present hex / input hex
    //if input hex is null (I have written like this, if you want to throw NullPointerException if input is null)
    //return present hex
    public Hexadecimal divide(Hexadecimal divide){
        //check if input is null or not.
        //if null just return present Hexadecimal object.
        if(divide == null){
            return this;
        }
        //else we need to convert hex as integer and input as integer
        //and multiply it and create new Hexadecimal object and return it.
        else{
            int number1 = Integer.parseInt(hex);
            int number2 = Integer.parseInt(divide.toString());
            return new Hexadecimal(Integer.toHexString(number1 / number2));
        }
    }
}