Learning C# with Xamarin: Object-Oriented Programming

Duration

15 minutes

Lab goals

Here you will complete the implementation of a RoadTrip class 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:

Below is sample output from the finished application.

- - - - - - - - - MISSING IMAGE - - - - - - - - - -

Required assets

The provided Exercise 2 folder contains resources that you will need in order to complete the lab:

Please make sure you have this folder before you begin.

Tip: If you are doing this exercise live in a session, make sure to make good use of the instructor, they are online to answer any questions you have!

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.
  2. 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 double so they can store floating-point values.

Show Code

Implement the Main program

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

  1. Open the file Program.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 two RoadTrip objects. 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. Add Console.WriteLine calls to print all fields of the two objects. This requires 6 calls to Console.WriteLine, but it is a useful exercise since it shows you that each object has its own copy of all the fields. That is, the chores object and the vacation object are independent, they each store their own data. The code below shows a sample for 1 of the 6 Console.WriteLine statements 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 to public.

Summary

In this lab, you added to your RoadTrip class a few fields to store data values and then wrote some code to utilize that data.

Go Back