Utfordring: Konstruktører
Følgende program har to strukturer, hvorav den ene heter Point
og definerer en koordinat med et x
- og et y
-attributt / felt. Den andre strukturen heter Triangle
og består av tre punkt-objekter. I konstruktøren sender du inn tre punkt-objekter for å initialisere en trekant korrekt.
Fyll inn de tomme feltene for å fullføre konstruktørene til begge strukturene.
Det kan også være en god øvelse i kodeforståelse å lese gjennom hele koden og prøve å forstå den, men det er ikke nødvendig for å løse denne oppgaven.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354using System; class Program { struct Point { public double x; public double y; ___ { ___ = x; ___ = y; } } struct Triangle { public Point[] vertices; ___ { ___ = new Point[] { a, b, c }; } // Calculates and returns the area of the triangle based on the vertices. public double getArea() { // Storing the values in shorter variables for ease of use and code readability. double x1 = this.vertices[0].x; double x2 = this.vertices[1].x; double x3 = this.vertices[2].x; double y1 = this.vertices[0].y; double y2 = this.vertices[1].y; double y3 = this.vertices[2].y; // Calculating and returning the area of the triangle. // This formula is easy available on the internet, you don't need to understand it. return (0.5) * ( x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) ); } } static void Main(string[] args) { Point p1 = new Point(47, 17); Point p2 = new Point(9, 50); Point p3 = new Point(7, 14); Triangle tri = new Triangle(p1, p2, p3); Console.WriteLine(tri.getArea()); } }
En konstruktør ligner på en metode, men har ingen returverdi. Bruk dessuten nøkkelordet public
før definisjonen.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354using System; class Program { struct Point { public double x; public double y; public Point(double x, double y) { this.x = x; this.y = y; } } struct Triangle { public Point[] vertices; public Triangle(Point a, Point b, Point c) { this.vertices = new Point[] { a, b, c }; } // Calculates and returns the area of the triangle based on the vertices. public double getArea() { // Storing the values in shorter variables for ease of use and code readability. double x1 = this.vertices[0].x; double x2 = this.vertices[1].x; double x3 = this.vertices[2].x; double y1 = this.vertices[0].y; double y2 = this.vertices[1].y; double y3 = this.vertices[2].y; // Calculating and returning the area of the triangle. // This formula is easy available on the internet, you don't need to understand it. return (0.5) * ( x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) ); } } static void Main(string[] args) { Point p1 = new Point(47, 17); Point p2 = new Point(9, 50); Point p3 = new Point(7, 14); Triangle tri = new Triangle(p1, p2, p3); Console.WriteLine(tri.getArea()); } }
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 2.04
Utfordring: Konstruktører
Sveip for å vise menyen
Følgende program har to strukturer, hvorav den ene heter Point
og definerer en koordinat med et x
- og et y
-attributt / felt. Den andre strukturen heter Triangle
og består av tre punkt-objekter. I konstruktøren sender du inn tre punkt-objekter for å initialisere en trekant korrekt.
Fyll inn de tomme feltene for å fullføre konstruktørene til begge strukturene.
Det kan også være en god øvelse i kodeforståelse å lese gjennom hele koden og prøve å forstå den, men det er ikke nødvendig for å løse denne oppgaven.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354using System; class Program { struct Point { public double x; public double y; ___ { ___ = x; ___ = y; } } struct Triangle { public Point[] vertices; ___ { ___ = new Point[] { a, b, c }; } // Calculates and returns the area of the triangle based on the vertices. public double getArea() { // Storing the values in shorter variables for ease of use and code readability. double x1 = this.vertices[0].x; double x2 = this.vertices[1].x; double x3 = this.vertices[2].x; double y1 = this.vertices[0].y; double y2 = this.vertices[1].y; double y3 = this.vertices[2].y; // Calculating and returning the area of the triangle. // This formula is easy available on the internet, you don't need to understand it. return (0.5) * ( x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) ); } } static void Main(string[] args) { Point p1 = new Point(47, 17); Point p2 = new Point(9, 50); Point p3 = new Point(7, 14); Triangle tri = new Triangle(p1, p2, p3); Console.WriteLine(tri.getArea()); } }
En konstruktør ligner på en metode, men har ingen returverdi. Bruk dessuten nøkkelordet public
før definisjonen.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354using System; class Program { struct Point { public double x; public double y; public Point(double x, double y) { this.x = x; this.y = y; } } struct Triangle { public Point[] vertices; public Triangle(Point a, Point b, Point c) { this.vertices = new Point[] { a, b, c }; } // Calculates and returns the area of the triangle based on the vertices. public double getArea() { // Storing the values in shorter variables for ease of use and code readability. double x1 = this.vertices[0].x; double x2 = this.vertices[1].x; double x3 = this.vertices[2].x; double y1 = this.vertices[0].y; double y2 = this.vertices[1].y; double y3 = this.vertices[2].y; // Calculating and returning the area of the triangle. // This formula is easy available on the internet, you don't need to understand it. return (0.5) * ( x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) ); } } static void Main(string[] args) { Point p1 = new Point(47, 17); Point p2 = new Point(9, 50); Point p3 = new Point(7, 14); Triangle tri = new Triangle(p1, p2, p3); Console.WriteLine(tri.getArea()); } }
Takk for tilbakemeldingene dine!