Vehicle Object Array - C# -
i'm having trouble writing code. i'm not sure , how write constructors , accessors.
the activity have this:
write 3 derived classes allow user enter details of 3 types of vehicles attributes.
• car (make, model, year, bodytype)
• airplane (make, model, year, noengines, enginetype)
• boat (make, model, year, length, hulltype)
the 4th class base class vehicle contains shared attributes , methods
make attributes either private (in derived classes) or protected (in base class) , write accessor methods each attribute.
write 2 constructors each derived class. 1 no arguments , other accepts values of attributes in derived class arguments.
write console application called fleet.cs creates , displays 2 of each vehicle type
my code far follows:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace consoleapplication5 { class vehicle { static void main(string[] args) { } class car { protected string make { { return make; } set { make = value; } } protected string model { { return model; } set { model = value; } } protected int year { { return year; } set { year = value; } } protected string bodytype { { return bodytype; } set { bodytype = value; } } public bool isinitialized; public car() { isinitialized = true; } } } class airplane { protected string make { { return make; } set { make = value; } } protected string model { { return model; } set { model = value; } } protected int year { { return year; } set { year = value; } } protected int numengines { { return numengines; } set { numengines = value; } } protected int enginetype { { return enginetype; } set { enginetype = value; } } } class boat { protected string make { { return make; } set { make = value; } } protected string model { { return model; } set { model = value; } } protected string year { { return year; } set { year = value; } } protected string length { { return length; } set { length = value; } } protected string hulltype { { return hulltype; } set { hulltype = value; } } } }
first part oop principles
a class construct enables create own custom types grouping variables of other types, methods , events. class blueprint. defines data , behavior of type. if class not declared static, client code can use creating objects or instances assigned variable. variable remains in memory until references go out of scope. @ time, clr marks eligible garbage collection. if class declared static, 1 copy exists in memory , client code can access through class itself, not instance variable. more information, see static classes , static class members (c# programming guide). unlike structs, classes support inheritance, fundamental characteristic of object-oriented programming. more information, see inheritance (c# programming guide).
also objects instances of classes.
inheritance, encapsulation , polymorphism, 1 of 3 primary characteristics (or pillars) of object-oriented programming. inheritance enables create new classes reuse, extend, , modify behavior defined in other classes. class members inherited called base class, , class inherits members called derived class. derived class can have 1 direct base class. however, inheritance transitive. if classc derived classb, , classb derived classa, classc inherits members declared in classb , classa.
derived class:
class created based on existing class (i.e., base class). derived class inherits of member variables , methods of base class derived. called derived type.
a method (or message) in object-oriented programming (oop) procedure associated object class. object made of behavior , data. data represented properties of object , behavior methods. methods interface object presents outside world. example window object have methods such open , close. 1 of important capabilities method provides method overriding. same name (e.g., area) can used multiple different kinds of classes. allows sending objects invoke behaviors , delegate implementation of behaviors receiving object. example object can send area message object , appropriate formula invoked whether receiving object rectangle,circle, triangle, etc.
"fields", "class variables", , "attributes" more-or-less same - low-level storage slot attached object. each language's documentation might use different term consistently, actual programmers use them interchangeably. (however, means of terms can ambiguous, "class variable" - can interpreted "a variable of instance of given class", or "a variable of class object itself" in language class objects can manipulate directly.)
"properties" are, in languages use, else entirely - they're way attach custom behaviour reading / writing field. (or replace it.)
so if want categorize them oop(object oriented programming) principles.
second part:
write console application called fleet.cs creates , displays 2 of each vehicle type.
so 1 way of doing creating vehicles hardcoded. other way ask user vehicle details console.readline()
. main
method this.
static void main(string[] args) { vehicle v1 = new vehicle { make = "test1", model = "model1", year = 1996 }; vehicle v2 = new vehicle { make = "test2", model = "model2", year = 1997 }; console.writeline(v1); console.writeline(v2); ... }
and override tostring()
method each class. this:
public override string tostring() { return string.format("vehicle {0} , of model {1} , made in {2}.", make, model, year); }
here can use base.tostring()
data of upper (base) class in derivided class.
edit 1: user input:
so if want user input make program this:
static void main(string[] args) { //input vehicle v1 = new vehicle(); console.write("enter make of 1st vehicle: "); v1.make = console.readline(); console.write("enter model of 1st vehicle: "); v1.model = console.readline(); console.writeline("enter year of manufacturing 1st vehicle:"); v1.year = int.parse(console.readline()); //output console.writeline("the data 1st vehicle: "); console.writeline(v1); ... }
even better create input
method in class , calling main
program. code not repeating itself.
finished program
vehicle.cs
using system; class vehicle { string make, model; int year; public string make { { return make; } set { make = value; } } public string model { { return model; } set { model = value; } } public int year { { return year; } set { year = value; } } public vehicle() { make = model = "unknown"; year = 0; } public vehicle(string make, string model, int year) { this.make = make; this.model = model; this.year = year; } public virtual void getfrominput() { console.write("enter make of vehicle: "); make = console.readline(); console.write("enter model of vehicle: "); model = console.readline(); console.writeline("enter year of manufacturing vehicle: "); year = int.parse(console.readline()); } public override string tostring() { return string.format("vehicle {0} , of model {1} , made in {2}.", make, model, year); } }
car.cs
using system; class car : vehicle { string bodytype; public string bodytype { { return bodytype; } set { bodytype = value; } } public car() : base() { bodytype = "unknown"; } public car(string make, string model, int year, string bodytype) : base(make, model, year) { this.bodytype = bodytype; } public override void getfrominput() { base.getfrominput(); console.write("enter body type car: "); bodytype = console.readline(); } public override string tostring() { return base.tostring() + string.format("this vehicle car body type of {0}.", bodytype); } }
airplane.cs
using system; class airplane : vehicle { int noengines; string enginetype; public int numberofengines{ { return noengines; } set { noengines = value; } } public string enginetype { { return enginetype; } set { enginetype = value; } } public airplane() : base() { noengines = 0; enginetype = "unknown"; } public airplane(string make, string model, int year, int noengines, string enginetype) : base(make, model, year) { this.noengines = noengines; this.enginetype = enginetype; } public override void getfrominput() { base.getfrominput(); console.write("enter number of engines on airplane: "); numberofengines = int.parse(console.readline()); console.write("enter engine type airplane: "); enginetype = console.readline(); } public override string tostring() { return base.tostring() + string.format("this vehicle airplane {0} engines , engine type of {1}.", numberofengines, enginetype); } }
boat.cs
using system; class boat : vehicle { int length; string hulltype; public int length { { return length; } set { length = value; } } public string hulltype { { return hulltype; } set { hulltype = value; } } public boat() : base() { length = 0; hulltype = "unknown"; } public boat(string make, string model, int year, int length, string hulltype) : base(make, model, year) { this.length = length; this.hulltype = hulltype; } public override void getfrominput() { base.getfrominput(); console.write("enter length of boat: "); length = int.parse(console.readline()); console.write("enter hull type boat: "); hulltype = console.readline(); } public override string tostring() { return base.tostring() + string.format("this vehicle boat length of {0} , hull type of {1}.", length, hulltype); } }
fleet.cs
using system; class fleet { static void main(string[] args) { vehicle v1 = new vehicle(); v1.getfrominput(); console.writeline(v1); //... other vehicles } }
Comments
Post a Comment