Implementing classes, Triangle isosceles - java

I need to implement a Triangle class and im stuck on comparing the lengths of the sides to determine if the triangle is indeed an isosceles. Here is what I have so far:
public class TriangleIsosceles {
private Point cornerA;
private Point cornerB;
private Point cornerC;
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public TriangleIsosceles(){
cornerA = new Point(0,0);
cornerB = new Point(10,0);
cornerC = new Point(5,5);
}
public TriangleIsosceles(int x1,int y1,int x2,int y2,int x3,int y3){
cornerA = new Point(x1,y1);
cornerB = new Point(x2,y2);
cornerC = new Point(x3,y3);
}
public String isIsosceles(String isIsosceles){
return isIsosceles;
}
}
The Point object im using is this:
public class Point {
private int x;
private int y;
public Point(){
this(0,0);
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public void printPoint(){
System.out.println(x + y);
}
public String toString(){
return "x = "+x+" y = "+y;
}
}
In another class (LineSegment) I created a method length() that determines the distance of two points. Which looks like:
public double length() {
double length = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
return length;
}
How can I use this method to help me find the lengths of the triangle in my TriangleIsosceles class?
I know I need to see if (lenghtAB == lengthBC || lengthBC == lenghtCA || lengthAB == lengthCA).

A quick, perfectly valid, solution would be to make your length method a static utility method, i.e.
public static double length(x1, y1, x2, y2)
{
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
or
public static double length(Point p1, Point p2)
{
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
You could also add the method to Point itself, i.e. in the Point class add:
public double calcDistance(Point otherPoint)
{
return Math.sqrt(Math.pow(this.x - otherPoint.x, 2) + Math.pow(this.y - otherPoint.y, 2));
}

Assuming your LineSegment class has a constructor that takes two Point objects, you should create three LineSegment objects (which you can cache in the Triangle class). Then using LineSegment#getLength() you can determine if any two sides are the same length.
Since this looks like homework I won't give you the full solution.

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;
}

Java Algorithm for finding shortest distance between 2d point and Matrix

I'm stuck on this question for a couple of days now and would really like to get some help.
I am given a 2 dimensional point in the range of (0-1 not including 1), such as (0.5,0.2), and N other points (also in the range of 0-1).
The first part of the question is to implement the "dumb" algorithm, which when given a certain point will find the point with the shortest distance from it, which has a complexity of O(N).
The part I'm stuck at, requires to build a Matrix K on K, where each "cell" will contain the points that belong to that cell. Once done, when given the original point I will need to search for the point with the shortest distance to it only in some of the cells and not the entire Matrix, which should result better complexity.
My original thought is to devide the points so that each block will have an arraylist of points that belong to him, and then to somehow go through the main block(the one that the original point belongs to) and continue by going through it's neighbors, however implementing it hasn't been very successful.
I would highly appreciate any help/ advice.
Below is what I currently have:
public class Point {
private double x;
private double y;
private Block b;
public Point(double x, double y)
{
this.x=x;
this.y=y;
}
public Point(double x, double y, Block b) //consrtuctor for complex case
{
this.x=x;
this.y=y;
b.points.add(this);
}
public double getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double distance(Point p)
{
double res=0;
res = Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2));
return res;
}
}
import java.util.ArrayList;
public class Block {
private int x;
private int y;
public ArrayList<Point> points;
public Block(int x, int y) {
points = new ArrayList<Point>();
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;
}
}
import java.util.Random;
public class ComplexCase {
private Block[][] blockMat;
public ComplexCase(int k, int n)
{
Random generator = new Random();
Point p1;
Block b1;
double x,y;
int bx1,by1;
int t;
t = 1/k;
blockMat = new Block[k][k];
for (int i =0;i<n;i++)
{
x = generator.nextDouble();
y = generator.nextDouble();
bx1 = (int) (x/t);
by1 = (int) (y/t);
b1 = new Block(bx1,by1);
p1 = new Point(x,y,b1);
}
}
public Block[][] getBlockMat() {
return blockMat;
}
public void setBlockMat(Block[][] blockMat) {
this.blockMat = blockMat;
}
}

How to check if circles intersect each other?

This is my test class,
public class Shape2DTester {
public static void main(String[] args) {
GeometricObject2D geoObject1 = new ComparableCircle2D(0, 5, 2);
GeometricObject2D geoObject3 = new ComparableCircle2D(0, 0, 2);
System.out.println("geoObject1 overlaps geoObject3: "
+ geoObject1.intersect(geoObject3));
}
}
This is my circle class,
public class ComparableCircle2D extends GeometricObject2D<ComparableCircle2D> {
public double x, y;
public double radius;
ComparableCircle2D() {
super();
this.radius = 1.0;
}
ComparableCircle2D(double radius) {
super();
this.radius = Math.abs(radius);
}
ComparableCircle2D(double x, double y, double radius) {
super(x, y);
this.radius = Math.abs(radius);
}
public double getArea() {
return Math.PI * getRadius() * getRadius();
}
public double getPerimeter() {
return 2 * Math.PI * getRadius();
}
public void setRadius(double setRadius) {
this.radius = Math.abs(setRadius);
}
public double getRadius() {
return radius;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
#Override
public boolean intersect(GeometricObject2D g) {
ComparableCircle2D other = (ComparableCircle2D) g;
double dx = other.x - getX();
double dy = other.y - getY();
double radi = other.radius + getRadius();
return (dx * dx + dy * dy < radi * radi);
}
}
}
this is my superclass,
public abstract class GeometricObject2D<T extends GeometricObject2D> implements
Comparable<GeometricObject2D> {
public double x, y;
GeometricObject2D() {
this.x = 0;
this.y = 0;
}
GeometricObject2D(double x, double y) {
this.x = x;
this.y = y;
}
public abstract double getArea();
public abstract double getPerimeter();
public abstract boolean intersect(GeometricObject2D g);
#Override
public int compareTo(GeometricObject2D o) {
// TODO Auto-generated method stub
return 0;
}
}
I want to find out possibility of intersecting two circles but there is an error in my code that I didn't realize.
For example I create two circle object coordinates-1(0,0) , radius-1=2 and coordinates-2(0,5) ,radius-2=2. That above method must return false but returns true. I didn't find error.
System.out.println("geoObject1 intersects geoObject3: "
+ geoObject1.intersect(geoObject3));
prints geoObject1 intersects geoObject3: true
As #Pshemo said, your code (now that you've shown it) has an extra } at the end that shouldn't be there.
How, if we paste all that code into IDEONE, and run it, we confirm your error.
If we then DEBUG the code by adding a single print statement, we see:
dx=0.0, dy=0.0, radi=4.0
Hmmm, why is dy = 0 when it should be 5?
Answer: Because you added another set of x and y fields to your subclass, that is hiding the fields from the base class!!!!
Simple debugging would have shown you this yourself. This is what #PeterLawrey was talking about in his comment:
you mistake is it is likely to be; the values are not what you think they are. This is where debugging your code can show this.
Of course, if you had used a good IDE, you wouldn't even need to debug, because the IDE would have warned you about the field hiding.
Rather than Math.pow(x, 2) it is more efficient to do x * x, and instead of using Math.sqrt you can square the sum of the radii.
public boolean intersect(GeometricObject2D g) {
ComparableCircle2D other = (ComparableCircle2D) g;
double dx = other.x - x; // e.g. 0 - 0
double dy = other.y - y; // e.g. 5 - 0
double radii = other.radius + radius; // e.g. 2 + 2
return dx * dx + dy * dy < radii * radii ; // e.g. 0 + 25 < 16 is false.
}
You never assign the fields x and y. Therefore dx = dy = 0.
You have to either assign the field's values or use the fields in the superclass (but you shouldn't have fields with the same information in the same object, so remove the fields created in ComparableCircle2D).
Also if your circle is defined as contour, not as area, then the test for intercepting circles is incorrect. Consider the case where 2 circles with the same center have different radii: dx² + dy² = 0 < dr², but the contours don't intersect; only the areas inside the circles overlap.

Using methods I have created in a program - Java

Alright so I have created 2 methods called Point and LineSegment (they both work).
Point is this:
public class Point {
private double x;
private double y;
public Point(){
x=0;
y=0;
}
public Point(double a, double b){
x=a;
y=b;
}
public double getY(){
return y;
}
public double getX(){
return x;
}
public void setX(double newX){
x= newX;
}
public void sety(double newY){
y= newY;
}
public void setXY(double newX, double newY){
x = newX;
y = newY;
}
public String toString(){
return "("+x+" , "+y+")";
}
}
LineSegment is this :
public class LineSegment {
private Point A;
private Point B;
public LineSegment (){
A = new Point();
B = new Point();
}
public LineSegment (int x1, int y1, int x2, int y2){
A = new Point(x1, y1);
B = new Point(x2, y2);
}
public LineSegment(Point P, Point Q){
A = new Point(P.getX(), P.getY());
B = new Point(Q.getX(), Q.getY());
}
public double Length(){
double length = Math.sqrt(Math.pow( (B.getX() - A.getX()), 2) + Math.pow((B.getY() - A.getY()),2));
return length;
}
public double Slope(){
double slope = (B.getY() - A.getY() )/ (B.getX() - A.getX());
return slope;
}
public String toString(){
return "("+A.getX()+" , "+A.getY()+") + ("+B.getX()+" , "+B.getY()+") ";
}
}
As I said both of these codes work but now my task is to use Linesegment to create 2 line segments called AB and CD, and to output the slope of both of them, I am not sure how to do this, can anybody help?
They are called classes, not methods.
And you've already made class instances in your LineSegment class at
A = new Point();
B = new Point();
So now, in some other class,
public static void main(String[] args) {
Point A = new Point();
Point B = new Point();
Point C = new Point();
Point D = new Point();
LineSegment AB = new LineSegment(A, B);
LineSegment CD = new LineSegment(C, D);
// output the slope
System.out.println(AB.Slope());
}
Also, note that your Slope method will return a divide-by-zero error if you run this code as-is.
Create a class, regardless of its name, and put the main method.
The method called main is essentially your starting point, where your code will start running.
public static void main(String[]args){
LineSegment segment1 = new LineSegment(1,2,3,4);
LineSegment segment2 = new LineSegment(5,6,7,8);
System.out.println("The first slope is: " + segment1.Slope());
System.out.println("The second slope is: " + segment2.Slope());
}

Using double method into a segment class

How can I write a double slope method for a segment class?
I have two variable: p1 = x1, y1 and p2 = x2, y2.
I did this code but this is wrong:
public double slope() {
return (double)(p2.y - p1.y)/(p1.x-p2.x);
}
Can someone tell me why is it wrong?
What is the right way to write it?
Thank you!
Depending on the type of p1, it could be a Point which takes both an x and a y coordinate.
public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
You'd have to use getX() and getY() to get the X and Y coordinates. You'd also have to be sure you created the point with new Point(1, 2) as well.
Also, be sure that you're getting the right cast behavior by adding parens around it and your numerator:
return ((double)(p2.getY() - p1.getY()))/(p1.getX() - p2.getX());
(Although that above seems to scream for a deltaY and deltaX method alone)

Categories