hej guys,
I am trying to solve this homework. I have reached a dead end and I am looking for someone who could help with this issue.
This is the task I am trying to solve:
Work 2: An abstract class is not a complete class, it misses some
parts, and you cannot create objects from it. The programmer who
writes the derived classes must fill in the missing parts. Consider
an abstract class Vehicle. Derive two hierarchies from this class as
it is shown below: Now, write 4 classes, see the yellow rectangle.
Start from the abstract base class Vehicle -> Vehicle with 4 wheels
-> Sport Cars and stop at the derived class Rally, which is the most specific class. The class Vehicle contains a field which
holds the vehicle name and an abstract method void Display().Implement this function in the derived classes, so that the function
returns information about the vehicle, e.g. the motor power and other
necessary properties. The last derived class has private fields to
hold the motor power, the car weight, the car acceleration, the
highest speed and a function that computes the specific power (power
/ weight). The function Display returns a text string with all this
information. Test your work in a Console application that uses
objects of the type of the classes Sport car and Rally.
Class Vehicle:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_work_2
{
public abstract class Vehicle
{
public string vehicleName;
public abstract void Display();
}
}
Class Vehicle4Wheels:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_work_2
{
public class Vehicle4Wheels : Vehicle
{
public override void Display()
{
Console.WriteLine("Car with four wheels.");
}
}
}
Class SportCar:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_work_2
{
public class SportCar : Vehicle4Wheels {
public override void Display()
{
Console.WriteLine("Sport version of the car.");
}
}
}
Class Rally:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_work_2
{
public class Rally : SportCar
{
private double motorPower = 408;
private double carWeight = 2380;
private double carAcceleration = 4.7;
private double highestSpeed = 250;
public double SpecificPower()
{
double specificPower = motorPower / carWeight;
return specificPower;
}
public override void Display()
{
Console.WriteLine("The acceleration is: {0}.\nThe highest speed is {1} km/h.", carAcceleration, highestSpeed);
Console.WriteLine("Specific power is {0}", SpecificPower());
}
}
}
I am not sure if I understand the task clearly and with the code I have so far, I dont know if I am on the right way to make it happen. I dont understand abstract classes and inhertiance very much.
Thank you for your help!
V.
EDIT: My question is: How can I change the code I have written to fulfill the task I have been given? Because I am not sure how to be general while writing the code. When I add car attribute to the Vehicle4Wheels class like:
private double carWeight;
How can I then approach this attribute in later derived classes?
So, lets review what you’ve got so far…
Your homework assignment didn’t tell you to worry about that.
You’ve implemented this pretty well already. The part you are missing is this:
Once you’ve implemented this, you’ll understand the point of all the code you’ve written.
To make it interesting, and so you get the point of the exercise, make sure your console app does something like this:
Notice I didn’t say
Rally rally = new Rally();Also notice that
Vehicle something = new SportsCar();works. Try adding a line:Vehicle something = new Vehicle();– It won’t compile.This all will demonstrate some of the most important parts of inheritance and (abstract) base types:
Try this all and see what happens.