• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Overloading Methods

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,
I have a question about overloading methods.
I have the following example:
Original Method:


My Overloaded Methods:



Is that done wrong? Because the Junit tests all fail on these.

Help is appreciated, thanks!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The overloading looks fine to me. The actual implementation doesn't look perfect - for example, what is going to happen if v is longer than w?

But perhaps you ought to tell us what the tests are and what they are failing on.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are all the errors:

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those all look like the code never compiled in the first place, so JUnit can't even find the methods to test. Make sure you compile the code, and if there are any errors fix those before you try and run the tests.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, it's strange that these are runtime errors, not compile-time errors. Do you have some precompiled JUnit tests; are you not compiling the unit tests together with your source code?

You have three methods that take two double[], two int[] and two long[]. The error messages indicate that the tests expect that there are methods with different combinations of parameters:


java.lang.NoSuchMethodError: de.ostfalia.gdp.ss15.Vektor2.add([D[I)[D
This is looking for an 'add' method that takes a double[] and an int[] and returns a double[]

java.lang.NoSuchMethodError: de.ostfalia.gdp.ss15.Vektor2.add([D[J)[D
This is looking for an 'add' method that takes a double[] and a long[] and returns a double[]

Likewise for all the other errors; there are also messages about the methods: scale, isZero, euclideanNorm

Section 4.3.2 of the Java Virtual Machine specification explains how to read the method signatures, for example:

[D = array of double
[I = array of int
[J = array of long
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The last post is really helpful, thank you!
I did not know what these signs meant in the Junit tests.
By the way maybe this helps:
The Junit test I showed is the one that is used in the SVN Repository to check our assignment.
The Junit test that we got to use to checkk for correctness for our assignment did not show any errors.

So I will maybe create some more overloaded Methods to cover the test cases?
 
Marshal
Posts: 78675
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult for this forum: moving.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why too difficult?
 
Campbell Ritchie
Marshal
Posts: 78675
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should find out what cases you are required to provide methods for. Then you will know how many overloadings to provide.
 
Campbell Ritchie
Marshal
Posts: 78675
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Von Valkenburgh wrote:Why too difficult?

Because that was the “beginning” forum and that is reserved for “easy” questions.
 
Ranch Hand
Posts: 63
Spring Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All are RuntimeExceptions. You need to compile the cod eproperly, the overloading logic looks good, no issues.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message is indeed very helpful.



It means at run time there was a difficulty in identifying a method "add" with two double parameters (varying parameter type for each error message) that is in the class "Vektor2" belonging to the package "de.ostfalia.dgp.ss15".

Did you compile your .java file?
 
Sheriff
Posts: 17627
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could it be that the unit test is expecting the methods to be non-static? All the methods you defined are static.
 
Campbell Ritchie
Marshal
Posts: 78675
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raghavan Muthu wrote: . . . difficulty in identifying a method "add" with two double parameters . . .

double array, surely, if there is a [ in the error message.

If there are two dozen error messages (I haven't checked whether the types shown are all different) that would suggest there are two dozen methods missing, which I think is too many. That is why I suggested OP ask how many methods there are supposed to be.
 
Campbell Ritchie
Marshal
Posts: 78675
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Watch this space

I though I might as well learn some Streams, so let't try something.38 lines and doesn't it do a lot I changed it slightly from my version because I have a keyboard input class and you haven't, but it should work unchanged with a Scanner.

The constructor creates a List and populates it; I copied and pasted your error messages as a single block to the terminal and then terminated input with ctrl‑D. If you use Windows® and ctrl‑D doesn't seem to work, try ctrl‑Z instead.
It adds all the lines to that first List.
  • Line 28: Then I got a Stream and filtered it to keep only those lines beginning with the name of the Exception. You notice that the Stream being created from a List<String> already is a Stream<String> so the function to startsWith works as long as the String has a startsWith method.
  • Line 29: Then I use the distinct method which uses the equals method to determine whether you have the same object twice.
  • Line 30: Then I mapped each element to a String with the toString method. That line is redundant and can be removed without altering the functionality.
  • Line 31: Then I mapped the Strings again using a function to get the substring after the last dot.
  • Line 32: Then I used the collect method and the example given under the reduction link in the API page, to collect the remaining contents of the Stream into a new List.
  • Line 35: Create a new Stream from the new List and use its forEach method to apply the ::println method in the reference System.out:: to the element of the Stream.
  • To use my class, copy and paste the entire error message block then feed the end of transmission character to terminate output. When I tried it, this is what I got:-Line 12 tells you it wants to find an .add method but it should be
    long[] add(long[] arr1, long[] arr2) ...
    You showed us a
    double[] add(long[] arr1, long[] arr2) ...
    method. It is slightly odd to add two long arrays and make a double array, and the tests expect a long[]. That is what the three Js mean in ([J[J)[J
    If you add two long[] arrays you would expect to get a long[] array as the result.

    Check that you are supposed to add two long[] arrays to make a third long[] array. Also check that the name of the class (…Vektor2) is exactly correct and matches the error messages. If that is indeed the case, then you will need to change the return type of that add method. You cannot have two methods in the same class with the same signature and different return types.
     
    I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic