Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have code written, but for some reason, when I want my child class to overwrit

ID: 3627776 • Letter: I

Question

I have code written, but for some reason, when I want my child class to overwrite the parent class, it is not actually overwriting the parent class for example, the FighterJet class is supposed to overwrite the values in the Airplane class, but when I try to execute these on my form, it keeps the values from the airplane class. Can someone review and point me in the error of my ways:

public partial class FrmAirplane : Form
{
public FrmAirplane()
{
InitializeComponent();
}

Airplane airplane;


private void btnCreate_Click(object sender, EventArgs e)
{

try
{

string name = txtName.Text;
int x = Convert.ToInt32(txtX.Text);
int y = Convert.ToInt32(txtY.Text);
double speed = Convert.ToDouble(txtSpeed.Text);
int direction = Convert.ToInt32(txtDirection.Text);
Position p = new Position(x, y, speed, direction);
airplane = new Airplane(name, p);
txtNumCreated.Text = airplane.GetNumberCreated().ToString();
lblMessage.Text = airplane.ToString();
}
catch
{
MessageBox.Show("Invalid data entered", "Error");
}

}

private void btnMove_Click(object sender, EventArgs e)
{
// calling Airplane class method Move() using airplane object
airplane.Move();
lblMessage.Text = airplane.ToString();
}

private void btnAccelerate_Click(object sender, EventArgs e)
{
// calling Airplane class method Accelerate() using airplane object
airplane.Accelerate();
lblMessage.Text = airplane.ToString();
}

private void btnDecelerate_Click(object sender, EventArgs e)
{
// calling Airplane class method Decelerate() using airplane object
airplane.Decelerate();
lblMessage.Text = airplane.ToString();
}

private void btnTurnLeft_Click(object sender, EventArgs e)
{
// calling Airplane class method TurnLeft() using airplane object
airplane.TurnLeft();
lblMessage.Text = airplane.ToString();
}

private void btnTurnRight_Click(object sender, EventArgs e)
{
// calling Airplane class method TurnRight() using airplane object
airplane.TurnRight();
lblMessage.Text = airplane.ToString();
}

private void btnClear_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtX.Text = "";
txtY.Text = "";
txtSpeed.Text = "";
txtDirection.Text = "";
cboAirplaneType.Text = "";

//Message box to display once fields have been cleared
System.Windows.Forms.MessageBox.Show("All fields on the form have now been updated to a null value.");
}

private void btnExit_Click(object sender, EventArgs e)
{
MessageBox.Show("Thank you. Click OK to Terminate",
"Exit Application",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
Application.Exit();
}

private void FrmAirplane_Load(object sender, EventArgs e)
{
//Calling the Display Application Information from the utilities class
Utilities.DisplayApplicationInformation();

//Adding the following time zones to the combo box time zone
cboAirplaneType.Items.Add("Generic Airplane");
cboAirplaneType.Items.Add("Passenger Airplane");
cboAirplaneType.Items.Add("Fighter Plane");
}

private void lblMessage_Click(object sender, EventArgs e)
{

}

private void btnBoardAirplane_Click(object sender, EventArgs e)
{
PassengerPlane.BoardPlane();
}

private void cboAirplaneType_SelectedIndexChanged(object sender, EventArgs e)
{

}

}
}

public class Airplane
{
//Declaring Variables
public string name;
public Position planePosition;
public static int numberCreated = 0;
public int MAX_SPEED = 500;

public Airplane()
{
}

public Airplane(string name, Position p)
{
this.name = name;
planePosition = new Position(p.X, p.Y, p.Speed, p.Direction);
numberCreated = numberCreated + 1;
}

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

public Position PlanePosition
{
get
{
return planePosition;
}
set
{
planePosition = value;
}
}
// return the total number of airplanes created using the class as the blueprint
public int GetNumberCreated()
{
// returns the number of airplanes created
return numberCreated + 1;
}
// overridden ToString() method
public override string ToString()
{
return "Name: " + Name + System.Environment.NewLine + PlanePosition.ToString();
}

// Procedure moves the positioning of the airplane by updating the value of X and Y
public virtual void Move()
{
// convert degrees to radians since C# uses radians for Sine and Cosine functions
double radians = planePosition.Direction * Math.PI / 180.0;
// change the x location by the x vector of the speed
planePosition.X += (int)(planePosition.Speed * Math.Cos(radians));
// change the y location by the y vector of the speed
planePosition.Y -= (int)(planePosition.Speed * Math.Sin(radians));
}
// procedure will cause the plane to "turn right"
public virtual void TurnRight()
{
if (planePosition.Direction > 0)
// or direction -= 1;
planePosition.Direction -= 1;
else
// reset direction to fit within 0 - 359 degrees
planePosition.Direction = 359;
}
// Procedure will cause the plane to "turn left"
public virtual void TurnLeft()
{
if (planePosition.Direction < 359)
// or direction += 1;
planePosition.Direction += 1;
else
// reset direction to fit within 0 - 359 degrees
planePosition.Direction = 0;
}
// Procedure will update the speed of the plane
public virtual void Accelerate()
{
if (planePosition.Speed < MAX_SPEED)
// or speed += 1;
planePosition.Speed += 1;
}
// Procedure will decrease the speed of the plane
public virtual void Decelerate()
{
if (planePosition.Speed > 0)
// or speed -= 1;
planePosition.Speed -= 1;
}

}
}

public class Position
{
//Declaring Variables
public int x;
public int y;
public double speed;
public int direction;

public Position(int x, int y, double speed, int direction)
{
this.x = x;
this.y = y;
this.speed = speed;
this.direction = direction;
}

public int X
{
get
{
return x;
}
set
{
x = value;
}
}

public int Y
{
get
{
return y;
}
set
{
y = value;
}
}

public int Direction
{
get
{
return direction;
}
set
{
direction = value;
}
}

public double Speed
{
get
{
return speed;
}
set
{
speed = value;
}
}
// overridden ToString() methode
public override string ToString()
{
return " Position: X: " + X + " Y: " + Y + " Speed: " + Speed + " Direction: " + Direction;
}
}
}


//Declaring Class and showing inheritance
class PassengerPlane : Airplane
{
//Declaring Variables
string airlineName;
int numberPassengers, flightNumber;

//Creating Module PassengerPlane
public PassengerPlane(string name, Position pos, int flight, int numPass)

{

this.airlineName = name;

this.flightNumber = flight;

this.numberPassengers = numPass;

this.Name = name;

this.PlanePosition = new Position(pos.X, pos.Y, pos.Speed, pos.Direction);

}

//Creating Module Board Plane
public static void BoardPlane()
{
//Creating Message box to show text
System.Windows.Forms.MessageBox.Show("The plane has now been boarded");
}

//over riding the data from parent module ToString
public override string ToString()
{
return base.ToString() + " : " + airlineName + " : " + flightNumber.ToString() + " : " + numberPassengers.ToString();
}

//over riding the data from parent module Turn Right
public override void TurnRight()
{
base.TurnRight();
}

//over riding the data from parent module Turn Left
public override void TurnLeft()
{
base.TurnLeft();
}

//over riding the data from parent module Accelerate
public override void Accelerate()

{

base.Accelerate();

}

//over riding the data from parent module Decelerate
public override void Decelerate()

{

if (PlanePosition.Speed > 0)
if (PlanePosition.Speed - 10 < 0)
PlanePosition.Speed = 0;
else
PlanePosition.Speed -= 10;
}
}
}

class FighterJet : Airplane
{
string fighterType;

public FighterJet(string name, Position pos, string type)
{
this.fighterType = type;
this.Name = name;
this.PlanePosition = new Position(pos.X, pos.Y, pos.Speed, pos.Direction);
}

//over riding the data from parent module ToString
public override string ToString()
{
return base.ToString() + " : " + fighterType;
}

//over riding the data from parent module Turn Right
public override void TurnRight()

{

if (PlanePosition.Direction < 359)

PlanePosition.Direction -= 20;

else

PlanePosition.Direction = 0;

}

//over riding the data from parent module Turn Left
public override void TurnLeft()

{

if (PlanePosition.Direction < 359)

PlanePosition.Direction += 20;

else

PlanePosition.Direction = 0;

}

//over riding the data from parent module Accelerate
public override void Accelerate()

{

if (PlanePosition.Speed < 2000)

if (PlanePosition.Speed + 100 < 2000)

PlanePosition.Speed += 100;

}

//over riding the data from parent module Decelerate
public override void Decelerate()

{

if (PlanePosition.Speed > 0)

if (PlanePosition.Speed - 100 < 0)

PlanePosition.Speed = 0;

else

PlanePosition.Speed -= 100;

}

}

}

Explanation / Answer

this is an example for you to follow for overriding methods /////// airplane ///////// using System.Collections.Generic; using System.Linq; using System.Text; namespace OverrideTest { class Airplane { public Airplane() { } public virtual string ToString() { return "This is the Airplane class!"; } } } ///// jet class /////// using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OverrideTest { class Jet : Airplane { public Jet() { } public override string ToString() { return base.ToString() + " now we return the base class and the Jet class"; } } } // and a simple test application///// using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OverrideTest { class Program { static void Main(string[] args) { Airplane a = new Airplane(); Jet j = new Jet(); Console.WriteLine("Airplane {0}", a.ToString()); Console.WriteLine("Jet {0}", j.ToString()); Console.ReadLine(); } } } I hope this can help you! as you can see your base calss (airplane) needs to have a virtual method. then you can override in the other method

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote