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

java program modify the origin code, implement the design 2, 3 and 5. Cannot pos

ID: 3886504 • Letter: J

Question

java program

modify the origin code, implement the design 2, 3 and 5. Cannot posted the picture, this is the link: https://imgur.com/PCHELnX

Codes:

public class PointCP
{
//Instance variables ************************************************

/**
   * Contains C(artesian) or P(olar) to identify the type of
   * coordinates that are being dealt with.
   */
private char typeCoord;

/**
   * Contains the current value of X or RHO depending on the type
   * of coordinates.
   */
private double xOrRho;

/**
   * Contains the current value of Y or THETA value depending on the
   * type of coordinates.
   */
private double yOrTheta;


//Constructors ******************************************************

/**
   * Constructs a coordinate object, with a type identifier.
   */
public PointCP(char type, double xOrRho, double yOrTheta)
{
    if(type != 'C' && type != 'P')
      throw new IllegalArgumentException();
    this.xOrRho = xOrRho;
    this.yOrTheta = yOrTheta;
    typeCoord = type;
}


//Instance methods **************************************************


public double getX()
{
    if(typeCoord == 'C')
      return xOrRho;
    else
      return (Math.cos(Math.toRadians(yOrTheta)) * xOrRho);
}

public double getY()
{
    if(typeCoord == 'C')
      return yOrTheta;
    else
      return (Math.sin(Math.toRadians(yOrTheta)) * xOrRho);
}

public double getRho()
{
    if(typeCoord == 'P')
      return xOrRho;
    else
      return (Math.sqrt(Math.pow(xOrRho, 2) + Math.pow(yOrTheta, 2)));
}

public double getTheta()
{
    if(typeCoord == 'P')
      return yOrTheta;
    else
      return Math.toDegrees(Math.atan2(yOrTheta, xOrRho));
}


/**
   * Converts Cartesian coordinates to Polar coordinates.
   */
public void convertStorageToPolar()
{
    if(typeCoord != 'P')
    {
      //Calculate RHO and THETA
      double temp = getRho();
      yOrTheta = getTheta();
      xOrRho = temp;
     
      typeCoord = 'P'; //Change coord type identifier
    }
}

/**
   * Converts Polar coordinates to Cartesian coordinates.
   */
public void convertStorageToCartesian()
{
    if(typeCoord != 'C')
    {
      //Calculate X and Y
      double temp = getX();
      yOrTheta = getY();
      xOrRho = temp;
  
      typeCoord = 'C'; //Change coord type identifier
    }
}

/**
   * Calculates the distance in between two points using the Pythagorean
   * theorem (C ^ 2 = A ^ 2 + B ^ 2). Not needed until E2.30.
   *
   * @param pointA The first point.
   * @param pointB The second point.
   * @return The distance between the two points.
   */
public double getDistance(PointCP pointB)
{
    // Obtain differences in X and Y, sign is not important as these values
    // will be squared later.
    double deltaX = getX() - pointB.getX();
    double deltaY = getY() - pointB.getY();
   
    return Math.sqrt((Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));
}

/**
   * Rotates the specified point by the specified number of degrees.
   * Not required until E2.30
   *
   * @param point The point to rotate
   * @param rotation The number of degrees to rotate the point.
   * @return The rotated image of the original point.
   */
public PointCP rotatePoint(double rotation)
{
    double radRotation = Math.toRadians(rotation);
    double X = getX();
    double Y = getY();
       
    return new PointCP('C',
      (Math.cos(radRotation) * X) - (Math.sin(radRotation) * Y),
      (Math.sin(radRotation) * X) + (Math.cos(radRotation) * Y));
}

/**
   * Returns information about the coordinates.
   *
   * @return A String containing information about the coordinates.
   */
public String toString()
{
    return "Stored as " + (typeCoord == 'C'
       ? "Cartesian (" + getX() + "," + getY() + ")"
       : "Polar [" + getRho() + "," + getTheta() + "]") + " ";
}
}

public class PointCPTest
{
//Class methods *****************************************************

/**
   * This method is responsible for the creation of the PointCP
   * object. This can be done in two ways; the first, by using the
   * command line and running the program using <code> java
   * PointCPTest &lt;coordtype (c/p)&gt; &lt;X/RHO&gt; &lt;Y/THETA&gt;
   * </code> and the second by getting the program to prompt the user.
   * If the user does not enter a valid sequence at the command line,
   * the program will prompte him or her.
   *
   * @param args[0] The coordinate type. P for polar and C for
   *                cartesian.
   * @param args[1] The value of X or RHO.
   * @param args[2] The value of Y or THETA.
   */
public static void main(String[] args)
{
    PointCP point;

    System.out.println("Cartesian-Polar Coordinates Conversion Program");

    // Check if the user input coordinates from the command line
    // If he did, create the PointCP object from these arguments.
    // If he did not, prompt the user for them.
    try
    {
      point = new PointCP(args[0].toUpperCase().charAt(0),
        Double.valueOf(args[1]).doubleValue(),
        Double.valueOf(args[2]).doubleValue());
    }
    catch(Exception e)
    {
      // If we arrive here, it is because either there were no
      // command line arguments, or they were invalid
      if(args.length != 0)
        System.out.println("Invalid arguments on command line");

      try
      {
        point = getInput();
      }
      catch(IOException ex)
      {
        System.out.println("Error getting input. Ending program.");
        return;
      }
    }
    System.out.println(" You entered: " + point);
    point.convertStorageToCartesian();
    System.out.println(" After asking to store as Cartesian: " + point);
    point.convertStorageToPolar();
    System.out.println(" After asking to store as Polar: " + point);
}

/**
   * This method obtains input from the user and verifies that
   * it is valid. When the input is valid, it returns a PointCP
   * object.
   *
   * @return A PointCP constructed using information obtained
   *         from the user.
   * @throws IOException If there is an error getting input from
   *         the user.
   */
private static PointCP getInput() throws IOException
{
    byte[] buffer = new byte[1024]; //Buffer to hold byte input
    boolean isOK = false; // Flag set if input correct
    String theInput = ""; // Input information
   
    //Information to be passed to the constructor
    char coordType = 'A'; // Temporary default, to be set to P or C
    double a = 0.0;
    double b = 0.0;

    // Allow the user to enter the three different arguments
    for (int i = 0; i < 3; i++)
    {
      while (!(isOK))
      {
        isOK = true; //flag set to true assuming input will be valid
         
        // Prompt the user
        if (i == 0) // First argument - type of coordinates
        {
          System.out.print("Enter the type of Coordinates you "
            + "are inputting ((C)artesian / (P)olar): ");
        }
        else // Second and third arguments
        {
          System.out.print("Enter the value of "
            + (coordType == 'C'
              ? (i == 1 ? "X " : "Y ")
              : (i == 1 ? "Rho " : "Theta "))
            + "using a decimal point(.): ");
        }

        // Get the user's input     
      
        // Initialize the buffer before we read the input
        for(int k=0; k<1024; k++)
        buffer[k] = 'u0020';       
            
        System.in.read(buffer);
        theInput = new String(buffer).trim();
       
        // Verify the user's input
        try
        {
          if (i == 0) // First argument -- type of coordinates
          {
            if (!((theInput.toUpperCase().charAt(0) == 'C')
              || (theInput.toUpperCase().charAt(0) == 'P')))
            {
              //Invalid input, reset flag so user is prompted again
              isOK = false;
            }
            else
            {
              coordType = theInput.toUpperCase().charAt(0);
            }
          }
          else // Second and third arguments
          {
            //Convert the input to double values
            if (i == 1)
              a = Double.valueOf(theInput).doubleValue();
            else
              b = Double.valueOf(theInput).doubleValue();
          }
        }
        catch(Exception e)
        {
        System.out.println("Incorrect input");
        isOK = false; //Reset flag as so not to end while loop
        }
      }

      //Reset flag so while loop will prompt for other arguments
      isOK = false;
    }
    //Return a new PointCP object
    return (new PointCP(coordType, a, b));
}
}

Explanation / Answer

Design2:

public class PointCP {

// Instance variables ************************************************

/**

* Contains C(artesian) or P(olar) to identify the type of coordinates that

* are being dealt with.

*/

private char typeCoord;

/**

* Contains the current value of X or RHO depending on the type of

* coordinates.

*/

private double xOrRho;

/**

* Contains the current value of Y or THETA value depending on the type of

* coordinates.

*/

private double yOrTheta;

// Constructors ******************************************************

/**

* Constructs a coordinate object, with a type identifier.

*/

public PointCP(char type, double xOrRho, double yOrTheta) {

if (type != 'C' && type != 'P')

throw new IllegalArgumentException();

if(type == 'P') {

this.xOrRho = xOrRho;

this.yOrTheta = yOrTheta;

} else {

this.xOrRho = (Math.sqrt(Math.pow(xOrRho, 2) + Math.pow(yOrTheta, 2)));

this.yOrTheta = Math.toDegrees(Math.atan2(yOrTheta, xOrRho));

}

typeCoord = 'P';

}

// Instance methods **************************************************

public double getX() {

return (Math.cos(Math.toRadians(yOrTheta)) * xOrRho);

}

public double getY() {

return (Math.sin(Math.toRadians(yOrTheta)) * xOrRho);

}

public double getRho() {

return xOrRho;

}

public double getTheta() {

return yOrTheta;

}

/**

* Converts Cartesian coordinates to Polar coordinates.

*/

public void convertStorageToPolar() {

return; // it is already polar

}

/**

* Converts Polar coordinates to Cartesian coordinates.

*/

public void convertStorageToCartesian() {

// do not convert, as we always store only polar coordinate

}

/**

* Calculates the distance in between two points using the Pythagorean

* theorem (C ^ 2 = A ^ 2 + B ^ 2). Not needed until E2.30.

*

* @param pointA

* The first point.

* @param pointB

* The second point.

* @return The distance between the two points.

*/

public double getDistance(PointCP pointB) {

// Obtain differences in X and Y, sign is not important as these values

// will be squared later.

double deltaX = getX() - pointB.getX();

double deltaY = getY() - pointB.getY();

return Math.sqrt((Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));

}

/**

* Rotates the specified point by the specified number of degrees. Not

* required until E2.30

*

* @param point

* The point to rotate

* @param rotation

* The number of degrees to rotate the point.

* @return The rotated image of the original point.

*/

public PointCP rotatePoint(double rotation) {

double radRotation = Math.toRadians(rotation);

double X = getX();

double Y = getY();

return new PointCP('C', (Math.cos(radRotation) * X)

- (Math.sin(radRotation) * Y), (Math.sin(radRotation) * X)

+ (Math.cos(radRotation) * Y));

}

/**

* Returns information about the coordinates.

*

* @return A String containing information about the coordinates.

*/

public String toString() {

return "Stored as "

+ (typeCoord == 'C' ? "Cartesian (" + getX() + "," + getY()

+ ")" : "Polar [" + getRho() + "," + getTheta() + "]")

+ " ";

}

}



Design 3:

public class PointCP

{

//Instance variables ************************************************

/**

* Contains C(artesian) or P(olar) to identify the type of

* coordinates that are being dealt with.

*/

private char typeCoord;

/**

* Contains the current value of X or RHO depending on the type

* of coordinates.

*/

private double xOrRho;

/**

* Contains the current value of Y or THETA value depending on the

* type of coordinates.

*/

private double yOrTheta;

//Constructors ******************************************************

/**

* Constructs a coordinate object, with a type identifier.

*/

public PointCP(char type, double xOrRho, double yOrTheta)

{

if(type != 'C' && type != 'P')

throw new IllegalArgumentException();

if(type=='C') {

this.xOrRho = xOrRho;

this.yOrTheta = yOrTheta;

} else {

this.xOrRho = (Math.cos(Math.toRadians(yOrTheta)) * xOrRho);

this.yOrTheta = (Math.sin(Math.toRadians(yOrTheta)) * xOrRho);

}

typeCoord = 'C';

}

//Instance methods **************************************************

public double getX()

{

return xOrRho;

}

public double getY()

{

return yOrTheta;

}

public double getRho()

{

return (Math.sqrt(Math.pow(xOrRho, 2) + Math.pow(yOrTheta, 2)));

}

public double getTheta()

{

return Math.toDegrees(Math.atan2(yOrTheta, xOrRho));

}

/**

* Converts Cartesian coordinates to Polar coordinates.

*/

public void convertStorageToPolar()

{

// no need as we always store cartesian only

return;

}

/**

* Converts Polar coordinates to Cartesian coordinates.

*/

public void convertStorageToCartesian()

{

// no need as we always store cartesian only

return;

}

/**

* Calculates the distance in between two points using the Pythagorean

* theorem (C ^ 2 = A ^ 2 + B ^ 2). Not needed until E2.30.

*

* @param pointA The first point.

* @param pointB The second point.

* @return The distance between the two points.

*/

public double getDistance(PointCP pointB)

{

// Obtain differences in X and Y, sign is not important as these values

// will be squared later.

double deltaX = getX() - pointB.getX();

double deltaY = getY() - pointB.getY();

return Math.sqrt((Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));

}

/**

* Rotates the specified point by the specified number of degrees.

* Not required until E2.30

*

* @param point The point to rotate

* @param rotation The number of degrees to rotate the point.

* @return The rotated image of the original point.

*/

public PointCP rotatePoint(double rotation)

{

double radRotation = Math.toRadians(rotation);

double X = getX();

double Y = getY();

return new PointCP('C',

(Math.cos(radRotation) * X) - (Math.sin(radRotation) * Y),

(Math.sin(radRotation) * X) + (Math.cos(radRotation) * Y));

}

/**

* Returns information about the coordinates.

*

* @return A String containing information about the coordinates.

*/

public String toString()

{

return "Stored as " + (typeCoord == 'C'

? "Cartesian (" + getX() + "," + getY() + ")"

: "Polar [" + getRho() + "," + getTheta() + "]") + " ";

}

}



Design 4:
PointCP.java:

public abstract class PointCP {

public abstract double getX();

public abstract double getY();

public abstract double getRho();

public abstract double getTheta();

public abstract void convertStorageToPolar();

public abstract void convertStorageToCartesian();

public abstract double getDistance(PointCP pointB);

public abstract PointCP rotatePoint(double rotation);

}


PointC.java:

public class PointC extends PointCP

{

//Instance variables ************************************************

/**

* Contains C(artesian) or P(olar) to identify the type of

* coordinates that are being dealt with.

*/

private char typeCoord;

/**

* Contains the current value of X or RHO depending on the type

* of coordinates.

*/

private double xOrRho;

/**

* Contains the current value of Y or THETA value depending on the

* type of coordinates.

*/

private double yOrTheta;

//Constructors ******************************************************

/**

* Constructs a coordinate object, with a type identifier.

*/

public PointC(char type, double xOrRho, double yOrTheta)

{

if(type != 'C' && type != 'P')

throw new IllegalArgumentException();

if(type=='C') {

this.xOrRho = xOrRho;

this.yOrTheta = yOrTheta;

} else {

this.xOrRho = (Math.cos(Math.toRadians(yOrTheta)) * xOrRho);

this.yOrTheta = (Math.sin(Math.toRadians(yOrTheta)) * xOrRho);

}

typeCoord = 'C';

}

//Instance methods **************************************************

public double getX()

{

return xOrRho;

}

public double getY()

{

return yOrTheta;

}

public double getRho()

{

return (Math.sqrt(Math.pow(xOrRho, 2) + Math.pow(yOrTheta, 2)));

}

public double getTheta()

{

return Math.toDegrees(Math.atan2(yOrTheta, xOrRho));

}

/**

* Converts Cartesian coordinates to Polar coordinates.

*/

public void convertStorageToPolar()

{

// no need as we always store cartesian only

return;

}

/**

* Converts Polar coordinates to Cartesian coordinates.

*/

public void convertStorageToCartesian()

{

// no need as we always store cartesian only

return;

}

/**

* Calculates the distance in between two points using the Pythagorean

* theorem (C ^ 2 = A ^ 2 + B ^ 2). Not needed until E2.30.

*

* @param pointA The first point.

* @param pointB The second point.

* @return The distance between the two points.

*/

public double getDistance(PointCP pointB)

{

// Obtain differences in X and Y, sign is not important as these values

// will be squared later.

double deltaX = getX() - pointB.getX();

double deltaY = getY() - pointB.getY();

return Math.sqrt((Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));

}

/**

* Rotates the specified point by the specified number of degrees.

* Not required until E2.30

*

* @param point The point to rotate

* @param rotation The number of degrees to rotate the point.

* @return The rotated image of the original point.

*/

public PointCP rotatePoint(double rotation)

{

double radRotation = Math.toRadians(rotation);

double X = getX();

double Y = getY();

return new PointC('C',

(Math.cos(radRotation) * X) - (Math.sin(radRotation) * Y),

(Math.sin(radRotation) * X) + (Math.cos(radRotation) * Y));

}

/**

* Returns information about the coordinates.

*

* @return A String containing information about the coordinates.

*/

public String toString()

{

return "Stored as " + (typeCoord == 'C'

? "Cartesian (" + getX() + "," + getY() + ")"

: "Polar [" + getRho() + "," + getTheta() + "]") + " ";

}

}



PointP.java:

public class PointP {

// Instance variables ************************************************

/**

* Contains C(artesian) or P(olar) to identify the type of coordinates that

* are being dealt with.

*/

private char typeCoord;

/**

* Contains the current value of X or RHO depending on the type of *

* coordinates.

*/

private double xOrRho;

/**

*

* Contains the current value of Y or THETA value depending on the type of

*

* coordinates.

*/

private double yOrTheta;

// Constructors ******************************************************

/**

*

* Constructs a coordinate object, with a type identifier.

*/

public PointP(char type, double xOrRho, double yOrTheta) {

if (type != 'C' && type != 'P')

throw new IllegalArgumentException();

if (type == 'P') {

this.xOrRho = xOrRho;

this.yOrTheta = yOrTheta;

} else {

this.xOrRho = (Math.sqrt(Math.pow(xOrRho, 2)

+ Math.pow(yOrTheta, 2)));

this.yOrTheta = Math.toDegrees(Math.atan2(yOrTheta, xOrRho));

}

typeCoord = 'P';

}

// Instance methods **************************************************

public double getX() {

return (Math.cos(Math.toRadians(yOrTheta)) * xOrRho);

}

public double getY() {

return (Math.sin(Math.toRadians(yOrTheta)) * xOrRho);

}

public double getRho() {

return xOrRho;

}

public double getTheta() {

return yOrTheta;

}

/**

*

* Converts Cartesian coordinates to Polar coordinates.

*/

public void convertStorageToPolar() {

return; // it is already polar

}

/**

*

* Converts Polar coordinates to Cartesian coordinates.

*/

public void convertStorageToCartesian() {

// do not convert, as we always store only polar coordinate

}

/**

*

* Calculates the distance in between two points using the Pythagorean

*

* theorem (C ^ 2 = A ^ 2 + B ^ 2). Not needed until E2.30.

*

*

*

* @param pointA

*

* The first point.

*

* @param pointB

*

* The second point.

*

* @return The distance between the two points.

*/

public double getDistance(PointCP pointB) {

// Obtain differences in X and Y, sign is not important as these values

// will be squared later.

double deltaX = getX() - pointB.getX();

double deltaY = getY() - pointB.getY();

return Math.sqrt((Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));

}

/**

*

* Rotates the specified point by the specified number of degrees. Not

*

* required until E2.30

*

*

*

* @param point

*

* The point to rotate

*

* @param rotation

*

* The number of degrees to rotate the point.

*

* @return The rotated image of the original point.

*/

public PointCP rotatePoint(double rotation) {

double radRotation = Math.toRadians(rotation);

double X = getX();

double Y = getY();

return new PointC('C', (Math.cos(radRotation) * X)

- (Math.sin(radRotation) * Y), (Math.sin(radRotation) * X)

+ (Math.cos(radRotation) * Y));

}

/**

*

* Returns information about the coordinates.

*

*

*

* @return A String containing information about the coordinates.

*/

public String toString() {

return "Stored as "

+ (typeCoord == 'C' ? "Cartesian (" + getX() + "," + getY()

+ ")" : "Polar [" + getRho() + "," + getTheta() + "]")

+ " ";

}

}