Skip to content

Instantly share code, notes, and snippets.

@Dyrits
Forked from codecademydev/IDirectable.cs
Last active May 10, 2023 08:06
Show Gist options
  • Save Dyrits/2a8fe267af249885f212820be99d6fb0 to your computer and use it in GitHub Desktop.
Save Dyrits/2a8fe267af249885f212820be99d6fb0 to your computer and use it in GitHub Desktop.
Rover Control Center

Rover Control Center

Welcome to SPACE: Space Program for Aeronautical Collection and Exploration. You are in charge of the Rover Control Center. All rovers on Moon and Mars are under your direction!

This project will show you some ways in which references, inheritance, interfaces, and arrays can help you be a better C# programmer.

When you start this project you don’t need to read through the class definitions. In fact, the project will tell you when you need to look. To start, all you need to know is that MarsRover and MoonRover inherit from Rover:

class MoonRover : Rover
class MarsRover : Rover
namespace RoverControlCenter
{
interface IDirectable
{
public string GetInfo();
public string Explore();
public string Collect();
}
}
using System;
namespace RoverControlCenter
{
class Program
{
static void Main(string[] args)
{
MoonRover lunokhod = new MoonRover("Lunokhod 1", 1970);
MoonRover apollo = new MoonRover("Apollo 15", 1971);
MarsRover sojourner = new MarsRover("Sojourner", 1997);
Satellite sputnik = new Satellite("Sputnik", 1957);
Rover[] rovers = { lunokhod, apollo, sojourner };
DirectAll(rovers);
Object[] probes = { lunokhod, apollo, sojourner, sputnik };
TrackProbes(probes);
IDirectable[] directables = { lunokhod, apollo, sojourner, sputnik };
DirectAll(directables);
}
static void DirectAll(IDirectable[] rovers)
{
foreach (IDirectable rover in rovers)
{
Console.WriteLine(rover.GetInfo());
Console.WriteLine(rover.Explore());
Console.WriteLine(rover.Collect());
}
}
static void TrackProbes(Object[] probes)
{
foreach(Object probe in probes)
{
Console.WriteLine($"Tracking a {probe.GetType()}...");
}
}
}
}
namespace RoverControlCenter
{
class Rover: IDirectable
{
public string Alias
{ get; private set; }
public int YearLanded
{ get; private set; }
public Rover(string alias, int yearLanded)
{
Alias = alias;
YearLanded = yearLanded;
}
public string GetInfo()
{
return $"Alias: {Alias}, YearLanded: {YearLanded}";
}
public virtual string Explore()
{
return "Rover is exploring the surface!";
}
public virtual string Collect()
{
return "Rover is collecting rocks!";
}
}
}
namespace RoverControlCenter
{
class Satellite: IDirectable
{
public string Alias
{ get; private set; }
public int YearLaunched
{ get; private set; }
public Satellite(string alias, int yearLaunched)
{
Alias = alias;
YearLaunched = yearLaunched;
}
public string GetInfo()
{
return $"Alias: {Alias}, YearLaunched: {YearLaunched}";
}
public virtual string Explore()
{
return "Satellite is exploring the far reaches of space!";
}
public virtual string Collect()
{
return "Satellite is collecting photographic evidence!";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment