Question
Question (Java): EDIT: i changed the code to text version so it is copy pastable. I'm not sure where to start in the assignment below
Question (Java): EDIT: i changed the code to text version so it is copy pastable.
I'm not sure where to start in the assignment below any help is appreciated.
This assignment will perform some operations on geometric objects. Specifically, the program will create lines and circles using points, and calculate standard properties, including the points of intersection between them. For this assignment you must implement various member methods in the Point2D, Line2D, and Circle2D objects. Use the provided code below to complete the assignment. The tasks are as follows:
The template code to use:
import java.util.ArrayList;
import java.util.List;
/*
* Note that having more than one class in a single file is not standard, but
* is done to simplify the assignment.
* The public/private modifier was omitted on purpose, since only one public
* class can exist in a file. The following classes are "package private,"
* meaning they are public to the package, but private everywhere else. This
* means they are public for this file, which is why the main XXcsc210hw9
* class can access them. If these were private classes, nothing could use
* them!! Having two public classes is prohibited by Java, hence the use of
* neither public/private.
*/
class Point2D {
//////////////////////////
// private members
private double x;
private double y;
//////////////////////////
// constructors
public Point2D() {
this.x = 0;
this.y = 0;
}
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
//////////////////////////
// getters and setters
public double getX() { return this.x; }
public double getY() { return this.y; }
public void setX(double x) { this.x = x; }
public void setY(double y) { this.y = y; }
//////////////////////////
// quick print info for convenience
public String info() {
return "[Point: (" +
String.format("% 6.4f",this.x) + ", " +
String.format("% 6.4f",this.y) + ")]";
}
}
class Circle2D {
//////////////////////////
// private members
private Point2D center;
private double radius;
//////////////////////////
// constructors
public Circle2D() {
this.center = new Point2D();
this.radius = 0.0;
}
public Circle2D(Point2D pt, double r) {
this.center = pt;
this.radius = r;
}
//////////////////////////
// getters and setters
public Point2D getCenter() { return this.center; }
public double getRadius() { return this.radius; }
public void setCenter(Point2D pt) { this.center = pt; }
public void setRadius(double r) { this.radius = r; }
public double getArea()
{
//Q1: implement this method
double result = 0.0;
return result;
}
public double getCircumference()
{
//Q2: implement this method
double result = 0.0;
return result;
}
public boolean pointExists(Point2D pt){
//Q5: implement this method
boolean exists = false;
return exists;
}
//////////////////////////
// quick print info for convenience
public String info() {
return "[Circle: center = " +
this.center.info() +
", r = " + this.radius +
"]";
}
}
class Line2D {
//////////////////////////
// private members
private double slope;
private double intercept;
//////////////////////////
// constructors
public Line2D() {
this.slope = 0.0;
this.intercept = 0.0;
}
public Line2D(double slope, double intercept) {
this.slope = slope;
this.intercept = intercept;
}
public Line2D(Point2D ptA, Point2D ptB) {
//Q4: implement this constructor
}
/////////////////////////
// getters and setters
public double getSlope() { return this.slope; }
public double getIntercept() { return this.intercept; }
public void setSlope(double m) { this.slope = m; }
public void setIntercept(double b) { this.intercept = b; }
public boolean pointExists(Point2D pt){
//Q3: implement this method
boolean exists = false;
return exists;
}
//////////////////////////
// quick print info for convenience
public String info() {
return "[Line: m = " +
this.slope +
", b = " +
this.intercept +
"]";
}
}
class Deg2Curve2D {
/*
* Implement a class for second-order polynomials
* of the form y = ax^2 + bx + c
* Specifically, implement getA, getB, getC,
* setA, setB, setC, appropriate constructors, and pointExists(Point2D pt)
*/
}
public class SKELcsc210hw9 {
// Intersect methods are here, in the public class
// These methods return a list holding Point2D objects
private static List
{
List
//Q6: calculate the intersection of two lines
return result;
}
private static List
{
List
// Q8: calculate the intersection(s) of two circles
return result;
}
// Calculates the intersections between a line and a circle
// This method is provided to detail how to use the objects
private static List
{
List
final double eps = 1e-7; // a number == 0 if less than eps
double m,i,a,b,r,xplus,xminus,yplus,yminus;
m = lA.getSlope(); // slope of the line
i = lA.getIntercept(); // intercept of the line
a = cB.getCenter().getX(); // x coordinate of circle center
b = cB.getCenter().getY(); // y coordinate of circle center
r = cB.getRadius(); // radius of the circle
/*
* Lines follow formula y = mx + i
* Circles follow formula r^2 = (x-a)^2 + (y-b)^2
* The following substitutes y with mx + i into the circle equation and
* solves for x using the quadratic formula.
* The resulting x value is then used to calculate y for
* the line and circle. If they match, the x,y pair is an intersecting
* point. Since there are two square roots involved (one for quadratic
* and one for the circle), there are 4 possible combinations
* to check.
* The calculations below are from expanding
* (x-a)^2 + (mx+i-b)^2 - r^2 = 0
* Note that all values are known except x.
*/
// A B C used for the quadratic formula
// need to check for +- due to sqrt
double A = m*m + 1;
double B = 2.0 * (-a + m*i - m*b);
double C = a*a - r*r + Math.pow(b-i,2.0);
double BAC = B*B - 4.0*A*C;
// Since we have to do sqrt of BAC, if it is negative then
// there are no solutions, ie no intersections.
// Just return with result being empty
if(BAC
xplus = ( -B + Math.sqrt(BAC) ) / (2.0*A);
xminus = ( -B - Math.sqrt(BAC) ) / (2.0*A);
// Circle equation is +- due to sqrt
yplus = Math.sqrt(r*r - Math.pow(xplus-a,2.0)) + b;
// Check if line equation matches the circle equation
if( Math.abs(m*xplus + i - yplus)
// This point is on both the line and the circle. Intersection!
result.add(new Point2D(xplus,yplus));
// The if/else is in case the +- solution is the same; we don't
// want to count the same point twice
else {
yminus = -Math.sqrt(r*r - Math.pow(xplus-a,2.0)) + b;
if( Math.abs(m*xplus + i - yminus)
result.add(new Point2D(xplus,yminus));
}
// Repeat above except for change xplus with xminus
// However skip if xplus and xminus the same (also if eg 0 and -0)
if(Math.abs(xplus - xminus) > eps) {
yplus = Math.sqrt(r*r - Math.pow(xminus-a,2.0)) + b;
if( Math.abs(m*xminus + i - yplus)
result.add(new Point2D(xminus,yplus));
else {
yminus = -Math.sqrt(r*r - Math.pow(xminus-a,2.0)) + b;
if( Math.abs(m*xminus + i - yminus)
result.add(new Point2D(xminus,yminus));
}
}
return result;
}
// For convenience; just swap arguments and call method above
private static List
return intersect(b,a);
}
public static void main(String[] args) {
Point2D xy = new Point2D (2.0, 3.0); // x=2, y=3
Circle2D circ = new Circle2D( xy, 1.0); // center at xy and radius=1
Line2D line = new Line2D (1.0, 0.0); // slope=1 and intercept=0
Line2D line2 = new Line2D (2.0, -1.0); // slope=2 and intercept=-1
Line2D line3 = new Line2D (1.0, 5.0); // slope=1 and intercept=5
List
System.out.print(" ");
System.out.print(" CSC210 Homework 9 ");
System.out.print("********************************************** ");
// Tests for Q1 and Q2
System.out.print(" ");
System.out.print("********************************************** ");
System.out.print("*** Testing Q1 and Q2: Circle2D methods *** ");
System.out.print("********************************************** ");
System.out.println("For " + circ.info() + ":");
System.out.printf("The area of the circle is: %4.2f" +
" ** should be 3.14 ** ", circ.getArea());
System.out.printf("The circumference of the circle is: %4.2f" +
" ** should be 6.28 ** ", circ.getCircumference());
// Tests for Q3:
System.out.print(" ");
System.out.print("********************************************* ");
System.out.print("*** Testing Q3: Line2D pointExists *** ");
System.out.print("********************************************* ");
Point2D somePoint = new Point2D(3.0,3.0);
line.setSlope(1.0);
line.setIntercept(0.0);
System.out.println("Is " +
somePoint.info() +
" on " +
line.info() + "?: " +
line.pointExists(somePoint) +
" ** should be true **");
somePoint.setX(2.0);
System.out.println("Is " +
somePoint.info() +
" on " +
line.info() + "?: " +
line.pointExists(somePoint) +
" ** should be false **");
// Test for Q4
System.out.print(" ");
System.out.print("********************************************** ");
System.out.print("*** Testing Q4: Line2D constructor *** ");
System.out.print("********************************************** ");
Point2D firstPt = new Point2D( 1.5,6.0);
Point2D secondPt = new Point2D(-1.5,0.0);
Line2D someLine = new Line2D(firstPt, secondPt);
System.out.println("someLine is " + someLine.info());
System.out.println(" ** slope should be 2.0000 **");
System.out.println(" ** intercept should be 3.0000 **");
// Test for Q5
System.out.print(" ");
System.out.print("********************************************* ");
System.out.print("*** Testing Q5: Circle2D pointExists *** ");
System.out.print("********************************************* ");
somePoint.setX(1.0);
somePoint.setY(4.0);
System.out.println("Is " +
somePoint.info() +
" on " +
circ.info() + "?: "
+ circ.pointExists(somePoint) +
" ** should be false **");
somePoint.setX(1.5);
somePoint.setY(3.0 + Math.sqrt(0.75));
System.out.println("Is " +
somePoint.info() +
" on " +
circ.info() + "?: "
+ circ.pointExists(somePoint) +
" ** should be true **");
// Test for Q6
System.out.print(" ");
System.out.print("********************************************* ");
System.out.print("*** Testing Q6: Line2D intersection *** ");
System.out.print("********************************************* ");
System.out.println("Calculating intersection between " +
line.info() + " and " +
line2.info());
intersectPoints = intersect(line, line2);
System.out.println("There were " +
intersectPoints.size() +
" intersections " +
" ** should be 1 at (1.0000, 1.0000) **");
for(Point2D pt: intersectPoints )
{
System.out.println(pt.info());
}
System.out.println(' ');
System.out.println("Calculating intersection between " +
line.info() + " and " +
line3.info());
intersectPoints = intersect(line, line3);
System.out.println("There were " +
intersectPoints.size() +
" intersections " +
" ** should be 0 **");
// Java style for-loop with iterators
// pt is each element in intersectPoints
for(Point2D pt: intersectPoints )
{
System.out.println(pt.info());
}
// Q7: The following chunk of code is copied 3 times. Make a method to
// that prints the intersections. Use the method here for each chunk.
System.out.print(" ");
System.out.print("********************************************** ");
System.out.print("*** Testing Q7: Method creation *** ");
System.out.print("********************************************** ");
// intersection between a circle and a line
intersectPoints = intersect(circ, line);
////////////////////////////////
// chunk 1
System.out.println("Calculated intersections between " +
circ.info() +
" and " +
line.info());
System.out.println("There were " +
intersectPoints.size() +
" intersections");
// This loop will not do anything if intersectPoints is empty.
for(Point2D pt: intersectPoints )
{
System.out.println(pt.info());
}
System.out.println();
// end chunk 1
////////////////////////////////
line = new Line2D(2.0,1.0);
intersectPoints = intersect(circ, line);
////////////////////////////////
// chunk 2
System.out.println("Calculated intersections between " +
circ.info() +
" and " +
line.info());
System.out.println("There were " +
intersectPoints.size() +
" intersections");
for(Point2D pt: intersectPoints )
{
System.out.println(pt.info());
}
System.out.println();
// end chunk 2
////////////////////////////////
line = new Line2D(0.5,-5.0);
intersectPoints = intersect(circ, line);
////////////////////////////////
// chunk 3
System.out.println("Calculated intersections between " +
circ.info() +
" and " +
line.info());
System.out.println("There were " + intersectPoints.size() + " intersections");
for(Point2D pt: intersectPoints )
{
System.out.println(pt.info());
}
System.out.println();
// end chunk 3
////////////////////////////////
// Determine the intersections between two Circle2D objects
System.out.print(" ");
System.out.print("********************************************* ");
System.out.print("*** Testing Q8: Circle2D intersection *** ");
System.out.print("********************************************* ");
System.out.println("Calculating circle intersection...");
intersectPoints = intersect(circ,
new Circle2D(new Point2D(3.0,2.0),1.0));
System.out.println("There were " +
intersectPoints.size() +
" intersections " +
" ** should be 2 at " +
"(2.0000, 2.0000) and (3.0000, 3.0000) **");
for(Point2D pt: intersectPoints )
{
System.out.println(pt.info());
}
// Implement the Deg2Curve2D class
// The curveA and curveB objects cannot be created without making
// the Deg2Curve2D class, so it is commented for now.
// Uncomment below once you have the Deg2Curve2D class created.
System.out.print("********************************************* ");
System.out.print("*** Testing Q9: Deg2Curve2D class *** ");
System.out.print("********************************************* ");
/*
Deg2Curve2D curveA = new Deg2Curve2D();
Deg2Curve2D curveB = new Deg2Curve2D(1.0,-2.0,1.0);
somePoint.setX(1.0);
somePoint.setY(0.0);
System.out.println("Is " +
somePoint.info() +
" on " +
curveA + "?: "
+ curveA.pointExists(somePoint) +
" ** should be false **");
System.out.println("Is " +
somePoint.info() +
" on " +
curveB + "?: "
+ curveB.pointExists(somePoint) +
" ** should be true **");
*/
System.out.print(" ");
System.out.print("********************************************* ");
System.out.print(" \"LIFE CAN ONLY BE UNDERSTOOD BACKWARD, BUT " +
" MUST BE LIVED FORWARD.\" -- KIERKEGAARD ");
System.out.print("********************************************* ");
System.out.print("*** Homework 9 complete! *** ");
System.out.print("********************************************* ");
}
}
Thank you in advance.
1. Calculate the area of a Circle2D double getArea Use the formula ??2 2. Calculate the circumference of a Circle2D ? double getCircumference() Use the formula 2?r 3. Determine whether a Point2D is on a Line2D ? boolean pointExists(Point2D pt) Plug the value into the Line2D equation (ymx +b), and see if the resulting y matches the y from the Point2D pt. If they match, then the point exists on the line. 4. Implement a Line2D constructor using two Point2D objects ? Line2D(Point2D ptA, Point2D ptB) Given two points, calculate the line the goes through these two points. Set the slope m and intercept b of the new Line2D object 5. Determine whether a Point2D is on a Circle2D boolean pointExists(Point2D pt) Similar to Q3, except plug in r into the circle equation and see if the resulting y matches. Don't forget about values!! 6. Calculate the intersection of two Line2D objects ? ListStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started