Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Desafío: Constructores | Structs y Enumeradores
C# Más Allá de lo Básico

bookDesafío: Constructores

El siguiente programa tiene dos estructuras, una de ellas se llama Point, la cual define una coordenada ya que tiene un atributo/campo x y otro y. La otra estructura se llama Triangle y está compuesta por 3 objetos de tipo point. En el constructor se pasan tres objetos point para inicializar correctamente un triángulo.

Completa los espacios en blanco para finalizar los constructores de ambas estructuras.

También será un buen ejercicio de lectura de código leer todo el código e intentar comprenderlo, sin embargo, no es necesario para resolver esta tarea.

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

Un constructor es similar a un método, sin embargo, no tiene ningún valor de retorno; además, utiliza la palabra clave public antes de su definición.

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()); } }
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 9

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you show me the code with the blanks that need to be filled?

What should the constructors for the `Point` and `Triangle` structs look like?

Can you explain how to use these structs once they are constructed?

Awesome!

Completion rate improved to 2.04

bookDesafío: Constructores

Desliza para mostrar el menú

El siguiente programa tiene dos estructuras, una de ellas se llama Point, la cual define una coordenada ya que tiene un atributo/campo x y otro y. La otra estructura se llama Triangle y está compuesta por 3 objetos de tipo point. En el constructor se pasan tres objetos point para inicializar correctamente un triángulo.

Completa los espacios en blanco para finalizar los constructores de ambas estructuras.

También será un buen ejercicio de lectura de código leer todo el código e intentar comprenderlo, sin embargo, no es necesario para resolver esta tarea.

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

Un constructor es similar a un método, sin embargo, no tiene ningún valor de retorno; además, utiliza la palabra clave public antes de su definición.

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()); } }
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 9
some-alt