Answer to Question #265406 in C# for laiba

Question #265406

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.


1
Expert's answer
2021-11-13T05:43:30-0500
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();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS