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

Thread: OOPAnnualFuelUse

  1. #1
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default OOPAnnualFuelUse

    All I need to know is if I am on the right track in terms of min and max, not neccessarily how to finish the program.
    Please, reply yes or no, and if no, why.

    // here is what I have
     class AnnualFuelUse
    {
        // instance variables - replace the example below with your own
        private int day, sMiles, eMiles, dist;
        private double gUsed, mpg, ppg, cost;
        public AnnualFuelUse(int dy,int sM,int eM,double gU, double pPg)
        {
            // initialise instance variables
           day = dy;
           sMiles = sM;
           eMiles = eM;
           gUsed = gU;
           dist = 0;
           ppg = pPg ;
           mpg = 0.0;
           cost = 0.0;
        }
        public void calcDistance()
        {
            dist = eMiles - sMiles;
        }
        public int getDistance()
        {
            // put your code here
            return dist;
        }
        public void calcMPG()
        {
            // put your code here
            mpg = ((dist)/(gUsed));
        }
        public double getMPG()
        {
            // put your code here
            return mpg;
        }
        public void calcCosts()
        {
            cost = (mpg * ppg);
        }
        public double getCost()
        {
            return cost;
        }
    }
    And for the tester
    public class AnnualFuelUseTester
    {
       public static void main (String[] Args)
       {
           AnnualFuelUse[] aFUT = { new AnnualFuelUse(1, 45023, 45231, 10.00, 2.95), 
                                           new AnnualFuelUse(4, 45231, 45480, 11.70, 2.99),  
                                           new AnnualFuelUse(8, 45480, 45659, 9.30, 2.99)
                                  };
     
           double [] mpg = new double[3];
           double [] cost = new double[3];
           double [] distance = new double[3]; 
           for(int i = 0; i < distance.length; i++)
              {
               aFUT[i].calcDistance();
               distance[i] = aFUT[i].getDistance();
              }
           for(int i = 0; i < mpg.length; i++)
              {
               aFUT[i].calcMPG();
               mpg[i] = aFUT[i].getMPG();
              }
           for( int i = 0; i < cost.length; i++) 
             { 
              aFUT[i].calcCosts();
              cost[i] = aFUT[i].getCost(); 
             }
           double max = Double.MIN_VALUE;
           double min = Double.MAX_VALUE;
           for(int i = 0; i < 3; i++)
               {
               if( distance[i]> max)
                   max = distance[i];
               }
           for(int i = 0; i < 3; i++)
               {
                if( distance[i]< min)
                    min = distance[i];
               }
           double max1 = Double.MIN_VALUE;
           double min1 = Double.MAX_VALUE;
           for(int i = 0; i < 3; i++)
              { 
               if( mpg[i]> max1)
                   max1 = mpg[i];
              }
           for(int i = 0; i < 3; i++)
               {
               if( mpg[i] < min1)
                   min1 = mpg[i] ;
               }
           double max2 = Double.MIN_VALUE;
           double min2 = Double.MAX_VALUE;
           for(int i = 0; i < 3; i++)
              { 
               if( cost[i]> max2)
                   max2 = cost[i];
              }
           for(int i = 0; i < 3; i++)
               {
               if( cost[i] < min2)
                   min2 = cost[i] ;
               }
     
       }
    }
    I made some changes to tester again please review!?
    Last edited by llowe29; August 1st, 2013 at 04:17 PM. Reason: I made some changes to tester please review

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: OOPAnnualFuelUse

    It's difficult to give a yes/no judgement on a program that has a variable name missing and an unfinished for loop. I do see one thing I'm curious about:

    In the main() method, you call getDistance() before calcDistance() is ever called. In that case, getDistance() can only return 0.

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: OOPAnnualFuelUse

    So how would I fix that

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: OOPAnnualFuelUse

    Quote Originally Posted by llowe29 View Post
    So how would I fix that
    All I need to know is if I am on the right track, not neccessarily how to finish the program.
    Please, reply yes or no, and if no, why.
    I tried to comply with your desires for help based on the the clear instructions you gave. Did you change your mind? And, frankly, my comment included the answer to "how to fix it?".

  5. #5
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: OOPAnnualFuelUse

    Sorry if I offended you; Please check this new version. its just that I wasn't sure what you meant in the comment so I was requesting an example.

  6. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: OOPAnnualFuelUse

    Quote Originally Posted by GregBrannon View Post
    In the main() method, you call getDistance() before calcDistance() is ever called.
    thats what you should be looking at in his reply

  7. #7
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: OOPAnnualFuelUse

    So did I solve the problem in my new version.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: OOPAnnualFuelUse

    Quote Originally Posted by llowe29 View Post
    Sorry if I offended you; Please check this new version. its just that I wasn't sure what you meant in the comment so I was requesting an example.
    You didn't offend. I just don't understand why you'd post specific instructions you didn't mean. That's not characteristic of a programmer striving to improve his craft while sharing what s/he knows with others.

    Glad you solved that and good luck finishing the assignment.

    Keep coding!

  9. #9
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: OOPAnnualFuelUse

    Thanks.Until Im done I may be upating newer versions, So check back for more please. Bye for now.

  10. #10
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: OOPAnnualFuelUse

    does it do what you want it to do? if so then yes you solved it if not then tell whats happening that shouldnt be happening..its not bad to have stuff not go the way you want right away because it teaches you how to debug and gives you experience fixing problems...which is what programming is

  11. #11
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: OOPAnnualFuelUse

    Yeah it works for now.

    --- Update ---

    Told you it wouldnt be very long before I updated my post

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: OOPAnnualFuelUse

    Now I would suggest that the class' AnnualFuelUse() constructor actually construct the object rather than expecting the calling program to do it. Specifically:
    public AnnualFuelUse(int dy,int sM,int eM,double gU, double pPg)
    {
        // initialise instance variables
       day = dy;
       sMiles = sM;
       eMiles = eM;
       gUsed = gU;
       dist = calcDistance();
       ppg = pPg ;
       mpg = calcMPG();
       cost = calcCosts();
    }
    I'm not a big fan of a class calling its own methods, but it seems appropriate in this case.

  13. #13
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: OOPAnnualFuelUse

    Thanks,Did you find anything wrong with the tester

  14. #14
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: OOPAnnualFuelUse

    oh and I didn't do the thing with the class calling its own methods because my professor only likes you to submit it their way.

  15. #15
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: OOPAnnualFuelUse

    help

  16. #16
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: OOPAnnualFuelUse

    Thought you were done. What help do you need?

  17. #17
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: OOPAnnualFuelUse

    just skim through

  18. #18
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: OOPAnnualFuelUse

    If you have a question, ask it. We're not beta testers or assignment graders.

    And it's a good thing, because you deleted the assignment description. Shouldn't there be some output?

  19. #19
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: OOPAnnualFuelUse

    Hello.
    I believe your code looks fine based on my observation of your definitions in the code.
    Do you have any specific question?

    Syed.