Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Need help with Overloading Methods

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fixed
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Walt Lee:
Fixed



Thanks!
 
Walt Lee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic