0

C#Help to understand the solution to the problem.

Destructors Five participants have advanced to the final round of a TV trivia show, and it is time to announce the winner and end the game. The program you are given defines the array of finalists, takes the index of the winner as input, and creates the FinalRound object. Complete the FinalRound class by writing the needed code in constructor to take the array and the winner index as parameters and output the corresponding message to show the winner. Then create a destructor to finish the game and output "Game Over". Sample Input 2 Sample Output Winner is Leyla Brown Game Over class Program { static void Main(string[] args) { string[] finalists = { "James Van", "John Smith", "Leyla Brown", "Tom Homerton", "Bob Douglas" }; /*int winner = Convert.ToInt32(Console.ReadLine());*/ string winner = Console.ReadLine(); //выведите победителя и фразы "Game Over" FinalRound finalRound = new FinalRound(finalists, winner); Console.WriteLine("Game over"); } } class FinalRound { public FinalRound(string[] finalists, string winner) { foreach (string n in finalists) { if (n.Contains(winner)) { Console.WriteLine("Winner is : "+ n); } } //завершите конструктор } //создайте деструктор => "Game Over" ~FinalRound() { Console.WriteLine("Game over"); } }

27th Apr 2021, 7:03 PM
Артём Попов
8 Respostas
+ 3
If the constructor takes in {finalists} as an array of names and {winner} as an integer value to indicate the index, then you can retrieve the value using: finalists[winner] NOTE: The value 2 reflects the 3rd position in the array using 0 based indexes. Therefore, the output is correct. But... why is this class modeled with these requirements? Is this something being taught in school? If so... I'm seriously worried about the next generation of CS graduates.
28th Apr 2021, 4:36 AM
David Carroll
David Carroll - avatar
27th Apr 2021, 10:03 PM
TOLUENE
TOLUENE - avatar
+ 2
Артём Попов Many of the concepts aren't going to click until you've seen them a number of times and applied them in practice on actual projects. Until then, I suppose you'll come across overly simplified examples like this that try to demonstrate the sequence of timing as to when the constructor and finalizer (or destructor) will execute using contrived scenarios that would never exist in real world implementations. Maybe I'm being overly critical of these examples that attempt to keep things simple and interesting. But, examples like this, in my opinion, can cause confusion and lead people to think this is a good use case for a class, constructor, and destructor. Just keep a critical mind to differentiate examples designed to give you simplified practice on complex concepts. While that may seem difficult as a learner, it will help you develop the intuition for designing and writing code that will serve you well throughout your career.
28th Apr 2021, 2:01 PM
David Carroll
David Carroll - avatar
+ 2
Why is it so difficult, why iterate over the array. You don't need to search here. You only need to display information. using System; namespace Decampilator { class Program { class Test { public Test (string [] ar, int x) { Console.WriteLine("Winner is " + ar[x]); } ~Test () { Console.WriteLine("Game Over"); } } static void Main(string[] args) { int x = Convert.ToInt32(Console.ReadLine()); string[] ar = { "James Van", "John Smith", "Leyla Brown", "Tom Homerton", "Bob Douglas" }; Test p = new Test( ar, x); } } }
22nd Aug 2021, 8:37 AM
Пётр Попов
Пётр Попов - avatar
+ 1
Thanks for the answer. Honestly, sometimes I do not understand the principle of choosing problems for a particular topic. More often you have to separately look for more materials or ask for help from specialists. I'm not at all sure that this program fully covers the entire body of knowledge.
28th Apr 2021, 7:26 AM
Артём Попов
+ 1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string[] finalists = { "James Van", "John Smith", "Leyla Brown", "Tom Homerton", "Bob Douglas" }; int winner = Convert.ToInt32(Console.ReadLine()); //this should show the winner and "Game Over" FinalRound finalRound = new FinalRound(finalists, winner); } } class FinalRound { public FinalRound(string[] finalists, int winner) { Console.WriteLine("Winner is " + finalists[winner]); } //create destructor => "Game Over" ~FinalRound(){ Console.Write("Game Over"); } } }
23rd Mar 2024, 12:36 PM
Mahima Rathore
Mahima Rathore - avatar
0
The code works, but it is not correct. I need to enter a number that corresponds to the player's name.
27th Apr 2021, 7:04 PM
Артём Попов
0
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string[] finalists = { "James Van", "John Smith", "Leyla Brown", "Tom Homerton", "Bob Douglas" }; int winner = Convert.ToInt32(Console.ReadLine()); //this should show the winner and "Game Over" FinalRound finalRound = new FinalRound(finalists, winner); } } class FinalRound { public FinalRound(string[] finalists, int winner) { //complete the constructor Console.WriteLine("Winner is " + finalists [winner ] ); } //create destructor => "Game Over" ~FinalRound(){ Console.WriteLine("Game Over"); } } }
12th May 2024, 6:05 PM
Vinayak Patil
Vinayak Patil - avatar