0

I realize I can put my data in the method that my constructor is calling and that it will work. However I am just messing with some code and I am trying to have additional methods do the calculations. This is a problem from a book, and since I do not have an instructor to bug for a few weeks I figured I would ask here.

Any input? Please and thank you

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace ConsoleApplication305
{
class Program
{
    static void Main(string[] args)
    {
        Trip a = new Trip();
        Console.WriteLine(a);

    }

    class Trip
    {
        int distanceTraveled;
        double costGas;
        int numberGallon;
        int mpg;
        double cpm;

        public int DistanceTraveled
        {
            get
            {
                return distanceTraveled;
            }
        }
        public double CostGas
        {
            get
            {
                return costGas;
            }
        }
        public int NumberGallon
        {
            get
            {
                return numberGallon;
            }
        }

        public Trip()
        {
            distanceTraveled = 312;
            costGas = 3.46;
            numberGallon = 10;

        }

        public void milesPerGal()
        {
            mpg = distanceTraveled / numberGallon;
        }

        public void costPerMile()
        {
            cpm = costGas * mpg;
        }

        public override string ToString()
        {
            return String.Format("The distance traveled is...{0} \nThe cost per gallon of gasoline is... {1} \nThe amount of gallons were... {2} \nThe miles per gallon attained were... {3} \nThe cost per MPG were... {4}", distanceTraveled, costGas, numberGallon, mpg, cpm);
        }
    }
}

}

Zoro
  • 695
  • 2
  • 7
  • 23
  • Your `Trip()` constructor does not call the `milesPerGal` and `costPerMile` methods. If you are aware of that, which is what I assume from your *"I realize I can put my data in the method that my constructor is calling and that it will work."* statement, I fail to understand your question. What is it exactly, that you are asking? – Jan Dörrenhaus Aug 16 '13 at 00:33
  • How do I call it? I seem to get an error when I try to call the method. My original realization was that I don't need the method and can just enter the data. But When I make the other 2 methods, I do not know how to call them in my constructor. I am very noob and trying to learn. – Zoro Aug 16 '13 at 00:37
  • The question is confusing. from "I am trying to have additional methods do the calculations" I think you want to change the costPerMile() and milesPerGal() methods to return a value and then call it on the object. – Aaron Aug 16 '13 at 00:38

2 Answers2

1

IF I understand your question correctly, this should work for you:

class Trip
{
    int distanceTraveled;
    double costGas;
    int numberGallon;
    int mpg;
    double cpm;

    public int DistanceTraveled
    {
        get
        {
            return distanceTraveled;
        }
    }
    public double CostGas
    {
        get
        {
            return costGas;
        }
    }
    public int NumberGallon
    {
        get
        {
            return numberGallon;
        }
    }

    public Trip()
    {
        distanceTraveled = 312;
        costGas = 3.46;
        numberGallon = 10;
        mpg = milesPerGal();
        cpm = costPerMile();
    }

    public int milesPerGal()
    {
        return distanceTraveled / numberGallon;
    }

    public double costPerMile()
    {
        return costGas * mpg;
    }

    public override string ToString()
    {
        return String.Format("The distance traveled is...{0} \nThe cost per gallon of gasoline is... {1} \nThe amount of gallons were... {2} \nThe miles per gallon attained were... {3} \nThe cost per MPG were... {4}", distanceTraveled, costGas, numberGallon, mpg, cpm);
    }
}
Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45
  • This is exactly what I was meaning. I apologize for how unclear my question was. I said I was noob. Thank you!!! – Zoro Aug 16 '13 at 00:44
1

Everything works fine here, to be honest. Since you never called the methods milesPerGal or costPerMile, the values for the private variables mpg and cpm are still 0, which will be output as well.

What you probably wanted was this constructor:

public Trip()
{
    distanceTraveled = 312;
    costGas = 3.46;
    numberGallon = 10;
    milesPerGal();
    costPerMile();
}

To make this code work as you expect, I would suggest you to use proper getters and setters like this:

class Trip
{
    public int DistanceTraveled { get; set; }
    public double CostGas { get; set; }
    public int NumberGallon { get; set; }
    public int MilesPerGallon { get { return (NumberGallon != 0) ? DistanceTraveled / NumberGallon : 0; } }
    public double CostPerMile { get { return CostGas * MilesPerGallon; } }

    public Trip()
    {
        DistanceTraveled = 312;
        CostGas = 3.46;
        NumberGallon = 10;
    }

    public override string ToString()
    {
        return String.Format("The distance traveled is...{0} \nThe cost per gallon of gasoline is... {1} \nThe amount of gallons were... {2} \nThe miles per gallon attained were... {3} \nThe cost per MPG were... {4}", DistanceTraveled, CostGas, NumberGallon, MilesPerGallon, CostPerMile);
    }
}

Note that with this approach, whenever you update any of the fields that have a setter (DistanceTraveled, CostGas and NumberGallons), the respective derived values (MilesPerGallon and CostPerMile) will be automatically correct without any additional method-calls, since they are calculated on the fly, when someone accesses these fields.

Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
  • I have never programmed before until a month and a half ago. Teaching myself I miss a lot of things that are obvious to others. Maybe my programming class in a few weeks will help! Thanks! – Zoro Aug 16 '13 at 00:45
  • If this answers you question, please accept the answer. For more information on getters and setters see http://stackoverflow.com/questions/6709072/c-getter-setter and http://stackoverflow.com/questions/1568091/why-use-getters-and-setters – Alexander Pacha Aug 16 '13 at 00:47