Question

In: Computer Science

Java question: I need to fix a point class (code below) Thank you! /** * A...

Java question: I need to fix a point class (code below) Thank you!

/**
 * A point, implemented as a location without a shape.
 */
public class Point extends Location {

   // TODO your job
   // HINT: use a circle with radius 0 as the shape!

   public Point(final int x, final int y) {
      super(-1, -1, null);
      assert x >= 0;
      assert y >= 0;
   }
}

Solutions

Expert Solution

import java.util.Objects;

public final class Point {
    private  double x;    // x-coordinate
    private  double y;    // y-coordinate

    // no parameter constructor to generate random point
    public Point() {
        x = (Math.random() * (1.0 - 0.0 + 1) + 0.0);
        y = (Math.random() * (1.0 - 0.0 + 1) + 0.0);
    }

    // parameterized constructor
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    // getters
    public double getX() { return x; }
    public double getY() { return y; }

    // Euclidean formula to find distance between two points.
    public double distanceTo(Point point) {
        double dx = this.x - point.x;
        double dy = this.y - point.y;
        return Math.sqrt(dx*dx + dy*dy);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Point point = (Point) o;
        return Double.compare(point.x, x) == 0 &&
                Double.compare(point.y, y) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }

    public String toString() {
        return "(" + x + ", " + y + ")";
    }



    // driver method
    public static void main(String[] args) {
        Point p1 = new Point();
        System.out.println("p  = " + p1);
        System.out.println("   x     = " + p1.getX());
        System.out.println("   y     = " + p1.getY());
        System.out.println();

        Point p2 = new Point(0.5, 0.5);
        System.out.println("q  = " + p2);
        System.out.println("   x     = " + p2.getX());
        System.out.println("   y     = " + p2.getY());
        System.out.println("dist(p, q) = " + p1.distanceTo(p2));

        //prints point p1 equals p2 or not
        System.out.println("Is p1 point equals p2 point? " + p1.equals(p2));
    }
}

Output:

p = (0.8087211080759387, 1.0236249374819213)
x = 0.8087211080759387
y = 1.0236249374819213

q = (0.5, 0.5)
dist(p, q) = 0.6078583697906786

Summary:

Above program is written to represent a point by taking x and y coordinates. x and y are made private so that they are no manipulated outside the Point class.

Point class contains two different constructor one parameterized constructor to create point based on the x and y values provided and another no parameter constructor to generate random point.

In main method p1 point is generated randomly and p2 is user defined point as we are setting x and y coordinates manually.

Point class also has distanceTo method to get the distance between 2 points.


Related Solutions

I keep getting the error below in zbooks. How can I fix this. Thank You JAVA...
I keep getting the error below in zbooks. How can I fix this. Thank You JAVA zyLabsUnitTest.java:14: error: cannot find symbol s = MidtermProblems.difference(75, 75); ^ symbol: method difference(int,int) location: class MidtermProblems 1 error import java.lang.Math; public class MidtermProblems { public static String difference(int a, int b) { int diff = Math.abs(a - b); String ans = "";    if (diff == 0) { ans = "EQUAL";    } else if (diff > 10) { ans = "Big Difference "...
I need this Java code translated into C Code. Thank you. //Logical is the main public...
I need this Java code translated into C Code. Thank you. //Logical is the main public class public class Logical { public static void main(String args[]) { char [][] table= {{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}}; // table contains total combinations of p,q,& r int totalTrue, totalFalse, proposition;    //proposition 1: proposition=1; start(proposition); totalTrue=0; totalFalse=0; for(int i=0;i<8;i++) {    { char o= conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0])); System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o); if(o=='T') totalTrue++; else totalFalse++;    } } finalOutput(totalTrue,totalFalse,proposition); System.out.println(" "); System.out.println(" ");    //proposition 2: proposition=2; start(proposition);...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static boolean isPrime(int num) { // Corner case if (num <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < num; i++) if (num % i == 0) return false; return true; } public static void main(String[] args) { int squares[] = new int[100]; for (int i = 1; i < squares.length; i++) squares[i-1] = i * i;...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
Need to fix this code for tc -tac-toe game .. see the code below and fix...
Need to fix this code for tc -tac-toe game .. see the code below and fix it #include <iostream> using namespace std; void display_board(); void player_turn(); bool gameover (); char turn ; bool draw = false; char board [3][3] = { {'1', '2', '3'}, { '4', '5', '6'}, { '7', '8', '9'}}; int main() { cout << " Lets play Tc- Tac- toe game " <<endl ; cout << " Player 1 [X] ----- player 2 [0] " <<endl <<endl;...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T,...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T, size> extends Throwable { private final T[] S = null ; public StackException(String s) { } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); int top = 0; return S[top]; } private boolean isEmpty() { return false; } public T pop() throws StackException { T item; if (isEmpty()) throw new StackException("Stack underflow."); int top = 0; item = S[top];...
JAVA question here, and thank you. I need ot update the following classes an fixme's on...
JAVA question here, and thank you. I need ot update the following classes an fixme's on this.canvas = null etc. Thanks! import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.Style; import edu.luc.etl.cs313.android.shapes.model.*; /** * A Visitor for drawing a shape to an Android canvas. */ public class Draw implements Visitor<Void> { // TODO entirely your job (except onCircle) private final Canvas canvas; private final Paint paint; public Draw(final Canvas canvas, final Paint paint) { this.canvas = null; // FIXME this.paint = null; //...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT