Sfida: Costruttori
Il programma seguente contiene due struct: una si chiama Point
e definisce una coordinata, poiché possiede un attributo/campo x
e uno y
. L'altra struct si chiama Triangle
ed è composta da 3 oggetti point. Nel costruttore si passano tre oggetti point per inizializzare correttamente un triangolo.
Completa gli spazi vuoti per completare i costruttori di entrambe le struct.
È anche un buon esercizio di lettura del codice leggere tutto il codice e cercare di comprenderlo, tuttavia non è necessario per risolvere questo compito.
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()); } }
Un costruttore è simile a un metodo, tuttavia non ha alcun valore di ritorno; inoltre, utilizzare la parola chiave public
prima della sua definizione.
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()); } }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
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
Sfida: Costruttori
Scorri per mostrare il menu
Il programma seguente contiene due struct: una si chiama Point
e definisce una coordinata, poiché possiede un attributo/campo x
e uno y
. L'altra struct si chiama Triangle
ed è composta da 3 oggetti point. Nel costruttore si passano tre oggetti point per inizializzare correttamente un triangolo.
Completa gli spazi vuoti per completare i costruttori di entrambe le struct.
È anche un buon esercizio di lettura del codice leggere tutto il codice e cercare di comprenderlo, tuttavia non è necessario per risolvere questo compito.
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()); } }
Un costruttore è simile a un metodo, tuttavia non ha alcun valore di ritorno; inoltre, utilizzare la parola chiave public
prima della sua definizione.
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()); } }
Grazie per i tuoi commenti!