Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to implement the following methods of class Segment:

Constructor with start and end points as parameters
Ensure that a created segment exists and is not degenerative which means that the start and the end of the segment is not the same point.
If it is, use throw new IllegalArgumentException() to raise an error.

double length()
Return length of the segment.

Point middle()
Return a middle point of the segment.

Point intersection(Segment another)
Return a point of the intersection of the current segment and the given one.
Return null if there is no such point.
Return null if segments are collinear.
Please, note that intersection point must lay on both segments.

Class Point is already there.

Examples
You may use Main class to try your code. There are some examples below.

Sample code:

Java
...
double length = new Segment(new Point(0, 0), new Point(3, 4)).length();
System.out.println(length);
Output:
 
5

Sample code:
Java
...
Segment first = new Segment(new Point(0, 0), new Point(4, 4));
Segment second = new Segment(new Point(2, 0), new Point(0, 2));
Point intersection = first.intersection(second);
 
System.out.println(intersection.getX());
System.out.println(intersection.getY());

Output:
 
1
1


Sample code:
Java
...
Segment segment = new Segment(new Point(2, 0), new Point(0, 2));
Point midpoint = segment.middle();
 
System.out.println(midpoint.getX());
System.out.println(midpoint.getY());

Output:
 
1
1


Sample code:
Java
...
Segment first = new Segment(new Point(0, 0), new Point(4, 0));
Segment second = new Segment(new Point(2, 1), new Point(1, 2));
Point intersection = first.intersection(second);
 
System.out.println(intersection == null);
Output:
 
true


This is class Segment:
Java
<pre>class Segment {
    Point start;
    Point end;
 
    public Segment(Point start, Point end) {
        if (start == null || end == null) {
            throw new IllegalArgumentException("Arguments can't be null");
        }
        if (start.equals(end)) {
            throw new IllegalArgumentException("The points must differ.");
        }
        this.start = start;
        this.end = end;
     
    }
 
    double length() {
 
        return sqrt(pow(end.getX() - start.getX(), 2) + pow(end.getY() - start.getY(), 2));
    }
 
    Point middle() {
       
        return new Point((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
 
    }
 
      Point intersection(Segment another) {
 
          double k1 = (end.getY() - start.getY()) / (end.getX() - start.getX());
          double k2 = (another.end.getY() - another.start.getY()) / (another.end.getX() - another.start.getX());
          if (k1 == k2) return null;
 
          double b1 = (start.getY() * end.getX() - end.getY() * start.getX()) / (end.getX() - start.getX());
          double b2 = (another.start.getY() * another.end.getX() - another.end.getY() * another.start.getX()) /
                  (another.end.getX() - another.start.getX());
 
          double x = (b2 - b1) / (k1 - k2);
          double y = (k1 * b2 - k2 * b1) / (k1 - k2);
 
          if ((x > start.getX() && x > end.getX()) || (x > another.start.getX() && x > another.end.getX()) ||
                  (x < start.getX() && x < end.getX()) || (x < another.start.getX() && x < another.end.getX()) ||
                  (y > start.getY() && y > end.getY()) || (y > another.start.getY() && y > another.end.getY()) ||
                  (y < start.getY() && y < end.getY()) || (y < another.start.getY() && y < another.end.getY()))
              return null;
 
          return new Point(x, y);
    }
}


This is class Point:
Java
class Point {
    private double x;
    private double y;
 
    public Point(final double x, final double y) {
        this.x = x;
        this.y = y;
    }
 
    public double getX() {
 
        return x;
    }
 
    public double getY() {
 
        return y;
    }
}


And this is the main class:
Java
public class Main {
    public static void main(String[] args) {
 
        {
            double length = new Segment(new Point(0, 0), new Point(3, 4)).length();
            System.out.println(length);
        }
 
        {
            Segment segment = new Segment(new Point(2, 0), new Point(0, 2));
 
            Point midpoint = segment.middle();
 
            System.out.println(midpoint.getX());
            System.out.println(midpoint.getY());
        }
 
        {
            Segment first = new Segment(new Point(0, 0), new Point(4, 4));
            Segment second = new Segment(new Point(2, 0), new Point(0, 2));
            Point intersection = first.intersection(second);
 
            System.out.println(intersection.getX());
            System.out.println(intersection.getY());
        }
 
        {
            Segment first = new Segment(new Point(0, 0), new Point(4, 0));
            Segment second = new Segment(new Point(2, 1), new Point(1, 2));
            Point intersection = first.intersection(second);
 
            System.out.println(intersection == null);
        }
    }
}

My result are:
5.0
1.0
1.0
1.0
1.0
true


What I have tried:

But i need to return them integers as in examples.  I need to change only the code from the class Segment.
I tried like this, adding (int) at every return, in order to return me integers, but it useless. I still have the same result.
Class Segment after changes:


Java
<pre>class Segment {
    Point start;
    Point end;
 
    public Segment(Point start, Point end) {
        if (start == null || end == null) {
            throw new IllegalArgumentException("Arguments can't be null");
        }
        if (start.equals(end)) {
            throw new IllegalArgumentException("The points must differ.");
        }
        this.start = start;
        this.end = end;
       
    }
 
    double length() {
 
        return sqrt((int) pow(end.getX() - start.getX(), 2) + (int) pow(end.getY() - start.getY(), 2));
    }
 
    Point middle() {
        
        return new Point((int) (start.getX() + end.getX()) / 2, (int) (start.getY() + end.getY()) / 2);
 
    }
 
      Point intersection(Segment another) {
 
          double k1 = (end.getY() - start.getY()) / (end.getX() - start.getX());
          double k2 = (another.end.getY() - another.start.getY()) / (another.end.getX() - another.start.getX());
          if (k1 == k2) return null;
 
          double b1 = (start.getY() * end.getX() - end.getY() * start.getX()) / (end.getX() - start.getX());
          double b2 = (another.start.getY() * another.end.getX() - another.end.getY() * another.start.getX()) /
                  (another.end.getX() - another.start.getX());
 
          double x = (b2 - b1) / (k1 - k2);
          double y = (k1 * b2 - k2 * b1) / (k1 - k2);
 
          if ((x > start.getX() && x > end.getX()) || (x > another.start.getX() && x > another.end.getX()) ||
                  (x < start.getX() && x < end.getX()) || (x < another.start.getX() && x < another.end.getX()) ||
                  (y > start.getY() && y > end.getY()) || (y > another.start.getY() && y > another.end.getY()) ||
                  (y < start.getY() && y < end.getY()) || (y < another.start.getY() && y < another.end.getY()))
              return null;
 
          return new Point((int) x, (int) y);
     }
}


Output
5.0
1.0
1.0
1.0
1.0
true
Does anyone know hot to convert the double in int in order to have this results:
5
1
1
1
1
true
Posted
Updated 1-Dec-22 1:28am
v2

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
You can control the format of the output by the use of format strings in your print statements: Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)[^]. Alternatively you could just cast the return values from getX and getY to integer types.
 
Share this answer
 
Comments
CPallini 1-Dec-22 5:01am    
5.
Richard already gave you the right suggestion.

However, 'producing the same (format of the) output of the examples' does make any sense?
You have a class, correctly holding double variables. Its methods (correctly) return double values.
From my humble point of view, it doesn't make any sense to alter the (correct) output format of your code.
 
Share this answer
 
Comments
Richard MacCutchan 1-Dec-22 5:11am    
You are right, of course.
I add in Main class (int) before I print in and now I received the right result

Java
public class Main {
    public static void main(String[] args) {
 
        {
            double length = new Segment(new Point(0, 0), new Point(3, 4)).length();
            System.out.println((int) length);
        }
 
        {
            Segment segment = new Segment(new Point(2, 0), new Point(0, 2));
 
            Point midpoint = segment.middle();
 
            System.out.println((int) midpoint.getX());
            System.out.println((int) midpoint.getY());
        }
 
        {
            Segment first = new Segment(new Point(0, 0), new Point(4, 4));
            Segment second = new Segment(new Point(2, 0), new Point(0, 2));
            Point intersection = first.intersection(second);
 
            System.out.println((int) intersection.getX());
            System.out.println((int) intersection.getY());
        }
 
        {
            Segment first = new Segment(new Point(0, 0), new Point(4, 0));
            Segment second = new Segment(new Point(2, 1), new Point(1, 2));
            Point intersection = first.intersection(second);
 
            System.out.println(intersection == null);
        }
    }
}

Now I have the output
5
1
1
1
1
true
which is the correct result.

But when I'm testing the solution, I failed one test.
I have this error: "SegmentTest.testConstructorEqualStartEndCase:35 Expected java.lang.RuntimeException to be thrown, but nothing was thrown."


Java
<pre>class Segment {
    Point start;
    Point end;
 
    public Segment(Point start, Point end) {
        if (start == null || end == null) {
            throw new IllegalArgumentException("Arguments can't be null");
        }
        if (start.equals(end)) {
            throw new IllegalArgumentException("The points must differ.");
        }
        this.start = start;
        this.end = end;
         
    }
 
    double length() {
 
       return sqrt(pow(end.getX() - start.getX(), 2) + pow(end.getY() - start.getY(), 2));
 
    }
 
    Point middle() {
         
        return new Point ((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
 
    }
 
      Point intersection(Segment another) {
 
          double k1 = (end.getY() - start.getY()) / (end.getX() - start.getX());
          double k2 = (another.end.getY() - another.start.getY()) / (another.end.getX() - another.start.getX());
          if (k1 == k2) return null;
 
          double b1 = (start.getY() * end.getX() - end.getY() * start.getX()) / (end.getX() - start.getX());
          double b2 = (another.start.getY() * another.end.getX() - another.end.getY() * another.start.getX()) /
                  (another.end.getX() - another.start.getX());
 
          double x = (b2 - b1) / (k1 - k2);
          double y = (k1 * b2 - k2 * b1) / (k1 - k2);
 
          if ((x > start.getX() && x > end.getX()) || (x > another.start.getX() && x > another.end.getX()) ||
                  (x < start.getX() && x < end.getX()) || (x < another.start.getX() && x < another.end.getX()) ||
                  (y > start.getY() && y > end.getY()) || (y > another.start.getY() && y > another.end.getY()) ||
                  (y < start.getY() && y < end.getY()) || (y < another.start.getY() && y < another.end.getY()))
              return null;
 
          return new Point(x, y);
    }
}


I guess that error it's coming from the conditions of the Constructor.
Do you have some advice how can I fix them?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900