Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help with making code more efficent! - PLEASE HELP:(

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Need help with making code more efficent! - PLEASE HELP:(

    So basically, I am developing a program to calculate Annual and Monthly premiums for Insurance.

    I am a beginners level Java Programmer. Studying Computer Science. and here is my code.
     public double calculatePremium(double premium, int time, int mostExpensive, int noGadgets) {
     
             if (time == 2) { // when user hits 2 this calcualtes monthly premium
                if (noGadgets == 1 && noGadgets  <= 500) {
                    premium = 4.99;
                }
                if (noGadgets  == 1 && noGadgets  > 500 && mostExpensive <= 750) {
                    premium = 6.15;
                }
     
                if (noGadgets  == 1 && noGadgets  > 750 && mostExpensive <= 1000) {
                    premium = 7.50;
                }
     
     
                if (noGadgets  >= 2 && noGadgets  <= 3 && mostExpensive <= 500) {
                    premium = 17.30;
                }
                if (noGadgets  >= 2 && noGadgets  <= 3 && mostExpensive > 500 &&mostExpensive <= 750) {
                    premium = 6.15;
                }
     
                if (noGadgets  >= 2 && noGadgets  <= 3 && mostExpensive > 750 && mostExpensive <= 1000) {
                    premium = 7.50;
                }
     
     
                if (noGadgets  >= 4 && noGadgets <= 5 && mostExpensive <= 500) {
                    premium = 14.99;
                }
                if ((noGadgets  >= 4 && noGadgets  <= 5) && mostExpensive > 500 && mostExpensive<= 750) {
                    premium = 18.60;
                }
     
                if (noGadgets  >= 4 && noGadgets  <= 5 && mostExpensive> 750 && mostExpensive <= 1000) {
                    premium = 21.82;
                }
     
     
                return premium;
     
     
     
            }
     
     
     
            if (time == 1) { // if user hits 1 on kbd then calculate annual premium
                if (noGadgets  == 1 && noGadgets  <= 500) {
                    premium = 4.99 * 12.00;
                }
                if (noGadgets  == 1 && noGadgets  > 500 && mostExpensive <= 750) {
                    premium = 6.15 * 12.00;
                }
     
                if (noGadgets  == 1 && noGadgets  > 750 && mostExpensive <= 1000) {
                    premium = 7.50 * 12.00;
                }
     
     
                if (noGadgets >= 2 && noGadgets  <= 3 && mostExpensive <= 500) {
                    premium = 17.30 * 12.00;
                }
                if (noGadgets  >= 2 && noGadgets  <= 3 && mostExpensive > 500 && mostExpensive <= 750) {
                    premium = 6.15 * 12.00;
                }
     
                if (noGadgets  >= 2 && noGadgets  <= 3 && mostExpensive > 750 && mostExpensive <= 1000) {
                    premium = 7.50 * 12.00;
                }
     
     
                if (noGadgets  >= 4 && noGadgets  <= 5 && mostExpensive <= 500) {
                    premium = 14.99 * 12.00;
                }
                if ((noGadgets  >= 4 && noGadgets  <= 5) && mostExpensive > 500 && mostExpensive <= 750) {
                    premium = 18.60 * 12.00;
                }
     
                if (noGadgets  >= 4 && noGadgets  <= 5 && mostExpensive > 750 && mostExpensive <= 1000) {
                    premium = 21.82 * 12.00;
                }
     
     
                return premium;
     
     
     
            }
            return premium;
        }

    My lecturers advise me not to use too many IF statements, but I'm really unsure on how to get around this?
    The code above is using LOADS of IF statements, and I need to make it more efficient. Could I use a loop? and if so, then how?
    Last edited by ProgrammablePeter; December 1st, 2013 at 04:38 PM. Reason: Code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,948
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Need help with making code more efficent! - PLEASE HELP:(

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Many of the if statements make no sense. For example:
    if (noGadgets == 1 && noGadgets > 500
    If noGadgets is 1 it won't be greater than 500
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Need help with making code more efficent! - PLEASE HELP:(

    Haha, thank-you. Sorry about that :/

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    336
    My Mood
    Angry
    Thanks
    39
    Thanked 56 Times in 56 Posts

    Default Re: Need help with making code more efficent! - PLEASE HELP:(

    if (noGadgets == 1 && noGadgets <= 500) {
    premium = 4.99;
    }


    If it is equal to 1, it will always be less than or equal to 500.

    if (noGadgets == 1 && noGadgets > 500 && mostExpensive <= 750) {
    premium = 6.15;
    }

    This can never be true as it cannot be both equal to one and greater than 500 at the same time.

    Why are you passing it the variable "premium" if all you do is change it in the method? Why not just have a variable inside the method called "premium" instead?


    You can use if, else if, else to avoid using too many if statements.

    You can have an if structure like this


    if (statement)


    also, this is allowed.

    if (condition)
    statement;

    else
    statement;


    Also, you can do this

    if (condition)
    statement;

    else if (condition)
    statement;
    ..... (can have more else ifs if needed)

    else
    statement;

    Note: Unless you have brackets after a condition with an if, else if, or else statement, it will only apply to the statement right after the condition.

    Google if, else if, else for more info.
    JavaScript is NOT Java.

  5. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    ProgrammablePeter (December 1st, 2013)

  6. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Need help with making code more efficent! - PLEASE HELP:(

    Okay, thank-you for your advise. Greatly appreciated! and I have no idea why i'm passing in the variable premium when i'm changing it in the method. I study Computer Science 1st year, and we have only just learnt methods last week. So i'm still rather new to them. I guess if my code works, then I think my code is correct... :S

Similar Threads

  1. Replies: 0
    Last Post: March 7th, 2013, 01:50 PM
  2. need help in making code
    By engmohy90 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 16th, 2013, 01:39 PM
  3. helo..nid help for making java code..tnx.
    By wap in forum Member Introductions
    Replies: 0
    Last Post: June 24th, 2012, 07:57 AM
  4. I had an old code it really sucked, so i now am making a new one
    By Lurie1 in forum What's Wrong With My Code?
    Replies: 74
    Last Post: February 9th, 2012, 07:48 PM
  5. making my code a little better, if needed.
    By vendetta in forum Object Oriented Programming
    Replies: 4
    Last Post: February 11th, 2010, 03:40 AM