Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Practicing Constructors | Structs & Enumerators
C# Beyond Basics

bookPracticing Constructors

The following program has two structs, one of them is called Point which defines some coordinate as it has an x and a y attribute / field. The other struct is called Triangle and it is made up of 3 point objects. In the constructor we pass three point objects to correctly initialize a triangle.

Fill in the blanks to complete the constructors of both the structs.

It will also be a good code reading exercise to read the whole code and try to understand it, however it is not necessary to solve this task.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
using 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()); } }

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 9

Spør AI

expand

Spør AI

ChatGPT

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

bookPracticing Constructors

Sveip for å vise menyen

The following program has two structs, one of them is called Point which defines some coordinate as it has an x and a y attribute / field. The other struct is called Triangle and it is made up of 3 point objects. In the constructor we pass three point objects to correctly initialize a triangle.

Fill in the blanks to complete the constructors of both the structs.

It will also be a good code reading exercise to read the whole code and try to understand it, however it is not necessary to solve this task.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
using 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()); } }

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 9
some-alt