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

Questions: a) What does the object referred to by reference handler look like wh

ID: 3849357 • Letter: Q

Question

Questions:

a) What does the object referred to by reference handler look like when statement (1) has been executed? Draw the object.

b) Implement the method circularShiftLeft.

c) Implement the method circularShiftRight.

Please explain step by step how to think (and step by step what to do) when you approach these kind of questions :)

The class DigitHandler manages a sequence of digits in different ways class Digit Handler private byte digits; public Digit Handler (String digits) this. digits new byte digits. length for (int pos 0; pos this digits length; post this digits [pos] (byte) (digits. charAt (pos) 48) public string toString o String Builder sb new stringBuilder for (byte digit digits sb.append (digit) return sb toString public void circularshiftLeft code is missing here public void circular shiftRight Code is missing here An instance of class DigitHandler is created and used like this: Digit Handler handler new Digit Handler ("12345" (1) System. out printin (handler); Systems out. printin 0; handler .circularshiftLeft System, 211t. printin (handler) handler circularshiftLeft System out printin (handler) handler circularshiftRight System. out printin (handler) handler circular ShiftRight System, out printin (handler) When this code fragment is executed, the following output is generated: 12345 23451 34512 23451 12345

Explanation / Answer

Answer for part a)

The handler is initialized with a string "12345". The constructor of DigitHandler breaks this input into byte array and converts the character '1', '2' , '3', '4', '5' to their numeric equivalent and stores in the byte array. 48 represents the ASCII value of '0'. i.e Numbers start from Ascii value of 48 for 0, 49 for 1 and so on. So to convert the character to its numeric value , 48 is subtracted and stored in the byte array.

The line (1) uses the toString() method to display the handler. The toString() method simply creates a string representation of the byte array and returns it. The output will be 12345

handler will have its digits[] = { 1, 2, 3, 4, 5} after the constructor is called.

Answer for b and c

Circular shift left moves all the digits 1 position left with the 1st digit coming back to end of the array.

Circular shift right moves all the digits 1 position right with the last digit coming back to the beginning of the array.

The following code implements it and output is shown.

Please don't forget to rate the answer if it helped. Please post a comment in case of any doubts and I shall respond. Thank you very much.

public class DigitHandler {

   private byte[] digits;

   public DigitHandler(String digits){

       this.digits = new byte[digits.length()];

       for(int pos = 0; pos < this.digits.length ; pos++){

           this.digits[pos] = (byte) (digits.charAt(pos) - 48);

       }      

          

   }

  

   public String toString()

   {

       StringBuilder sb = new StringBuilder("");

       for(byte digit : digits)

           sb.append(digit);

       return sb.toString();

   }

   //when we do circular shift left, all digits will move 1 position to left and the first digit

   //moves circularly into the last position

   public void circularShiftLeft()

   {

       int len = digits.length;

       byte first = digits[0]; //copy the first digit

       //starting from beginning move all digits 1 position left

       for(int i = 0; i < len -1 ; i++)

           digits[i] = digits[i+1];

       //put the first digit to end

       digits[len - 1] = first;

   }

   //when we do circular shift right, all digits move 1 position right and the last digit comes to first place

   public void circularShiftRight()

   {

       int len = digits.length;

       byte last = digits[len - 1]; //copy the last digit

      

       //starting from end, move each digit 1 postition to right

       for(int i = len - 1; i > 0 ; --i)

           digits[i] = digits[i-1];

       //copy the last digit into first place

       digits[0] = last;

   }

  

   public static void main(String[] args) {

       DigitHandler handler = new DigitHandler("12345");

       System.out.println(handler); //should print 12345

       System.out.println();

      

       handler.circularShiftLeft(); System.out.println(handler); //should print 23451

       handler.circularShiftLeft(); System.out.println(handler); //should print 34512

       handler.circularShiftRight(); System.out.println(handler); //should print 23451

       handler.circularShiftRight(); System.out.println(handler); //should print 12345

   }

}

output

12345

23451

34512

23451

12345