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

static void Q4(ArrayList<ChallengeLine> input, double x){ //Question: Sort the i

ID: 3836421 • Letter: S

Question

static void Q4(ArrayList<ChallengeLine> input, double x){
//Question: Sort the input ChallengeLine's by their evaluation at x

Hey there, this is the last question I need to complete before the semester ends and I seriously need some help. If anyone would be kind enough to figure this out I would greatly appreciate it. (Caution: this is a challenging question)

Heres the PointXY class i can draw from

public class PointXY{


// Instance variables are declared inside the class definition and outside any methods
// Private variables cannot be access from outside the class
private double x;
private double y;


// The default constructor creates a point on the origin
public PointXY(){
this.x = 0;
this.y = 0;
}


// A constructor that sets the initial position of the point
public PointXY(double x, double y){
this.x = x;
this.y = y;
}


// public getter, or accessor, methods to allow access the private instance variables from other classes/objects
public double getX(){
return this.x;
}

public double getY(){
return this.y;
}


// public setter, or mutator, methods to allow other classes/objects to change the private instance variables
public void setX(double x){
this.x = x;
}

public void setY(double y){
this.y = y;
}


// Define how an object of this type is printed (We will cover Overrides on Friday)
@Override
public String toString(){
return "(" + x + ", " + y + ")";
}

Heres the previous questions that I completed that i feel may help:

//Q2: Setup this class to contain doubles "m" (slope) and "b" (y-intercept) with getters/setters and a constructor to
// initialize these values

private double m;
   private double b;
     
     
  
   public double getM(){
       return m;
         
   }
     
   public void setM(double m){
       this.m = m;
   }
     
   public double getB(){
       return b;
   }
  
   public void setB(double b){
       this.b = b;
   }
     
     
   public ChallengeLine(double m, double b){
       this.setM(m);
       this.setB(b);
   }
     

// Q3: Write a non-static method named intersection that takes an instance of ChallengeLine as a parameter and
// returns an instance of the PointXY class (provided) at the intersection of the input line and this line.

  
   public PointXY intersection(ChallengeLine ChallengeLine) {
       ChallengeLine = new ChallengeLine(m, b);
       double x = (ChallengeLine.b - this.b) / (this.m - ChallengeLine.m);
       double y = this.m * x + this.b;

       PointXY p = new PointXY(x,y);

       return p;
       }

Explanation / Answer

ChallengeLine.java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ChallengeLine {
   private double b, m;

   // It follows a line equation in 2D plane as
   // y = mx + b;
   // where m is the gradient, and b is the x axis intercept
   public ChallengeLine(double b, double m) {
       this.b = b;
       this.m = m;
   }

   public double getB() {
       return b;
   }

   public void setB(double b) {
       this.b = b;
   }

   public double getM() {
       return m;
   }

   public void setM(double m) {
       this.m = m;
   }

   public double getValueAtX(double x) {
       // on basis of y = mx+c, we need to return value of the equation on
       // value of x
       return m * x + b;
   }

   // This function will sort the ArrayList on basis of the value at provided x axis point
   static void Q4(ArrayList<ChallengeLine> input, double x){
      
       // passing the custom comparator
       Collections.sort(input, new ChallengeLineComparator(x));
   }
}

class ChallengeLineComparator implements Comparator<ChallengeLine> {

   double xValue;

   public ChallengeLineComparator(double xValue) {
       this.xValue = xValue;
   }

   // This function compares the value of two lines at particular x value
   @Override
   public int compare(ChallengeLine l1, ChallengeLine l2) {
       return l1.getValueAtX(xValue) > l2.getValueAtX(xValue) ? 1 : -1;
   }

}


I have implemented the functionality using the custom comparator.. Please check the comparator class which i created