• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to implement the following methods of class Segments

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


Sample code:

Sample code:

Sample code:


This is class Segment:


This is class Point:


And this is the Main class:


My result are:
5.0
1.0
1.0
1.0
1.0
true

But i need to be 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:


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
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can cast doubles to ints, but I think you shouldn't because you will lose the fractional parts of any coordinates.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A better name would be Linesegment. And do the casting to int only at the very end of your calculations, then it will limit the error almost as much as possible.
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I add in Main class (int) before I print it and now I received the right result



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

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."



I guess that error it's coming from the conditions of the Constructor.
Do you have some advice how can I fix them?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your Segment-constructor, you test for:

For that to work correctly, you must override the 'equals'-method in your Point-class. When are two Points equal?
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried, it's useless.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allright, what was the test that failed?
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SegmentTest.testConstructor:21 � NullPointer Cannot invoke "Object.equals(Obje...
[ERROR]   SegmentTest.intersectionTestCases:116 � NullPointer Cannot invoke "Object.equa...
[ERROR]   SegmentTest.lengthTestCases:90 � NullPointer Cannot invoke "Object.equals(Obje...
[ERROR]   SegmentTest.middleTestCases:104 � NullPointer Cannot invoke "Object.equals(Obj...

And I cannot Override it, It doesn't not allow me
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us the test that gave this result.
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Point class hasn't implemented an equals() method.
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I implemented like this:


Now I have this result when I'm running the code: "Cannot invoke "Object.equals(Object)" because "this.start" is null"

And fails some tests:
[ERROR]   SegmentTest.testConstructor:21 � NullPointer Cannot invoke "Object.equals(Obje...
[ERROR]   SegmentTest.intersectionTestCases:116 � NullPointer Cannot invoke "Object.equa...
[ERROR]   SegmentTest.lengthTestCases:90 � NullPointer Cannot invoke "Object.equals(Obje...
[ERROR]   SegmentTest.middleTestCases:104 � NullPointer Cannot invoke "Object.equals(Obj...
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's the constructor for Line that needs to compare the two end points, not the Point class.
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody suggested me to do like this, to put in Point class.
My solution I put only in Construct class but It fails a test
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add this to your Point-class:
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my update solution:



Segment class:


And Main class



When I'm running the code I have the right result
5
1
1
1
1
true

But when I tested I received this error
"SegmentTest.testConstructorEqualStartEndCase:35 Expected java.lang.RuntimeException to be thrown, but nothing was thrown."

I think it's because of the conditions from Constructor
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with one of your tests was that you expected an Exception for the testcase:

But because the equals-method was not overridden:

p is NOT equal to q!
and therefore no exception is thrown.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet has shown you how to add equals() to Point class.
 
Cris Marinescu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah! Thank you!
I've just seen it!
 
reply
    Bookmark Topic Watch Topic
  • New Topic