Create a class that captures planets. Each planet has a name, a distance from the sun, and its gravity relative to Earth’s gravity. For distance and gravity, use the type double which captures real numbers. Make objects for Earth and your favorite non-earth planet.
using System;
namespace ConsoleApp1
{
class Program
{
public class Planet
{
// properties
public string Name;
public double Distance;
public double Gravity;
// constructor of Planet
public Planet(string name, double distance, double gravity)
{
Name = name;
Distance = distance;
Gravity = gravity;
}
// Output data about planet to console
public void Display()
{
Console.WriteLine("Planet: Name = {0}, Distance = {1:F3} km, Gravity = {2:F3} %", Name, Distance, Gravity);
}
}
static void Main(string[] args)
{
// Make objects
Planet earth = new Planet("Earth", 149.6 * 1000000 /* 149.6 mln km */, 1); ;
Planet favoritePlanet = new Planet("Mars", 227.9 * 1000000 /* 227.9 mln km */, 0.394 /* is 39.4% of the earth*/);
// Display data
earth.Display();
favoritePlanet.Display();
}
}
}
Comments
Leave a comment