C# help discription: public abstract class Chassis Chassis is an abstract class
ID: 3604926 • Letter: C
Question
C# help
discription:
public abstract class Chassis
Chassis is an abstract class representing a generic tank model. You will need to create at least one concrete class inheriting from it for this game to work. Different tanks can have different graphics, weapons, armour etc.
public static Chassis CreateTank(int tankNumber)
This is a factory method, used to create a new object of a concrete class that inherits from Chassis and return it. This way different parts of the program can create a variety of different tanks without having to know anything about your concrete class.
If you only have one type of tank your method can simply be something like:
return new MyTank();
If you have multiple varieties of tank, you will want to return a specific type of tank based on the value of tankNumber. The unit tests assume that tank numbers start at 1.
code in chassis abstract class:
public static Chassis CreateTank(int tankNumber)
{
}
test case(code in main function): Chassis tank = Chassis.CreateTank(1);
how do i make the last line of code to work?
Explanation / Answer
Chassis tank = Chassis.CreateTank(1);
Above line assumes that Chassis class's CreateTank() function will create one new instance of Chassis class and return it back so that it will get assigned to tank. To achieve that you have to complete the function code in CreateTank() method of Chassis class as follows:
public static Chassis CreateTank(int tankNumber)
{
}
This method is called as factory method means return type is one and intenally many instances of same type are getting created based on given condition. Here condition expression can be generated based on given "tankNumber". Like for example if tankNumber is 1 then different instance of Chassis and if tankNumber is 2 then different instance of Chassis.
Here different instance of Chassis means there will be some child classes of Chassis class which will be implmenting the Chassis class hence based on given tankNumber you have to created different instance of different implementing class of Chassis.
Note: Chassis class's instance can not be created as this is abstract class so you have to create instance of child classes of Chassis class based on given tankNumber.
Please provide details of child classes of Chassis class and what is the use of tankNumber of in child classes then I can tell you the complete code also for factory method.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.