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 6 of 6

Thread: can anyone think of a more efficient way of doing this...

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default can anyone think of a more efficient way of doing this...

    guys, this code calculate the amount of classes that a student needs to take lets say for 17 credits. It will be 5 three credits classes and 2 one credit classes, but I think the loop is kind of unnecessary. could anyone post a more efficient way? thanks

    public static void numOfClasses(int credits)
        {
            int class3 = 3, class1 = 1, numOfClass;
     
            do
            {
                if(credits >= 3)
                {
                    numOfClass = credits / class3;
                    credits -= (numOfClass * class3);
                    System.out.println("3 credits classes: "+numOfClass);
                }           
                else
                {
                    numOfClass = credits / class1;
                    credits -= (numOfClass * class1);
                    System.out.println("1 credit classes: "+numOfClass);
                }                
            }while(credits != 0);
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can anyone think of a more efficient way of doing this...

    Look at using the /, - and % operators.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: can anyone think of a more efficient way of doing this...

    Quote Originally Posted by Norm View Post
    Look at using the /, - and % operators.
    like this? or could be better?

    public static void numOfClasses(int totalCred)
        {
            int numOfClass = 0;
     
            if(totalCred >= 3)
            {
                numOfClass = totalCred / 3;
                totalCred = totalCred % 3;
                System.out.println("3 credit classes: "+numOfClass);
            }
            if(totalCred >= 1)
            {
                numOfClass = totalCred;
                System.out.println("1 credit classes: "+numOfClass);
            }
        }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can anyone think of a more efficient way of doing this...

    Does that code give the correct results?
    Do you intend that both if statements be true if the first one is true?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: can anyone think of a more efficient way of doing this...

    Quote Originally Posted by Norm View Post
    Does that code give the correct results?
    Do you intend that both if statements be true if the first one is true?
    yes, it gives correct result... now both statement are not always true, depending on the total of credits "totalCred"... have you try it?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can anyone think of a more efficient way of doing this...

    No, I haven't tried it. If you get the correct results that should be the test.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to make code more efficient?
    By Apocalypse in forum Java Theory & Questions
    Replies: 2
    Last Post: October 21st, 2011, 09:07 AM
  2. efficient way to build Tree gui
    By yinp in forum AWT / Java Swing
    Replies: 8
    Last Post: August 29th, 2011, 07:01 AM
  3. efficient way of checking duplicates
    By starmandell in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 2nd, 2011, 06:52 PM
  4. What is the fastest and most efficient for 3 dimensional structures?
    By aussiemcgr in forum Collections and Generics
    Replies: 10
    Last Post: December 11th, 2010, 07:50 PM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM