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

(Java Problem)In this question we will step you through implementing classes and

ID: 3742105 • Letter: #

Question

(Java Problem)In this question we will step you through implementing classes and methods in Java for a new ride-sharing app called Rebu. Rebu connects passengers looking for a ride with drivers in their area. You can assume that the classes or methods in earlier questions “exist” in later questions, even if you haven’t answered the question. You must follow proper Object Oriented Design principles, and Java programming conventions. Write all class declarations. Do not write method signatures that are given to you. [30 marks]

(c) Implement a User class, which represents both passengers and drivers. A User is defined by their location in the world. Your class should include a constructor, appropriate getters and setters, and a toString method that returns the User’s location as a String. [4 marks]

Explanation / Answer

public class User{

  

   private String name;

   private Position pos;

  

   public User(String name) {

       super();

       this.name = name;

       pos = new Position();

   }

  

   public User(String name, Position pos) {

       super();

       this.name = name;

       this.pos = pos;

   }

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public Position getPos() {

       return pos;

   }

   public void setPos(Position pos) {

       this.pos = pos;

   }

   @Override

   public String toString() {

       return "User [name=" + name + ", pos=" + pos + "]";

   }

  

  

}

public final class Position {

/* Insert your name here */

final private double x;

final private double y;

//TODO Implement Point.Point(double x, double y)

/** instantiate a point

   * "this.x" refers to the instance variable of the object

   * x refers to the parameter

   * same for this.y and y

   */

public Position(double x, double y){

// System.out.println("Point(x,y) not implemented yet");

   this.x = x;

   this.y = y;

}

//TODO Implement Point.Point()

/**

   * Point() creates the origin by appropriately calling

   * the general Point constructor

   */

public Position(){

   // System.out.println("Point() not implemented yet");

   this.x = 0;

   this.y = 0;

}

  

//TODO Implement Point.getX

/**

   * @return x

   */

public double getX(){

   // System.out.println("getX not implemented yet");

return x;

}

//TODO Implement Point.getY

/**

   * @return y

   */

public double getY(){

   // System.out.println("getY not implemented yet");

return y;

}

// Given Point.toString

/**

   * convert to String

   */

public String toString(){

return "("+x+","+y+")";

}

//TODO Implement Point.equals

/**

   *

   * @param p other point

   * @return test for equality using epsilon, because we

   * are dealing with doubles,so roundoff can occur

   */

public boolean equals (Position p){

if (this.x==p.x && this.y==p.y)

   return true;

return false;

}

  

// Given equals(Object o)

/**

   * We need this equals method for ArrayList<Point>, because

   * the generic ArrayList is really an ArrayList of Object.

   * In the case of equals, the signature is

   * public boolean equals(Object o)

   * and the method below overrides the Object equals method

   * and the calls the class's equals(Point) method

   *

   * @see java.lang.Object#equals(java.lang.Object)

   */

public boolean equals(Object obj){

if (obj instanceof Position){

   Position p = (Position)obj;

return equals(p);

}

return false;

}

  

//TODO Implement Point.euclidDist

/**

   *

   * @param p

   * @return Euclidean distance of this point to Position p

   */

public double euclidDist(Position p){

   return Math.sqrt((this.x - p.x) *(this.x - p.x) + (this.y - p.y) *(this.y - p.y) );

}

}