File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need help with Overloading Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with Overloading Methods" Watch "Need help with Overloading Methods" New topic
Author

Need help with Overloading Methods

Walt Lee
Greenhorn

Joined: Oct 20, 2007
Posts: 4
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

Joined: Jul 11, 2001
Posts: 14112
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!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Walt Lee
Greenhorn

Joined: Oct 20, 2007
Posts: 4
Fixed
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Walt Lee:
Fixed


Thanks!
Walt Lee
Greenhorn

Joined: Oct 20, 2007
Posts: 4
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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Overloading Methods