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

I need help creating test cases for my code, two per class My code below -------

ID: 3870998 • Letter: I

Question

I need help creating test cases for my code, two per class

My code below

-----------

public class Line {

private Point p1, p2;

/**
* Initializes a newly created Line object with the given values.
*
* @param x1
* and x2 the x coordinates of p1 and p2
* @param y1
* and y2 the y coordinates of p1 and p2
*/
public Line(int x1, int y1, int x2, int y2) {
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
}

/**
* Initializes a newly created Line object with the values from the two
* input Point objects.
*
* @param p1
* and p2 two Point objects used to initialize this Line object
*/
public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}

/**
* Calculate the slope of this Line object using the formula (y1 - y2)/(x1 -
* x2)
*
* slope of a vertical line is undefined, that is, x1 and x2 are equal,
* throw an ArithmeticException
*
* @return the slope of this Line object
*/
public double getSlope() {
double sl = ((double) p1.getY() - p2.getY()) / ((double) p1.getX() - p2.getX());
return sl;
}

/**
* Calculate the distance between the two points of this Line object
*
* @return the distance
*/
public double getDistance() {

return Math.sqrt(Math.pow((p2.getX() - p1.getX()), 2) + Math.pow((p2.getY() - p1.getY()), 2));
}

/**
* Calculate the middle point of this Line object
*
* @return a Point object
*/
public Point getMidpoint() {
Point p = new Point((p1.getX() + p2.getX()) / 2,
((p1.getY() + p2.getY()) / 2));
return p;
}

/**
* two lines are parallel if they have the same slope, or if they are both
* vertical. Note that two slopes are the same if their difference is very
* small.
*
* @param line
* the other Line object
*
* @return true if the objects are parallel; false otherwise.
*/
public boolean parallelTo(Line line) {
if (this.getSlope() == line.getSlope())
return true;
return false;
}

/**
* Compares this object to the other object. The result is true if and only
* if the argument is not null and is a Line object with the same values as
* this Line object
*
* @param obj
* the object to compare with.
*
* @return true if the objects are the same; false otherwise.
*/

public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Line other = (Line) obj;
if (p1 == null) {
if (other.p1 != null)
return false;
} else if (!p1.equals(other.p1))
return false;
if (p2 == null) {
if (other.p2 != null)
return false;
} else if (!p2.equals(other.p2))
return false;
return true;
}

/**
* Returns a String object that represents this Line
*
* @return a string representation of this Line's value.
*/
public String toString() {
return "[" + p1 + "," + p2 + "]";
}

}

----------

Explanation / Answer

Point.java

public class Point {
private int x;
private int y;
/**
* Initializes a newly created Point object with x and y
* coordinates set to 0.
*/
public Point() {}
/**
* Initializes a newly created Point object with the given
* values.
*
* @param x the x coordinate of this point
* @param y the y coordinate of this point
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Initializes a newly created Point object with the values
* from the input string. Throws an IllegalArgumentException
* if parameter str is a null reference or contains more
* than two values; sets both instance variables to zero if
* str contains nothing but space characters.  
*
* @param str string containing values of coordinates, such
* as "10,20".
*/
public Point(String str) {
String arr[] = str.split(",");
this.x = Integer.parseInt(arr[0]);
this.y = Integer.parseInt(arr[1]);
}
/**
* Initializes a newly created Point object with the values
* from the input Point object.
*
* @param other a Point object used to initialize this Point
* object
*/
public Point(Point other) {
this.x = other.x;
this.y = other.y;
}
/**
* Returns the x coordinate of this Point object.
*
* @return the x coordinate of this object.
*/
public int getX() {
return x;
}
/**
* Returns the y coordinate of this Point object.
*
* @return the y coordinate of this object.
*/
public int getY() {
return y;
}
/**
* Returns a String object that represents this Point as,
* for example, (5, 3) if x is 5 and y is 3.
*
* @return a string representation of this Point's value.
*/
public String toString() {
return "(" + x + "," + y + ")";
}

}

_________________

Line.java

public class Line {
private Point p1, p2;

/**
* Initializes a newly created Line object with the given values.
*
* @param x1
* and x2 the x coordinates of p1 and p2
* @param y1
* and y2 the y coordinates of p1 and p2
*/
public Line(int x1, int y1, int x2, int y2) {
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
}

/**
* Initializes a newly created Line object with the values from the two
* input Point objects.
*
* @param p1
* and p2 two Point objects used to initialize this Line object
*/
public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}

/**
* Calculate the slope of this Line object using the formula (y1 - y2)/(x1 -
* x2)
*
* slope of a vertical line is undefined, that is, x1 and x2 are equal,
* throw an ArithmeticException
*
* @return the slope of this Line object
*/
public double getSlope() {
double sl = ((double) p1.getY() - p2.getY()) / ((double) p1.getX() - p2.getX());
return sl;
}

/**
* Calculate the distance between the two points of this Line object
*
* @return the distance
*/
public double getDistance() {
return Math.sqrt(Math.pow((p2.getX() - p1.getX()), 2) + Math.pow((p2.getY() - p1.getY()), 2));
}

/**
* Calculate the middle point of this Line object
*
* @return a Point object
*/
public Point getMidpoint() {
Point p = new Point((p1.getX() + p2.getX()) / 2,
((p1.getY() + p2.getY()) / 2));
return p;
}

/**
* two lines are parallel if they have the same slope, or if they are both
* vertical. Note that two slopes are the same if their difference is very
* small.
*
* @param line
* the other Line object
*
* @return true if the objects are parallel; false otherwise.
*/
public boolean parallelTo(Line line) {
if (this.getSlope() == line.getSlope())
return true;
return false;
}

/**
* Compares this object to the other object. The result is true if and only
* if the argument is not null and is a Line object with the same values as
* this Line object
*
* @param obj
* the object to compare with.
*
* @return true if the objects are the same; false otherwise.
*/
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Line other = (Line) obj;
if (p1 == null) {
if (other.p1 != null)
return false;
} else if (!p1.equals(other.p1))
return false;
if (p2 == null) {
if (other.p2 != null)
return false;
} else if (!p2.equals(other.p2))
return false;
return true;
}

/**
* Returns a String object that represents this Line
*
* @return a string representation of this Line's value.
*/
public String toString() {
return "[" + p1 + "," + p2 + "]";
}
}

___________________

Test.java

public class Test {

public static void main(String[] args) {
//Creating an Two Point class objects
Point p1 = new Point(3, 4);
Point p2 = new Point(4, 5);

//Creating an Line class class object by passing Point class Objects as arguments
Line l1 = new Line(p1, p2);

//Displaying the slope and distance and mid point
System.out.println("--- Line#1 ---");
System.out.println("Slope of the Point#1 " + p1 + " and Point#2 " + p2 + " is :" + l1.getSlope());
System.out.println("Distance between Point#1 " + p1 + " and Point#2 " + p2 + " is :" + l1.getDistance());
System.out.println("Mid Point of the line whose end Points" + p1 + " and " + p2 + " is :" + l1.getMidpoint());

//Creating an Line class class object by passing the x1,y1,x2,y2 as arguments
Line l2 = new Line(1, 2, 8, 9);

//Displaying the slope and distance and mid point
System.out.println("--- Line#2 ---");
System.out.println("Slope of the line having Points " + l2.toString() + " is :" + l2.getSlope());
System.out.println("Distance between Points " + l2.toString() + " is :" + l2.getDistance());
System.out.println("Mid Point of the line whose end Points " + l2.toString() + " is :" + l2.getMidpoint());

}

}

______________________

Output:

--- Line#1 ---
Slope of the Point#1 (3,4) and Point#2 (4,5) is :1.0
Distance between Point#1 (3,4) and Point#2 (4,5) is :1.4142135623730951
Mid Point of the line whose end Points(3,4) and (4,5) is :(3,4)
--- Line#2 ---
Slope of the line having Points [(1,2),(8,9)] is :1.0
Distance between Points [(1,2),(8,9)] is :9.899494936611665
Mid Point of the line whose end Points [(1,2),(8,9)] is :(4,5)


_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote