Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Overloading Overloading methods

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given a method that takes a long value and returns the max value of the long type minus the given number.

public static long getMaxMinusCurrent(long val) {
   return Long.MAX_VALUE - val;
}
Overload this method with one new method. The method should take an int and return the max value of the int type minus the given value.

For the method, the type of result value must be the same as the type of the parameter.

hi, can you help me to resolve this : https://codeshare.io/oQdK8Y
i don't understand !!! it's difficult for me
Explain me PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE
PLEASE HELP
 
Marshal
Posts: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain which part you don't understand; it looks simple to me.
 
olivier boudigue
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't see that what to put in // write a method here
I'm a beginner !
Can tell me at least the theory or some internet link that can explain me and put me on the path?
Or give me the answer with an explanation?
Yesterday I solved the println on my own ... and everyone told me that it was simple but nobody knew how to direct me!
 
Campbell Ritchie
Marshal
Posts: 74774
336
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overloading means having two methods (or constructors) with the same name but different types of parameters. Methods may of course have different return types.
That is different from overriding, where two instance methods, one in the superclass and the other in the subclass, have the same types of parameters.
 
Campbell Ritchie
Marshal
Posts: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

olivier boudigue, quoting CodeShare wrote:. . . the type of result value must be the same as the type of the parameter. . . .

That should read, “The return type must be the same as the parameter type.”
I think the code example you were given uses Scanners incorrectly. But that shouldn't matter; you can start by hard‑coding the inputs
 
olivier boudigue
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like this doesn't work

public class Main {

   public static long getMaxMinusCurrent(long val) {
       return Long.MAX_VALUE - val;
   }
   
   System.out.println(getMaxMinusCurrent(123_456_789L));
   
   // write a method here
   // public static ...

   // Do not change code below
 
olivier boudigue
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you help me here the code : https://codeshare.io/oQdK8Y

with the question, please help me for me it's difficult
 
lowercase baba
Posts: 13024
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

olivier boudigue wrote:Like this doesn't work


You need to be more specific.  HOW does it not work?  Does it compile? Does it run? Does it crash?  Does it return a value other than what you expected?  If so, what did it return, and what did you expect?

When you are asking for help, you should try and make it as easy as possible for people to help you.  I would strongly advise you take a few minutes and look at our How to ask questions FAQ., then come back with more details.
 
olivier boudigue
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just have to write lines of code where it says but I don't understand, this is difficult for me.

the rule: Given a method that takes a long value and returns the max value of the long type minus the given number.

here the rule : https://codeshare.io/oQdK8Y
 
fred rosenberger
lowercase baba
Posts: 13024
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know java syntax at all?
Do you know how to write any kind of method?
Do you know how to pass an argument into a function?
Do you know how to return a value from a function?

Without knowing the answers to questions like these, it's really pointless for anyone to try and help you.  Nobody wants to spend 30 minutes crafting the perfect answer, only to have you say "no, that's not what I need to know".  

Start small.  where it says "Write a method here"...write a method.  the method you write does not have to solve the final problem (yet).  just write a method that prints something like "my method".  compile it. Test it.  see if that works.

Then try to update it to pass in a value, and print that value.  compile it. Test it.  

keep adding a teeny tiny piece, compile, and test.  Build it up to get what you need.

Nobody here will write the code for you.  Folk here will generally bend over backwards, if you show you are putting in more effort than simply saying "I need help".
 
Campbell Ritchie
Marshal
Posts: 74774
336
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

olivier boudigue wrote:. . . Given a method that takes a long value and returns the max value of the long type minus the given number . . .

That already exists in the code given. All you have to do is write another method taking an int parameter, with int return type, and doing what the rest of the question says.

Don't expect us to write the method for you. I could do it in a few seconds, but you will learn very little like that. You could probably write that method yourself in a few minutes; try it and tell us what happens.
 
Greenhorn
Posts: 29
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to know the basic structure of a method definition in order to answer your question. Basically, method consists of modifiers, return type, method name, parameters and then the method body. here's a pseudo-code to better understand the structure of a method definition:

Now, let's create a java method based on this pseudo-code:


public and static keywords are modifiers.
long is a data type that will be returned after its body is executed.
getMaxMinusCurrent is a method name.
long val is a parameter. it consists of a data type and a parameter name.
{} is a method body. Codes inside of these braces are executed once the method is called(or invoked).
return is a keyword that makes the execution exit the method body. If your method has a return type that is not void, you can add a value after the keyword and that value will be returned to the caller once the execution exits the method via return keyword. The data type of the value should be the same as the return type.

Method overloading, to put it simply, is defining methods that have similar construct.
For example:
int findMax(int[]  n1){/*method-body*/}
int findMax(int n1, int n2){/*method-body*/}
In the example above, findMax has two forms: a method with an array parameter and a method with two int parameters.

If you understand my explanation above then, I think you could answer your own question.

I think this blogspot has an alright explanation regarding java methods: Java Tutorial: Methods
 
olivier boudigue
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Elya,

I'm looking for the answer ...

i write this but it's wrong ... Can you help me again maybe give me an exemple more concret ?

   public static void print(int val) {
       System.out.println(getMaxMinusCurrent("int arg: " + val));
   }
 
Campbell Ritchie
Marshal
Posts: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't an overloaded method because it has a different name from the original.

You are trying too hard; the question is much simpler than you think.
 
Elya Matsunomi
Greenhorn
Posts: 29
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I'll give a simple example of overloaded method:

1st form:


2nd form:


As you can see, the two methods have the same modifiers, return type, method name. However, their parameters are different. In the 1st form, the given value(the value passed to the method) is a value with a data type of double. In the 2nd form, the given value is a value with a data type of float.
 
Campbell Ritchie
Marshal
Posts: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Elya Matsunomi wrote:. . . a simple example of overloaded method . . .

Good idea

. . . the two methods have the same modifiers, return type, method name. . . .

The JLS (=Java® Language Specification) only requires the two methods have the same name:-

That JLS section wrote:If two methods of a class . . . have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

It also says the name is overloaded rather than the method.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic