Basic AF programmer needs help.

widellski

New member
Joined
Apr 15, 2019
Messages
2
Programming Experience
Beginner
Hey!

Just joined this site out of what one might call desperation.

I am doing an assignment and have very limited time left to do it, im doing night Courses on top of my Daily job. And have lately been very overworked, no rest for the wicked.

But i recently started a coding course, and this assignment is kind of kicking my butt.

The assignment is as follows;

I need to create a program that calculates the top speed of a veichle using one input, horsepower. The math is irrelevant. Its just a made up formula.

So im meant to store the horsepower, apply that to a random formula, and then send that information back to the user. After this has been completed I need to inherit the qualities of my first class. I.E the veichle onto Another class called "Truck". And then perform a different b.s formula with the addition of weight to it. The idea is to specifically use the inhertiance feature of VS studios to do this. But I feel like im missing so much more.

I am in the middle of Writing the code, and my brain just seems to have frozen. Included is the code im working on, any kind of help would be greatly appreciated.

C#:
namespace Fordonen
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Var god ange antalet hästkrafter; ");
            int horsepower = int.Parse(Console.ReadLine());
            Console.WriteLine("Ange färgen på ditt fordon; ");
            string color = Console.ReadLine();
            Console.WriteLine("Ange nu vikten på ditt fordon; ");
            int weight = int.Parse(Console.ReadLine());
            Fordon f = new Fordon(horsepower, color, weight); //skapa nytt objekt med parametrar som konstruktorn tar emot.
            f.PrintData();
        }

    }
    public class Fordon
    {
        public Fordon(int a, string b, int c)
        {
            horsepower = a; color = b; weight = c;
        } //Detta är konstruktorn, den anropas när objektet skapas

        public void PrintData()
        {
            Console.WriteLine("hästkrafter: {0}, color: {1}, weight {3}", horsepower, color, weight);
        }

        private int horsepower;
        private string color;
        private int _horsepower;
        private int weight;
    }
}
 
If you are supposed to inherit a class then you need a class to inherit. Without more specific guidance, it sounds like you need a Vehicle class with a Horsepower property and a GetTopSpeed method or read-only TopSpeed property that uses the value of the Horsepower property in its calculation. You would then define a Truck class that inherits Vehicle, adds a Weight property and overrides the GetTopSpeed method or TopSpeed property and uses both the Horsepower and Weight properties in its calculation. Your Main method will then create instances of those classes, pass the user input into them via the properties and then get the out back via the method or read-only property.
 
thanks for your reply. I moved around a bit, settled ona version where i dont inherit at all. This is the code I currently have, it does the job. But doesnot utilize the inheritance factor. Any chance I can get some assistance on how to do that? I keep getting errors with accesing the fields within my new class.n This is how itlooksright now:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fordonen
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Var god ange antalet hästkrafter; ");
            int horsepower = int.Parse(Console.ReadLine());
            Console.WriteLine("Ange färgen på ditt fordon; ");
            string color = Console.ReadLine();
            Fordon f = new Fordon(horsepower, color); //skapa nytt objekt med parametrar som konstruktorn tar emot.
            f.PrintData();
            Console.ReadKey();
            Console.WriteLine("Nu är det dags att testa en lastbil! ");
            Console.WriteLine("Var god ange hästkrafterna; ");
            int _horsepower = int.Parse(Console.ReadLine());
            Console.WriteLine("Ange färgen på din lastbil;" );
            string _color = Console.ReadLine();
            Console.WriteLine("Och sist men tyngst, ange lastbilens vikt; ");
            int weight = int.Parse(Console.ReadLine());
            Fordon l = new Lastbil(_horsepower, _color, weight);
            l.printData();
            Console.WriteLine("Kalkulation avklarad, tryck på valfri knapp för att avsluta programmet");           
            Console.ReadKey();
        }

    }
    public class Fordon
    {
        public Fordon(int a, string b)
        {
            horsepower = a; color = b;
        } //Detta är konstruktorn, den anropas när objektet skapas

        public void PrintData()
        {
            int topspeed = horsepower * 2 - 200;
            Console.WriteLine("Maxhastigheten är; " + topspeed);

        }

        private int horsepower;
        private string color;
        private int weight;

    }
    public class Lastbil : Fordon
    {
        public void printData()
        {
            int topspeed = horsepower + weight * 2 - 200;
            Console.WriteLine("Maxhastigheten på lastbilen är; ");
        }
    }
}
 
I've provided guidance on what you should do with regards to inheritance. Follow those instructions first and then, if there's an issue with that implementation, I can help further.
 
Back
Top Bottom