Guest User

Guest User

  • Tech Writer
  • 270
  • 2k

Help to Solve the Problems

May 16 2022 7:32 PM

I need Help to solve 2 Problems in my Code and maybe a Explanation where the code has to go and why:

3rd task:
For this submission task, create a new project "ESA_Project" into which you copy the latest version of the aircraft project.
Complete the project with programming that allows the position data of a flight to be logged in a file.

  •  The logging file should be written to the program's executable folder (“Release” or “Debug”)
  •  The name of each log file should follow the pattern _---.bin

Example: LH 3040_date-00-00-00.bin
be put together. This can be generated programmatically and reliably differentiates between several log files with this time-dependent coding.
A string containing the following information should first be written to the file:
Flight "" (type "") departs from position "--" with target position "--".
Example: LH 3040 TYP starts at position pos.x-pos.y-pos.h with target position x-y-h
This is the header of the log file, which is then followed by the data. Again, all expressions with angle brackets are to be replaced
by concrete values from the programming.

  • The x, y and h data of the current position are then to be written continuously to the file as int values ?? until the flight is complete.

These are, so to speak, the data of the file.

  • Logging should be made dependent on the value of a "switch" that you define as a static variable in the Program class: public static bool log = true;

If this switch is set to true, logging will occur, if set to false, logging will not occur.
This task is not only about the technical realization of the aforementioned requirements, but above all about fundamental considerations of object-oriented programming.

For example, when you declare the necessary writer, you should make it clear that a writer corresponds to a flight recorder and check which type of pilot can use a flight recorder at all. There were examples of such considerations of object-oriented planning in this booklet, but also during the planning of the project in study booklet CSH01B - e.g. on the location of the transponder.
Based on these considerations, the following questions need to be answered:


a) Where do you declare the Writer object so that it is known in the program where it is needed?
b) At what point in the program do you assemble the file path, create the writer, and write the header?
d) At what point in the program do you close the writer?
c) Where in the program do you write the data of the x, y and h values?
e) How do you ensure that these operations only take place if the "switch" in the program class is set to true?


Enter the program code in each case and briefly explain your programming.

A notice:
BinaryWriter knows a method public virtual void Write ( string value ) ... which not only writes the string to be specified as a parameter to the file, but also saves information about the length of the string beforehand (compare the .NET documentation).
BinaryReader knows a mirror image method public virtual string ReadString () ... which first evaluates the length information stored in the binary file and then precisely reads the length of the following string so that the read pointer for further read operations is set exactly to the following byte.

4. Task:
Add a method to the FliegerProject program class (for submission task 3).
public void ESA4Out(string logpath)
This method is intended to read back the binary file of a flight, which is created with the programming in ESA 3, and output its content
to the console. The result could be for two flights as in Calls to this method "ESA4Out" at the end of the program control in "Program clocks".
In order for the ESA4Out method to always output the last logged flight, store the path to its log file in a new property that is simply
furnished with a get accessor for retrieval. This property is created where the logging takes place.
When programming "ESA4Out", the internal file structure of the log file must be observed so that the header and then the flight data are output in the console. The flight data starts with the start position and ends with the reached destination position. Because of the rough clocking and the resulting rough position calculations, the specified target position and the target position actually reached
will deviate from one another.
Explain the main steps of your solution with code snippets.

Here two files that needed in Debug of programm to read some Parameters to read this File name it LH 500.init and LH 3000.init it is a simple txt file:

LH 3000.init:

zielPos.x=1000
zielPos.y=500
zielPos.h=200
streckeProTakt=160
flughoehe=350
steighoeheProTakt=25
sinkhoeheProTakt=15
typ=4
sitzplaetze=290

LH 500.init:

zielPos.x=1000
zielPos.y=500
zielPos.h=200
streckeProTakt=200
flughoehe=300
steighoeheProTakt=20
sinkhoeheProTakt=10
typ=3
sitzplaetze=250

and here my Code, I will learn and Understand why i must Code in what :

using System;
using System.Threading;
using System.IO;

namespace CSH03ESA_Aufgabe3
{
    delegate void TransponderDel(string kennung, Position pos);
    delegate void FliegerRegisterDel();
    interface ITransponder
    {
        void Transpond(string kennung, Position pos);
    }
    public enum Airbus : short { A300, A310, A318, A319, A320, A321, A330, A340, A350, A380 }
    public struct Position
    {
        public int x, y, h;
        public Position(int x, int y, int h)
        {
            this.x = x;
            this.y = y;
            this.h = h;
        }
        public void PositionÄndern(int deltaX, int deltaY, int deltaH)
        {
            x = x + deltaX;
            y = y + deltaY;
            h = h + deltaH;
        }
        public void HöheÄndern(int deltaH)
        {
            h = h + deltaH;
        }
    }

    abstract class Luftfahrzeug
    {
        protected string kennung;
        public string Kennung
        {
            protected set { kennung = value; }
            get { return kennung; }
        }

        protected Position pos;
        public abstract void Steigen(int meter);
        public abstract void Sinken(int meter);
    }

    class Flugzeug : Luftfahrzeug
    {
        protected Position zielPos;
        protected int streckeProTakt;
        protected int flughöhe;
        protected int steighöheProTakt;
        protected int sinkhöheProTakt;
        protected bool steigt = false;
        protected bool sinkt = false;

        public void Starte(Position zielPos, int streckeProTakt, int flughöhe, int steighöheProTakt, int sinkhöheProTakt)
        {
            this.zielPos = zielPos;
            this.streckeProTakt = streckeProTakt;
            this.flughöhe = flughöhe;
            this.steighöheProTakt = steighöheProTakt;
            this.sinkhöheProTakt = sinkhöheProTakt;
            this.steigt = true;
        }
        public Flugzeug(string kennung, Position startPos)
        {
            this.kennung = kennung;
            this.pos = startPos;
        }

        public override void Steigen(int meter)
        {
            pos.HöheÄndern(meter);
            Console.WriteLine(kennung + "steigt " + meter + "Meter, Höhe =" + pos.h);
        }
        public override void Sinken(int meter)
        {
            pos.HöheÄndern(-meter);
            Console.WriteLine(kennung + "sinkt " + meter + "Meter, Höhe =" + pos.h);
        }
    }

    class Starrflügelflugzeug : Flugzeug, ITransponder
    {
        public Starrflügelflugzeug(string kennung, Position startPos) : base(kennung, startPos)
        {
        }

        public void Transpond(string kennung, Position pos)
        {
            double abstand = Math.Sqrt(Math.Pow(this.pos.x - pos.x, 2) + Math.Pow(this.pos.y - pos.y, 2));

            DateTime timestamp = DateTime.Now;
            //if (kennung.Equals(this.kennung))
            int[] Byte = { pos.x, pos.y, pos.h };
            if(kennung.Equals(this.kennung))
            {
                Console.Write("{0}:{1}:{2}", timestamp.Hour, timestamp.Minute, timestamp.Second);
                Console.Write("\t{0}-Position: {1}-{2}-{3}", this.kennung, base.pos.x, base.pos.y, base.pos.h);
                Console.Write(" Zieldistanz: {0} m\n", Zieldistanz()); 
            }
            else
            {
                Console.Write("\t{0} ist {1} m von {2} entfernt.\n", this.kennung, (int)abstand, kennung);
                if (Math.Abs(this.pos.h - pos.h) < 100 && abstand < 500)
                    Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\tWARNUNG: {0} hat nur {1} m Höhenabstand!", kennung, Math.Abs(this.pos.h - pos.h));
                Console.ResetColor();
            }
        }

        double a, b, alpha, a1, b1;
        bool gelandet = false;

        private bool SinkenEinleiten()
        {
            double strecke = Math.Sqrt(Math.Pow(streckeProTakt, 2) - Math.Pow(sinkhöheProTakt, 2));
            int sinkstrecke = (int)(strecke * (pos.h - zielPos.h) / sinkhöheProTakt);
            int zieldistanz = Zieldistanz();
            if (sinkstrecke >= zieldistanz)
            {
                //optinale Consolenausgabe zur Kontrolle
                Console.WriteLine("{0} Sinkstrecke {1} >= Zieldistanz {2}", kennung, sinkstrecke, zieldistanz);
                return true;
            }
            else
                return false;
        }

        private void PositionBerechnen(double strecke, int steighöheProTakt)
        {
            // modifizierte Übernahme der bisherigen Berechungen aus "Steuern"
            a = zielPos.x - pos.x;
            b = zielPos.y - pos.y;
            alpha = Math.Atan2(b, a);
            a1 = Math.Cos(alpha) * strecke;
            b1 = Math.Sin(alpha) * strecke;
            pos.PositionÄndern((int)a1, (int)b1, steighöheProTakt);
        }

        private int Zieldistanz()
        {
            return (int)Math.Sqrt(Math.Pow(zielPos.x - pos.x, 2) + Math.Pow(zielPos.y - pos.y, 2));
        }

        public void Steuern()
        {
            if (steigt)
            {
                if (this.SinkenEinleiten())
                {
                    steigt = false;
                    sinkt = true;
                }
                else if (pos.h > flughöhe)
                {
                    steigt = false;
                }
            }
            else if (sinkt)
            {
                if (pos.h <= zielPos.h + sinkhöheProTakt)
                    gelandet = true;
            }
            else
            {
                //Horizontalflug
                if (this.SinkenEinleiten())
                {
                    sinkt = true;
                }
            }
            if (!gelandet)
            {
                // Zunächst die aktuelle Position ausgeben:
                Program.transponder(kennung, pos);
                //"Strecke" (am Boden) berechnen:
                if (steigt)
                {
                    double strecke = Math.Sqrt(Math.Pow(streckeProTakt, 2) - Math.Pow(steighöheProTakt, 2));
                    this.PositionBerechnen(strecke, steighöheProTakt);
                }
                else if (sinkt)
                {
                    double strecke = Math.Sqrt(Math.Pow(streckeProTakt, 2) - Math.Pow(sinkhöheProTakt, 2));
                    this.PositionBerechnen(strecke, -sinkhöheProTakt);
                }
                else
                {
                    // im Horizontalflug ist "strecke" gleich "streckeProTakt"
                    this.PositionBerechnen(streckeProTakt, 0);
                }
            }
            else
            {
                // Flieger derigistrieren, Transponder abschalten, Abschlussmeldung
                Program.fliegerRegister -= this.Steuern;
                Program.transponder -= this.Transpond;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\n{0} gelandet (Zieldistanz = {1}, Höhendistanz = {2})", kennung, Zieldistanz(), pos.h - zielPos.h);
                Console.ResetColor();
            }

        }
    }

    class Düsenflugzeug : Starrflügelflugzeug
    {
        protected Airbus typ;

        private int sitzplätze;
        private int fluggäste;
        public int Fluggäste
        {
            set
            {
                if (sitzplätze < (fluggäste + value))
                    Console.WriteLine("Keine Buchung: Die " + "Fluggastzahl würde mit der Zubuchung " + "von {0} Plätzen die verfügbaren Plätze "
                        + "von {1} um {2} übersteigen!", value, sitzplätze, value + fluggäste - sitzplätze);
                else
                    fluggäste += value;
            }
            get { return fluggäste; }
        }
        public Düsenflugzeug(string kennung, Position startPos) : base(kennung, startPos)
        {
            bool initialized = this.Starte();
            if (initialized)
            {
                Program.transponder += this.Transpond;
                Program.fliegerRegister += this.Steuern;
            }
        }
        public bool Starte()
        {
            string pfad = @".\" + kennung + ".init";
            StreamReader reader;
            try
            {
                reader = new StreamReader(File.Open(pfad, FileMode.Open));
            }
            catch (IOException e)
            {
                Console.WriteLine("{0} Fehler beim Zugriff auf die Datei " + pfad, e.GetType().Name);
                return false;
            }
            int[] data = new int[9];
            for (int i = 0; i < 9; i++)
            {
                string str = reader.ReadLine();
                str = str.Substring(str.IndexOf('=') + 1);
                //Zur Kontrolle, später auskommentieren
                //Console.WriteLine(str);
                data[i] = Int32.Parse(str);
            }
            reader.Close();
            this.zielPos.x = data[0];
            this.zielPos.y = data[1];
            this.zielPos.h = data[2];
            streckeProTakt = data[3];
            flughöhe = data[4];
            steighöheProTakt = data[5];
            sinkhöheProTakt = data[6];

            //"typ" aus data[7] initialisieren
            Array typen = Enum.GetValues(typeof(Airbus));
            this.typ = (Airbus)typen.GetValue(data[7]);
            sitzplätze = data[8];
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("Flug {0} vom Typ {1} mit {2} Plätzen initialisiert.", kennung, typ, sitzplätze);
            Console.ResetColor();
            steigt = true;
            Console.WriteLine();
            return true;
        }
        public void Buchen(int plätze)
        {
            Fluggäste += plätze;
        }
    }

    class Program
    {
        public static TransponderDel transponder;

        public delegate void FliegerRegisterDel();
       
        public static FliegerRegisterDel fliegerRegister;

        public void ProgrammTakten()
        {
            Düsenflugzeug flieger1 = new Düsenflugzeug("LH 500", new Position(3500, 1400, 180));
            Düsenflugzeug flieger2 = new Düsenflugzeug("LH 3000", new Position(3000, 2000, 100));

            while (fliegerRegister != null)
            {
                fliegerRegister();
                Console.WriteLine();
                Thread.Sleep(1000);
            }
        }

        static void Main(string[] args)
        {
            Program test = new Program();
            test.ProgrammTakten();
            Console.ReadLine();
        }
    }
}