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

(a) Write a method public int reverse (int nun) which reverses the digits in a n

ID: 663339 • Letter: #

Question

(a) Write a method public int reverse (int nun) which reverses the digits in a number. For example, if the number is 4395, then its reverse is 5934. Your method need not make any allowance for numbers which end with zero (for example, the reverse of 450 will just come out as 54). (b) Given below is the drawLmne method of the Canvas class: public void drawLine(int x1, int y1,int x2, int y2) (6 marks) The method allows yo to draw a line connecting two given points, (x1,y1) and (x2 ,y2), on the current canvas. Write the following method for the Canvas class: public void drawSquares(int x, int y, int side) The method should take in (x,y) as the coordinates of the top-left corner and side as the length of the sides of the outer square to be drawn and produce nested squares as shown below: In the diagram, the edges of each square are 1 pixel wide and adjacent squares have a 1 pixel gap. Thus, if the sides of the largest square are 10 pixels, then the sides of the next smaller square should be 6 pixels, and so on. For the smallest (i.e.. the most inner) square, its sides must be at least 2 pixels but at most 5 pixels long. (6 marks)

Explanation / Answer

public static int reverse(int num){
       int ans = 0;
       while(num != 0){
           ans += num % 10;
           num /= 10;
           if(num > 0) ans *= 10;
       }
       return ans;
   }
  
   public void drawSquares(int x, int y, int side){
       while(side >= 2){
           drawLine(x, y, x + side, y);
           drawLine(x, y, x, y - side);
           drawLine(x + side, y, x + side, y - side);
           drawLine(x, y - side, x + side, y - side);
           x++;
           y--;
           side -=2;
       }
   }