This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with Overloading Methods

 
Walt Lee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to figure out how to create 3 columns for subtraction, using an overloading method. First column first number and largest, second number smaller number, and 3rd column what the first number subtracting the second number equals. I wasn't doing bad with begginer stuff in java till I started this and just can't figure out what I need to do. The actual instructions are:

Write overloaded Java methods that return the difference of two integers. Your methods should contain the logic to return the difference of the (larger - smaller).

Parm 1 TypeParm 2 TypeReturn Type

int int int
double double double
int double double
double int double


Anyone able to help out. I know it is probably fairly easy, but I am just not figuring it out using the book I have.
[ October 21, 2007: Message edited by: Walt Lee ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "Cybrephoenix",
welcome to JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name.
Thanks, and have fun!
 
Walt Lee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone able to help out. I know it is probably fairly easy, but I am just not figuring it out using the book I have.


It is "fairly easy"... Unfortunately, it is too easy. This look too much like a homework question.

Anyway, learning Java is best done by trying. So... give it a shot. And if you can't get it to work, show us what you have done, so that we can give you a few hints in the right direction.

Henry
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Walt Lee:
Fixed


Thanks!
 
Walt Lee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have. I am sure it is probably something simple, but can't seeme to find it.




import javax.swing.JOptionPane;

public class Ovrldmeth {

public static void main(String[] args) {


System.out.printf("Num 1\tNum 2\tResult", sub(7,3), sub(13.5,12.6), sub(9.9, 3), sub(6,2.9));

}

public static int sub(int num1, int num2) {
return (num1 > num2) ? num1 - num2 : num2 - num1;
}

public static int sub(double num1, double num2) {
return ((num1 > num2) ? num1 - num2 : num2 - num1);
}

public static int sub(double num1, int num2) {
return ((num1 > num2) ? num1 - num2 : num2 - num1);
}

public static double sub(int num1, double num2) {
return (num1 > num2) ? num1 - num2 : num2 - num1;


}
}
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, take a look at the signature, does it match the signature as required in your assignment -- particularly the return type.


But... the problem with your program is likely the printf() method. Take a look at the javadoc for that method again. The first parameter is the format string, which is just printing the heading -- it makes no mention of the parameters that follow it.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic