Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Desafio: Construtores | Structs e Enumeradores
C# Além do Básico

bookDesafio: Construtores

O programa a seguir possui duas structs, uma delas chamada Point, que define uma coordenada pois possui os atributos/campos x e y. A outra struct é chamada Triangle e é composta por 3 objetos do tipo point. No construtor, você passa três objetos point para inicializar corretamente um triângulo.

Preencha as lacunas para completar os construtores de ambas as structs.

Também é um bom exercício de leitura de código analisar todo o código e tentar compreendê-lo, porém isso não é necessário para resolver esta tarefa.

index.cs

index.cs

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

Um construtor é semelhante a um método, porém não possui valor de retorno e, além disso, utiliza a palavra-chave public antes de sua definição.

index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
using 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()); } }
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 9

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 2.04

bookDesafio: Construtores

Deslize para mostrar o menu

O programa a seguir possui duas structs, uma delas chamada Point, que define uma coordenada pois possui os atributos/campos x e y. A outra struct é chamada Triangle e é composta por 3 objetos do tipo point. No construtor, você passa três objetos point para inicializar corretamente um triângulo.

Preencha as lacunas para completar os construtores de ambas as structs.

Também é um bom exercício de leitura de código analisar todo o código e tentar compreendê-lo, porém isso não é necessário para resolver esta tarefa.

index.cs

index.cs

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

Um construtor é semelhante a um método, porém não possui valor de retorno e, além disso, utiliza a palavra-chave public antes de sua definição.

index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
using 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()); } }
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 9
some-alt