Results 1 to 2 of 2

Thread: Help with Arrays

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Answers
    1

    Help with Arrays

    Hello all,

    I'm having a little trouble with this program, can't tell where I have went wrong any help would be appreciated.

    I keep getting the error:

    tenPercentOff(int) in DebugThree4 cannot be applied to (double)

    here is the code:

    public class DebugThree4
    // This class discounts prices by 10%
    {
    public static void main(String args[])
    {
    int price = 100;
    double price2 = 100.00;
    tenPercentOff(price);
    tenPercentOff(price2);
    }
    public static void tenPercentOff(int p)
    {
    double newPrice = p * .10;
    System.out.println("Ten percent off");
    System.out.println("New price is " + newPrice);
    }
    }


  2. #2
    Contributing Member
    Join Date
    Sep 2006
    Answers
    42

    Re: Help with Arrays

    public static void main(String args[])
    {
    int price = 100;
    double price2 = 100.00;
    tenPercentOff(price);
    A. tenPercentOff(price2);
    }
    B. public static void tenPercentOff(int p)
    {
    In the above program, you are passing an double to function that accepts an integer value. Since double is more precise than an integer (i.e its value is greater that int) hence Java does not do an implicit cast. If you cast the double to the int, you will loose info.
    Eg: 100.90 (a double value) when cast to int will become 100
    Thus when there is such loss, you either have to explicitly cast or change the method parameter to accept a double as follows

    Change line A to
    tenPercentOff((int)price2);

    OR

    Change line B to
    public static void tenPercentOff(double p).

    Hope this helps.


    Regards,
    Sahil.

    --Smile, it makes people wonder what you are thinking!!!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact