Exercise 3.2 Planning a Road Trip - Part 2


Lab goals
Here you will complete the implementation of aRoadTripclass that you began in Part 1. This part adds fields to the class which are used to calculate metrics for your journey. The high-level goals for the exercise are listed below:

  • Implement three fields to hold miles, hours elapsed and gallons used in our RoadTrip class. All of these fields of type double.
  • Add code to Main method to use the RoadTrip class and output the value of the fields.

Below is sample output from the finished application.

Required assets

The provided Part 02 Resources folder contains resources that you will need in order to complete the lab:

  • The Part2.Start folder contains a starter solution you can use if you did not complete the previous exercise, or if you would like to begin with a fresh project.

Please make sure you have this folder before you begin.

Steps


Below are the step-by-step instructions to implement the exercise.

Implement the RoadTrip fields

In this section, you will add data fields inside the RoadTrip class.

1.Open the RoadTrip.cs file in your IDE.

  1. Make the RoadTrip class public, this is always a good idea if you plan to use the class in other places.

3.Inside the RoadTrip class, add three fields to store information about your journey:Miles,HoursElapsed, and GallonsUsed. All the fields should be public so they can be accessed from other code such as Main. All the fields should be of type doubles o they can store floating-point values.

namespace Travel
{
   public class RoadTrip
   {
      public double Miles;
      public double HoursElapsed;
      public double GallonsUsed;
   }
}

Implement the Main program

This section asks you to write code to create objects and access their fields.

1.Open the fileProgram.cs. You will be adding code to the Main method inside MainClass.

2.Create two RoadTrip objects using the new operator (see below for a sample showing the creation of one of the two objects). Name them chores and vacation. You need these objects because they contain the fields you will use to store information about each trip. Each RoadTrip object gets its own copy of all the fields.

RoadTrip chores = new RoadTrip();

3.Load values of your choice into each of the fields of the twoRoadTripobjects. Since you have 2 objects with 3 fields each, this will require 6 assignments. The code below shows a sample for 1 of the 6 assignments you will need.

chores.Miles = 50.00;

4.AddConsole.WriteLinecalls to print all fields of the two objects. This requires 6 calls toConsole.WriteLine, but it is a useful exercise since it shows you that each object has its own copy of all the fields. That is, thechoresobject and thevacationobject are independent, they each store their own data. The code below shows a sample for 1 of the 6Console.WriteLinestatements you will need.

Console.WriteLine(chores.Miles);

5.Run the program to test your work.

6.[Optional] If you have time, try changing the Miles field from public to private. Build your project and see what happens. You should get error messages where the Main method attempts to access Miles. We will explore this idea more in a future session; for now, please return the access level topublic.

results matching ""

    No results matching ""