Reputation: 98
In the Main() method, implement the following algorithm programs:
Each newly created object must be stored in an array of objects.
I am new to programming, I will hope for help. I got this code, I don't know how to create and populate objects:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Dog
{
private string name; // кличка
private string breed; // порода
private double weight; // вага
private int age; // вік
private char gender; // стать
public void Info()
{
name = Console.ReadLine();
breed = Console.ReadLine();
weight = double.Parse(Console.ReadLine());
age = int.Parse(Console.ReadLine());
gender = char.Parse(Console.ReadLine());
Console.WriteLine($"Кличка: {name} | Порода: {breed} | Вага: {weight} | Вік: {age} | Стать: {gender}");
}
}
public class Program
{
public static void Main(string[] args)
{
int dogsCount = int.Parse(Console.ReadLine());
int[] dogs = new int[dogsCount];
for(int i = 0; i < dogs.Length; i++)
{
Console.WriteLine($"Введіть елемент масиву під індексом {i}:\t");
dogs[i] = int.Parse(Console.ReadLine());
}
for(int i = 0; i < dogs.Length; i++)
{
Console.WriteLine(dogs[i]);
}
//Dog dog = new Dog();
//dog.Info();
}
}
}
Upvotes: 0
Views: 107
Reputation: 146
You should define constructor which save input properties to new dog object. Method GetInfo returns string with complete info about dog.
After start you have to ask to dog count and then in for loop you have to read individual properties for each dog. These properties you should send to the constructor of the dog object. Then you can save new dog to array and show info about dog.
using System;
public class Dog
{
private string Name { get; set; }
private string Breed { get; set; }
private double Weight { get; set; }
private int Age { get; set; }
private char Gender { get; set; }
public Dog(string name, string breed, double weight, int age, char gender)
{
Name = name;
Breed = breed;
Weight = weight;
Age = age;
Gender = gender;
}
public string GetInfo()
{
return string.Format("Name: {0} | Breed: {1} | Weight: {2} | Age: {3} | Gender: {4}", Name, Breed, Weight, Age, Gender);
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("How many dogs?");
int dogsCount = int.Parse(Console.ReadLine());
Dog[] dogs = new Dog[dogsCount];
for(int i = 0; i < dogsCount; i++)
{
Console.WriteLine("==================================");
Console.WriteLine(string.Format("Creating dog {0}/{1}", i + 1, dogsCount));
Console.WriteLine("Name:");
var name = Console.ReadLine();
Console.WriteLine("Breed:");
var breed = Console.ReadLine();
Console.WriteLine("Weight:");
var weight = double.Parse(Console.ReadLine());
Console.WriteLine("Age:");
var age = int.Parse(Console.ReadLine());
Console.WriteLine("Gender:");
var gender = char.Parse(Console.ReadLine());
var dog = new Dog(name, breed, weight, age, gender);
dogs[i] = dog;
Console.WriteLine(string.Format("Created dog -> {0}", dog.GetInfo()));
}
}
}
Upvotes: 3