Overriding print statement in java based on object class - java

I have the following code in Java
I am able to print the calculated values in printDistance method.
Output:
4.0
3.0
But I need the output in below format(Expected Output).Please help
3D Distance = 4.0
2D Distance = 3.0
public class Point2D {
double x;
double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
public double dist2D(Point2D p) {
x = (int) Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2)));
return x;
}
public static void printDistance(double d) {
System.out.println(d);
}
static public void main(String args[]) {
Point3D obj1 = new Point3D(1.0, 2.0, 3.0);
Point3D obj2 = new Point3D(3.0, 4.0,5.0);
printDistance(obj1.dist3D(obj2));
printDistance(obj1.dist2D(obj2));
}
}
class Point3D extends Point2D {
double z;
public double getZ() {
return z;
}
public Point3D(double x, double y, double z) {
super(x, y);
this.z = z;
}
public double dist3D(Point3D p) {
return (int) Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2) + +Math.pow((p.getZ() - z), 2)));
}
}

You can do it as follows:
public class Point2D {
double x;
double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
public String dist2D(Point2D p) {
x = (int) Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2)));
return "2D Distance = "+x;
}
public static void printDistance(String d) {
System.out.println(d);
}
static public void main(String args[]) {
Point3D obj1 = new Point3D(1.0, 2.0, 3.0);
Point3D obj2 = new Point3D(3.0, 4.0,5.0);
printDistance(obj1.dist3D(obj2));
printDistance(obj1.dist2D(obj2));
}
}
class Point3D extends Point2D {
double z;
public double getZ() {
return z;
}
public Point3D(double x, double y, double z) {
super(x, y);
this.z = z;
}
public String dist3D(Point3D p) {
return "3D Distance = "+ Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2) + +Math.pow((p.getZ() - z), 2)));
}
}
Output:
3D Distance = 4.0
2D Distance = 3.0

Related

Incompatible types error in Command Prompt

I have a work about calculating the distances between two points. It consist of Point Class, Line Class as well as Main Class. The following is my Point class. After working on the private double distance(Point p) method, I am unable to return p at the public double getDistance(Point p) method. I run code on Command Prompt and it shows error: incompatible types: Point cannot be converted to double. Please advice.
class Point
{
private int x;
private int y;
//Constructor
public Point()
{
//nothing
}
//Second constructor
public Point (int x, int y)
{
this.x = x;
this.y = y;
}
//Copy constructor
public Point (Point p)
{
this (p.x, p.y);
}
private double distance(Point p)
{
int dX = this.x - p.x;
int dY = this.y - p.y;
double result = Math.sqrt(dX * dX + dY * dY);
return result;
}
public double getDistance(Point p)
{
return p;
}
//getter
public int getX()
{
return x;
}
public int getY()
{
return y;
}
//setter
public void set(int x, int y)
{
this.x = x;
this.y = y;
}
public String toString ()
{
return String.format ("Given Point (%d, %d)", x, y);
}
}
You have the object Point p as your parameter and return it as a double.
In your block of code you're stating a return of the Object Point p and not a double data type.
public double getDistance(Point p) {
return p;
}
If you're just trying to calculate the distance of the object, use your distance() method. This method already returns the distance calculated as a double.
private double distance(Point p) {
int dX = this.x - p.x;
int dY = this.y - p.y;
double result = Math.sqrt(dX * dX + dY * dY);
return result;
}

How can I write a Comparator comparing multiple arguments? [duplicate]

This question already has answers here:
How to compare objects by multiple fields
(23 answers)
Closed 4 years ago.
Im trying to write a Comparator which compares two objects of the class Coordinate. The Coordinate class is Pretty simple:
public class Coordinate {
private int x, y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Now I want the Comparator to compare the x and the y value for two instances of the class Coordinate. Here is an example:
I have a Coordinate c1 which has x = 42 and y = 23. My second Coordinate c2 has x = 23 and y = 54. Now i put them both in an ArrayList an want to sort the List. I wanted to be sorted like the following:
The Coordinate with the lowest y value goes Always first, when you have two Coordinates having the same y value the Coordinate goes first which has a lower x value.
Example:
c1 (y = 4, x = 5 ) < c2 (y = 4, x = 6) < c3 (y = 5, x = 2)
So how can I write a Comparator for this Purpose?
Thank you very much!
Comparator<Coordinate> c = Comparator.comparingInt(Coordinate::getY)
.thenComparingInt(Coordinate::getX);
You can build compound comparators by means of thenComparing and thenComparingX.
var list = List.of(
new Coordinate(6, 4),
new Coordinate(2, 5),
new Coordinate(5, 4)
);
list.sort(c);
System.out.println(list);
The snippet prints
[{y=4, x=5}, {y=4, x=6}, {y=5, x=2}]
Using Comparator
import java.util.ArrayList;
import java.util.Comparator;
class Coordinate {
private int x, y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String toString() {
return "x = " + x + " y = " + y;
}
}
public class Temp {
public static void main(String[] args) {
ArrayList<Coordinate> A = new ArrayList<>();
A.add(new Coordinate(1, 2));
A.add(new Coordinate(2, 1));
A.add(new Coordinate(3, 2));
A.sort(new Comparator<Coordinate>() {
#Override
public int compare(Coordinate o1, Coordinate o2) {
if (o1.getY() < o2.getY()) {
return -1;
} else if (o1.getY() > o2.getY()) {
return 1;
} else {
if (o1.getX() < o2.getX()) {
return -1;
} else if (o1.getX() > o2.getX()) {
return 1;
}
return 0;
}
}
});
System.out.println(A.toString());
}
}
Using Comparable Interface
import java.util.ArrayList;
class Coordinate implements Comparable<Coordinate> { # Notice implementing Comparable interface
private int x, y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
#Override
public int compareTo(Coordinate o) { # implementing the abstract method of Comparable interface
if (y < o.y) {
return -1;
} else if (y > o.y) {
return 1;
} else {
if (x < o.x) {
return -1;
} else if (x > o.x) {
return 1;
}
return 0;
}
}
public String toString() {
return "x = " + x + " y = " + y;
}
}
public class Temp {
public static void main(String[] args) {
ArrayList<Coordinate> A = new ArrayList<>();
A.add(new Coordinate(1, 2));
A.add(new Coordinate(2, 1));
A.add(new Coordinate(3, 2));
A.sort(null);
System.out.println(A.toString());
}
}
Output
[x = 2 y = 1, x = 1 y = 2, x = 3 y = 2]

Java: Triangle rotation - unsuccessful concept

What do I do wrong? I get incorrect results (coordinates).
A triangle (see the picture below: blue is the original triangle, lime is the rotated one: a clone. Edge A is a fixed point).
The classes I use:
A Point class:
public class Point {
private final double x;
private final double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
#Override
public String toString() {
return "[(" + x + ") (" + y + ")]";
}
}
An abstract Object2D class:
public abstract class Object2D {
Point[] point;
public Point getPoint(int i) {
return point[i];
}
public double getLowestX() {
return Arrays.asList(point).stream()
.mapToDouble(Point::getX)
.min().getAsDouble();
}
public double getHighestX() {
return Arrays.asList(point).stream()
.mapToDouble(Point::getX)
.max().getAsDouble();
}
public double getLowestY() {
return Arrays.asList(point).stream()
.mapToDouble(Point::getY)
.min().getAsDouble();
}
public double getHighestY() {
return Arrays.asList(point).stream()
.mapToDouble(Point::getY)
.max().getAsDouble();
}
public double getLength() {
return getSide(getLowestX(), getHighestX());
}
public double getHeight() {
return getSide(getLowestY(), getHighestY());
}
private double getSide(double v1, double v2) {
return (v1 < v2) ? (0 - v1) + v2 : (0 - v2) + v1;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Point pt : point) sb.append(pt).append("\n");
return sb.toString();
}
}
A Line class:
public class Line extends Object2D {
public Line(Point point0, Point point1) {
point = new Point[2];
point[0] = point0;
point[1] = point1;
}
public double getLineLength() {
return Math.sqrt(Math.pow(getLength(), 2) + Math.pow(getHeight(), 2));
}
}
My Triangle class:
public class Triangle extends Object2D {
public Triangle(Point point0, Point point1, Point point2) {
point = new Point[3];
point[0] = point0;
point[1] = point1;
point[2] = point2;
}
public static Triangle getRotatedTriangle(Triangle triangle) {
Point point0 = triangle.getPoint(0);
Point point1 = triangle.getPoint(1);
Point point2 = triangle.getPoint(2);
Line baseLine = new Line(point0, point1);
double rotationHeight = baseLine.getHeight();
double baseLength = baseLine.getLineLength();
double sinA = rotationHeight / baseLength;
double angle = Math.asin(sinA);
double cosA = Math.cos(angle);
point1 = new Point(
(point1.getX() * cosA - point1.getY() * sinA),
(point1.getX() * sinA + point1.getY() * cosA));
point2 = new Point(
(point2.getX() * cosA - point2.getY() * sinA),
(point2.getX() * sinA + point2.getY() * cosA));
return new Triangle(point0, point1, point2);
}
}
And, of course, my main class:
public class TestDrive {
public static void main(String[] args) {
Triangle triangle = new Triangle (
new Point(-6.5, -1.5),
new Point(2.5, 7.5),
new Point(6.5, -5.5)
);
System.out.println(triangle);
System.out.println(Triangle.getRotatedTriangle(triangle));
}
}
You made 2 mistakes in your code:
You are trying to rotate with the angle α when you should be rotating with the angle -α (since you are rotating clockwise).
Your multiplication matrix is incorrect: your code will perform a rotation around the origin of the XY plane instead of rotating around the point A. For the point (x, y) rotated by the rotation matrix R around the center (a, b), the correct formula for the new rotated point (x', y') would be (x', y') = R * (x - a, y - b) + (a, b).
This should be enough for you to correct your code. As reference, here's the solution I get for the rotated triangle:
[(-6.5) (-1.5)]
[(6.227922061357855) (-1.5000000000000009)]
[(-0.1360389693210724) (-13.520815280171309)]

Writing Generic Physics Vector Class

It's been difficult to Google for a solution, because I'm not interested in the vector data strucutre. I'm interested in the phytsics vector. I'm trying to create a generic class to wrap any number type for a Vector. This is my original double vector:
public class Vector2D {
private double x, y;
Vector2D (double x, double y){
this.x = x;
this.y = y;
}
public static Vector2D random() {
return new Vector2D(Math.random() * 2 - 1, Math.random() * 2 - 1);
}
public double magnitude(){
return Math.sqrt(this.x * this.x + this.y * this.y);
}
public Vector2D add(Vector2D other) {
return new Vector2D(this.x + other.x, this.y + other.y);
}
//etc...
}
However, I'm coming across some dificulites in porting this to a generic class. This is what I have so far:
public class Vector2D<T extends Number> {
private T x, y;
Vector2D (T x, T y){
this.x = x;
this.y = y;
}
public static Vector2D random() {
return new Vector2D(Math.random() * 2 - 1, Math.random() * 2 - 1);
}
public T magnitude(){
//this gives an error: cannot cast from double to T
return (T)Math.sqrt(this.x.doubleValue() * this.x.doubleValue() + this.y.doubleValue() * this.y.doubleValue());
}
}
I'm stuck on magnitude and random. I'm really stumped. Any guidance as to how I can complete those methods?

Succinct class for rotational transforms (reducing redundancy)

I wrote this Java class for doing rotational transforms about the x,y,z axes.
public class Coord {
private double x,y,z;
public Coord(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Coord M(Coord[] M) {
return new Coord(
x*M[0].x + y*M[0].y + z*M[0].z,
x*M[1].x + y*M[1].y + z*M[1].z,
x*M[2].x + y*M[2].y + z*M[2].z);
}
public Coord xR(double theta) {
double s = Math.sin(theta);
double c = Math.cos(theta);
return M(new Coord[]{V(1,0,0), V(0,c,-s), V(0,s,c)});
}
public Coord yR(double theta) {
double s = Math.sin(theta);
double c = Math.cos(theta);
return M(new Coord[]{V(c,0,s), V(0,1,0), V(-s,0,c)});
}
public Coord zR(double theta) {
double s = Math.sin(theta);
double c = Math.cos(theta);
return M(new Coord[]{V(c,-s,0), V(s,c,0), V(0,0,1)});
}
public Coord V(double x, double y, double z) {
return new Coord(x,y,z);
}
}
It transforms a x,y,z coordinate, as given by the class Coord, to a new coord by the matrix multiplication M. It works OK, such as Coord c = new Coord(1,1,0).xR(0.1);, try System.out.printf("%f,%f,%f", c.x, c.y, c.z); will show a small rotation (theta=0.1 radians) about x, while keeping x constant.
Anyway, I want to know if there's any way of getting rid of all the Math.sin, Math.cos while still maintaining readability (preferably) - but just getting rid of them would be good!. They are annoying to look at, and, seem to be asking to be reduced somehow
As per Paul's comment, turns out to be pretty good
import static java.lang.Math.sin;
import static java.lang.Math.cos;
public class Coord {
private double x,y,z;
public Coord(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Coord M(Coord[] M) {
return new Coord(
x*M[0].x + y*M[0].y + z*M[0].z,
x*M[1].x + y*M[1].y + z*M[1].z,
x*M[2].x + y*M[2].y + z*M[2].z);
}
public Coord xR(double t) {
return M(new Coord[]{V(1,0,0), V(0,cos(t),-sin(t)), V(0,sin(t),cos(t))});
}
public Coord yR(double t) {
return M(new Coord[]{V(cos(t),0,sin(t)), V(0,1,0), V(-sin(t),0,cos(t))});
}
public Coord zR(double t) {
return M(new Coord[]{V(cos(t),-sin(t),0), V(sin(t),cos(t),0), V(0,0,1)});
}
public Coord V(double x, double y, double z) {
return new Coord(x,y,z);
}
}
Answering my own Q .. happy to know of other ways, looks pretty syntax bare now though .... I think I can live with this

Categories