0

I've been working on my project for about 15 hours straight and I ran out of ideas and am in desperate need of some help. I have looked at tutorials and books, but i cannot figure it out.

My main problem is that i cannot understand/calculate how i can use an overload method to choose between an int or a double. To explain I'm trying to get my program to use user input to convert the value to either int to a double (or vice-versa) and store the result to show the correct answers.

Instead of just leaving it as double I'm trying to get the variation for each possibility.

The reason I'm trying to do so is as follows (i know the code doesn't need it but the assignment requires i do so in this way but I keep hitting dead ends) :


Here’s where the overloading comes in: some applications will define the amount as an int, and others will define it as a double. Therefore, you need to write two overloaded versions of this method:

Get user input only once. If you convert the input value for the amount loaned to an int and store it in an int variable, you can then convert the value of this int to a double and store the result of this conversion in a double variable. The use of cast operators to either convert int values to double or double values to int.

Cœur
  • 37,241
  • 25
  • 195
  • 267
CriCri
  • 191
  • 1
  • 10
  • The `amountLoaned` variable is declared as a double. Input from the user is parsed as a double. The `amountLoaned` value is passed to the `interestcalc` method which expects a double. Where does (and why would) an int enter into this scenario? – epalm Feb 17 '14 at 04:03
  • Method overload means having different input parameters on the smae method name. I don't see any requirement for that in your code. – jeremyjjbrown Feb 17 '14 at 04:05
  • My assignment asks for me to "some applications will define the amount as an int, and other will define it as a double. therefore, you need to write two overloaded versions of this method. 1/ one that taks the annual interest rate as a double, the number of years as an int and the amount loaned as an int. – CriCri Feb 17 '14 at 04:06
  • 2. another one that takes the annual interest rate as a double, the number of years as a int and the amount loaned as a double. – CriCri Feb 17 '14 at 04:06
  • Sorry forgot to include that let me edit the question for clarification – CriCri Feb 17 '14 at 04:07

2 Answers2

0

Why complicating the situation? A simple solution: - Receive the input as double - Check whether the number is integer. If yes then convert it to integer then call the appropriate method. A quick search give this:

How to test if a double is an integer

Convert double to Int, rounded down

Community
  • 1
  • 1
0

If you need to write two overloaded versions of the interestcalc function, just make two separate methods with the following signatures:

public static void interestcalc (int aLoan, double interes, int numYears)
{
    // perform calculations here
}


public static void interestcalc (double aLoan, double interes, int numYears)
{
    // perform calculations here
}
JDJ
  • 4,298
  • 3
  • 25
  • 44