none
HELP! beginners class... I need to develop a "MPG" application...

    Question

  • I already have the 2 forms made... I just don't know where to begin the actual coding, I have the "exit" buttons coded and that's all so far. Its sad haha. Literally stuck. I think once I get a good starting point I will be able to code the rest! Below is the actual assignment and the requirements... I know that I need to "declare" an empty array... and declare my structure? would that be where I need to begin?

    Miles Per Gallon Calculator

    Calculate the average MPG for a car.  The user would enter starting mileage, ending mileage, and gallons purchased for fuel.  The application would calculate number of miles (ending mileage - starting mileage) / gallons purchased.  This would be done for as many entries as the user desires.  The application would store/retrieve these details from a text file and display information to the form.  A list box may be a great option with the user selecting an entry to display further detail.

    The application would include the features listed below:


    1. Application includes at least two images
    2. All controls are renamed
    3. Tab stop is set
    4. Use variables
    5. Assign data types to variables
    6. Format numbers
    7. Create calculations
    8. Use decision structures (if...then...else)
    9. Use logical operators
    10. Use loops in an application
    11. Create a procedure
    12. Create a function
    13. Utilize arrays within an application
    14. Store and retrieve information from a data file
    15. Application includes multiple forms
    16. Application includes inline documentation
    Friday, April 28, 2017 9:34 PM

All replies

  • Nicole,

    I'm sure this is a class assignment of some sort?

    As such we can't write it for you but then again, it's not that tough from the looks of it.

    The "features" ... ehh - it'll use that as needed; get it functional first.

    Have you done anything at all? If so, post it using the <> code block tool and explain where you're stuck. The best way to start though is to work it out on paper and use that as a pseudo-reference when you write the actual code.

    I hope it helps and good luck with it all. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, April 28, 2017 9:47 PM
  • I know that I need to "declare" an empty array... and declare my structure? would that be where I need to begin?

    A structure (or class - the difference is slight for this case) will be needed to store the values per entry, but a List(Of()) is much preferable to an array. If you are obliged to use an array you will need to decide (or ask the user) about the number of entries to be allowed.  Creating the definition for the structure would be a good place to start.

    Then you will need to decide on the user input procedure.  For instance, will the user enter three values and then press an OK button?  If so, then you need three text boxes for inputting the values, and the OK button click event handler will have the code to collect the three entries, verify them, convert them to numeric, load them onto an instance of your class and add the class instance to the list.  That is the starting point you need. 

    Friday, April 28, 2017 9:55 PM
  • I'll add this while I'm thinking about it and on the same lines of what I said earlier about doing it manually:

    You'd probably set up a log book and each time you stopped to fill up* the tank, you'd write down the odometer reading along with other information like the gallons used and the total cost (if you're interested in cost-per-mile).

    So for a given period, you'll have several entries. That will be a collection of some sort, but for now just focus on the SECOND and subsequent rows of entries. What information can you ascertain from that?

    The delta mileage will tell you how many miles [since the last entry] and when you fill up, you'll then know how many gallons.

    Given that - how would you then calculate the "miles-per-gallon"? Show that please

    * You have to "fill up" each time or the values won't be correctly reflective.

    Food for thought. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Friday, April 28, 2017 10:31 PM
  • Thank you for the reply! Ok... I am in full panic mode, as Ive been trying to figure this out all day...  it is for my Visual Basic class at school, and I have a 95% in the class so far... so why is this final project so dang difficult??? I feel like there's just something I am missing!! Anyways, could you tell me what the "<> code block tool" is??? I have never used this forum before sorry :(

    Also, Yes I do have some done. I have managed to create the 2 forms needed for the program. And now for some reason when I open Visual Basic, only one of the forms "design" actually show up, the 2nd form, only the code I can see. I assume I am missing something here too!!! because clearly the second form is "there" if the code is still visable?

    I do have it written out on paper (wish I could photo-copy it and post it here haha) Heres some of what I have. PLEASE bare with me lol, I know this is SUPER long... I just want to make sure I included everything!!!!

    Input: starting mileage, ending mileage, gallons purchased

    Output: Miles per gallon

    Calculation: (ending mileage - starting mileage) / gallons = mpg

    Data Fields: Start Mileage Int (integer)

                     Ending Mileage Int (Integer)

                     Gallons Purchased Int

    ------------------------------------------------ (above line is in the text file, ALL is included in the structure)

    Structure: MPG                      Decimal(dec)

    As far as interface design, I have 2 forms as mentioned before.

    1st form: (named Data) includes a BtnAdd, a BtnEdit, and a BtnClose, and a list box, (lstDetail) start mileage, end mileage, gallons purchased and MPG are passed to the list box, from the structure?? (NOT sure if thats correct???? Does the structure pass these to the list box??)

    2nd form: (named MPGCalc) Has, 3 text boxes and 1 label. txtStarting (starting mileage) txtEnding (ending mileage) txtGallons (Gallons Purchased) and lblMPG then 2 buttons, BtnClose and BtnUpdate so this is the form where the user will be inputting the information I need.

    Now where I am confused.... "The application would store/retrieve these details from a text file (I know how to make the text file itself!) and display the information to the form." How do I get the details TO the text file??

    Friday, April 28, 2017 10:56 PM
  • I should mention the purpose of the list box and text file, are so the user can click on an entry in the list box and expand to see the details!
    Friday, April 28, 2017 10:59 PM
  • Thank you for the reply! Ok... I am in full panic mode, as Ive been trying to figure this out all day...  it is for my Visual Basic class at school, and I have a 95% in the class so far... so why is this final project so dang difficult??? I feel like there's just something I am missing!! Anyways, could you tell me what the "<> code block tool" is??? I have never used this forum before sorry :(

    Also, Yes I do have some done. I have managed to create the 2 forms needed for the program. And now for some reason when I open Visual Basic, only one of the forms "design" actually show up, the 2nd form, only the code I can see. I assume I am missing something here too!!! because clearly the second form is "there" if the code is still visable?

    I do have it written out on paper (wish I could photo-copy it and post it here haha) Heres some of what I have. PLEASE bare with me lol, I know this is SUPER long... I just want to make sure I included everything!!!!

    Input: starting mileage, ending mileage, gallons purchased

    Output: Miles per gallon

    Calculation: (ending mileage - starting mileage) / gallons = mpg

    Data Fields: Start Mileage Int (integer)

                     Ending Mileage Int (Integer)

                     Gallons Purchased Int

    ------------------------------------------------ (above line is in the text file, ALL is included in the structure)

    Structure: MPG                      Decimal(dec)

    As far as interface design, I have 2 forms as mentioned before.

    1st form: (named Data) includes a BtnAdd, a BtnEdit, and a BtnClose, and a list box, (lstDetail) start mileage, end mileage, gallons purchased and MPG are passed to the list box, from the structure?? (NOT sure if thats correct???? Does the structure pass these to the list box??)

    2nd form: (named MPGCalc) Has, 3 text boxes and 1 label. txtStarting (starting mileage) txtEnding (ending mileage) txtGallons (Gallons Purchased) and lblMPG then 2 buttons, BtnClose and BtnUpdate so this is the form where the user will be inputting the information I need.

    Now where I am confused.... "The application would store/retrieve these details from a text file (I know how to make the text file itself!) and display the information to the form." How do I get the details TO the text file??

    Who are you talking to?

    We don't all see this forum the same way to I can't tell.

    *****

    To use the code block tool, when you post you'll see a menu bar (of sorts) at the top and one has the "<>" symbol in it. Click that, choose "VB" and then paste your code in there.

    That said though, tell me how you'd do it manually? Until you get that worked out, don't worry about doing it in code yet.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, April 28, 2017 11:05 PM
  • I was talking to both of you (or anyone else who tried to help) I just wanted to include what the forms looked like, and all the details I already have down. What do you mean by "manually"? I thought that was what I just posted (that you quoted)?
    Friday, April 28, 2017 11:09 PM
  • I was talking to both of you (or anyone else who tried to help) I just wanted to include what the forms looked like, and all the details I already have down. What do you mean by "manually"? I thought that was what I just posted (that you quoted)?

    Show me this in code please?

    Calculation: (ending mileage - starting mileage) / gallons = mpg


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, April 28, 2017 11:12 PM
  • Also, Yes I do have some done. I have managed to create the 2 forms needed for the program. And now for some reason when I open Visual Basic, only one of the forms "design" actually show up, the 2nd form, only the code I can see. I assume I am missing something here too!!! because clearly the second form is "there" if the code is still visable?

    Select the second form in the Solution Explorer and right click.  Choose 'View Designer'.

    Data Fields: Start Mileage Int (integer)

                     Ending Mileage Int (Integer)

                     Gallons Purchased Int

    ------------------------------------------------ (above line is in the text file, ALL is included in the structure)

    Structure: MPG                      Decimal(dec)

    The advantage of using a class (or structure) is that it can be easily changed as the application develops.  So don't be too concerned about the design now.  The basics you have above are fine.  The important point is that there will be several instances of this object - that's what the array is for.  It will be an array of instances of your class (or structure).  However, a list will be easier to use than an array.  Perhaps you can satisfy the requirements by using an array somewhere else.

    1st form: (named Data) includes a BtnAdd, a BtnEdit, and a BtnClose, and a list box, (lstDetail) start mileage, end mileage, gallons purchased and MPG are passed to the list box, from the structure?? (NOT sure if thats correct???? Does the structure pass these to the list box??)

    The list (or array) of class (or structure) instances is where your data is stored.  That's what is updated when new data is added, and changed when data is edited or deleted (only Add concerns you for now).   This list (or array) is the source from which the list box is updated.  You do this whenever the list changes.  It is simplest to rewrite the list box from scratch - it might be inefficient but that just doesn't matter.  So you will create a routine that scans the collection of class instances and writes the information to the list box, and call it whenever the list box needs to be refreshed.

    2nd form: (named MPGCalc) Has, 3 text boxes and 1 label. txtStarting (starting mileage) txtEnding (ending mileage) txtGallons (Gallons Purchased) and lblMPG then 2 buttons, BtnClose and BtnUpdate so this is the form where the user will be inputting the information I need.

    You will need to get these values from the text boxes, validate them and put them into an instance of your class (or structure).  That instance will be exposed as a property of this form, so that the main form can access it and update the collection (and, of course, refresh the listbox).  This form will be a dialog.  It's a good place to start.  The detail of getting the result back to the main form is a bit tricky, but someone can show you the setup for that when you have the code for the form itself.

    Now where I am confused.... "The application would store/retrieve these details from a text file (I know how to make the text file itself!) and display the information to the form." How do I get the details TO the text file??

    Forget that for now - it's pretty much the last task you will tackle.  If you create and use your collection of class instances correctly, you already have nearly everything you need to save the information to file and reload it from file.

    The forums are misbehaving, so your responses might be intermittent.

    • Edited by AcamarMVP Friday, April 28, 2017 11:21 PM fmt
    Friday, April 28, 2017 11:19 PM
  • @Acamar MVP Thank you so very much for your help, I will try to apply this today! And see where I end up. I have a couple of examples I can look back on, its just a matter of tweeking them to help me with this exact program. I think my problem is I am including WAY more code than I need to.
    Saturday, April 29, 2017 5:17 PM
  • I should mention the purpose of the list box and text file, are so the user can click on an entry in the list box and expand to see the details!

    You also wrote this : "Now where I am confused.... "The application would store/retrieve these details from a text file (I know how to make the text file itself!) and display the information to the form." How do I get the details TO the text file??"

    If that is a requirement, it seems to me that each "data set" needs to have some kind of unique identifier, such as the person's name and the date/time the MPG was calculated. Each data set can be saved to a text file using System.IO

            Dim Data As String = "Bob Smith,01/17/2017,14:22:21,113464,114101, 23.5" & vbNewLine
            Data = Data & "Larry Bern, 01/21/2017,11:53:32,34721,35109,9.8" & vbNewLine
            File.WriteAllText("C:\Mileage.txt", Data, System.Text.Encoding.ASCII)
    

    The list box could be populated with the Name, Date and Time. The "code block tool" is the icon that looks like a piece of paper with <> at the lower left, that's what I used to insert the example code above. If you hover the mouse over the icons, a little popup shows what each one is.
    ------------------------------------------------HERE--------

    Miles and gasoline are normally measured in decimal units not integers, don't forget that. Also if you allow a user to enter a number in a textbox, it's good to make sure that he or she does not enter something that is invalid, Start Miles of 50500 and ending miles of 565w4 will crash the program, so you should consider Integer.TryParse or Decimal.TryParse. If you are dividing by gallons, make sure a number other than zero is entered. Ending miles must be greater then starting miles, lots of things to consider

    Saturday, April 29, 2017 6:20 PM
  • Nicole,

    I don't know where you are with your project but if you're interested, I put together my own version using randomly generated [fake] data:

    One point here is that you have to be mindful not of a single instance but rather of a collection of them.

    If you're interested then I'll zip up the project directory and upload it for you. That way you can step through mine and get some ideas about how you might set up your own.

    Let me know if you're interested.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 3, 2017 11:32 AM