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

1) Write a method that is passed in a full array of integers and shifts all the

ID: 3836747 • Letter: 1

Question

1) Write a method that is passed in a full array of integers and shifts all the data in the list down one index. The first value in the array should be stored at the end.

{5, 8, 9, 2, 1} becomes --> {8, 9, 2, 1, 5}

2) Write an instance method toString() that returns a String representation of this object, which contains the String version of each CSC142Point, separated by the ' ' Test
This uses our CSC142Point class (you'll find the source code in week 4)

Here the source code for CSC142Point class

Explanation / Answer

Ans 1. Put it in file name LeftRotate.java


public class LeftRotate {
  
  
   int array[]={5, 8, 9, 2, 1};
  
  
   static   void leftRotatebyOneShift(int array[], int n)
    {
        int i, temp;//temporary variables
        temp = array[0]; // store the leftmost integer in temp
        for (i = 0; i < n - 1; i++) //for the rest of the integers store the corresponding integer in place of previous one
        {
           array[i] = array[i + 1];
        }//after this for loop array is ={8,9,2,1,1}
        array[i] = temp;//replace the last integer with the temp value that is 5
    }

    /* utility function to print an array */
   static void printArray(int array[], int size)
    {
        int i;
        for (i = 0; i < size; i++)
            System.out.print(array[i] + " "); //It prints the array
    }

    // Driver program to test above functions
    public static void main(String[] args)
    {
       int array[]={5, 8, 9, 2, 1};
       leftRotatebyOneShift(array,array.length);
        printArray(array, array.length);
    }

}

2.The changes in the existing class has been done

/** Represents a 2-dimensional CSC142Point
* @author CSC 142
* @version 2006
*/
public class CSC142Point {
private double x, y; // the coordinates

// two different constructors

/**
   * Create a CSC142Point at (0, 0)
   */
public CSC142Point() {
    x = 0;
    y = 0;
}

/**
   * Create a CSC142Point with the given coordinates
   * @param initialX the x-coordinate
   * @param initialY the y-coordinate
   */
public CSC142Point (double initialX, double initialY) {
    x = initialX;
    y = initialY;
}

// update (mutator) methods
// that change the state of a CSC142Point object

/**
   * Set the x-coordinate of this CSC142Point
   * @param updateX the new x-coordinate
   */
public void setX(double updateX) {
    x = updateX;
}

/**
   * Set the y-coordinate of this CSC142Point
   * @param newY the new y-coordinate
   */
public void setY(double updateY) {
    y = updateY;
}

/**
   * Set the x and y coordinates of this CSC142Point
   * @param newX the new x-coordinate
   * @param newY the new y-coordinate
   */
public void setPoint( double newX, double newY){
    x = newX;
    y = newY;
}

// query (accessor) methods
// that somehow report the state of a CSC142Point
// without changing it

/**
   * Get the x-coordinate of this CSC142Point
   * @return the x-coordinate
   */
public double getX() {
    return x;
}

/**
   * Get the y-coordinate of this CSC142Point
   * @return the y-coordinate
   */
public double getY() {
    return y;
}

/**
   * Calculate the distance between this CSC142Point and the origin
   * @return the distance to (0, 0)
   */
public double distanceToOrigin() {
    return Math.sqrt(x * x + y * y);
}


/** Calculate the distance between this CSC142Point and some other CSC142Point
   * @param other the other CSC142Point
   * @return the distance between the 2 CSC142Points
   */
public double distance(CSC142Point other) {
    double diffX = x - other.x;
    double diffY = y - other.y;
    return Math.sqrt(diffX * diffX + diffY * diffY);
}


/**
   * Find the midpoint between this CSC142Point and another CSC142Point
   * @param p the other CSC142Point
   * @return the CSC142Point midway between the two CSC142Points
   */
public CSC142Point midPoint(CSC142Point other) {
    double midX = (x + other.x) / 2;
    double midY = (y + other.y) / 2;
    return new CSC142Point(midX, midY);
}

/**
   * The String version of this CSC142Point
   * @return the String representation
   */
//The String representation of both x and y points separated by
public String toString() {
    return "x coordinate is " + x + " "+"y coordinate is " + y ;
}


/**
   * a test method - do not change this code!
   * it should display 3 results in a terminal window
   */
public static void test (){
      CSC142Point alpha = new CSC142Point( 5, 5 );
      CSC142Point beta = new CSC142Point( -3, 7 );
      System.out.println( "alpha is " + alpha.distanceToOrigin() + " from the origin" );
      System.out.println( "The x coordinate of beta is " + beta.getX() );
      beta.setY( 72 );
      System.out.println( "The y coordinate of beta is " + beta.getY() );
      //here are the string representation of alpha and beta objects
      System.out.println(alpha.toString());
      System.out.println(beta.toString());
    
    }

// the added main method to run the class
public static void main(String[] args)
{
      test();
}

}

Feel free to ask in case of any doubt