Usuário com melhor resposta
Orientação a objetos

Pergunta
-
Boa Tarde galera! Desenvolvi o código abaixo e precisava passar ele pra orientado a objetos. Queria algumas dicas, pois não sei trabalhar muito bem com POO.
O código abaixo, cria 2 vetores, inicializa os 2 com valores randômicos e soma cada posição correspondente nos dois vetores. A soma é atribuída a um terceiro vetor. E depois é calculado o menor e o maior valor do vetor soma.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercicio_Final
{
class Program
{
//cria vetores
static void CriaVetor(out int[] vet1, out int[] vet2, out int[] vetsoma)
{
vet1 = new int[100];
Console.Clear();
vet2 = new int[100];
Console.Clear();
vetsoma = new int[100];
Console.Clear();
}
static void PreencheVetor(int[] vet1, int[] vet2)
{
Console.Write("-------- VETOR 1 --------\n");
Random numRandom = new Random();
for (int i = 0; i < vet1.GetLength(0); i++)
{
vet1[i] = numRandom.Next(1, 1000);
Console.Write(vet1[i] + "\t");
}
Console.WriteLine();
Console.Write("-------- VETOR 2 --------\n");
Random numRandom2 = new Random();
for (int i = 0; i < vet2.GetLength(0); i++)
{
vet2[i] = numRandom.Next(1, 1000);
Console.Write(vet2[i] + "\t");
}
Console.WriteLine();
}
static void somaVetores(int[] vet1, int[] vet2, int[] vetsoma)
{
Console.Write("-------- SOMA DOS VETORES --------\n");
for (int i = 0; i < vet1.GetLength(0); i++)
{
vetsoma[i] = vet1[i] + vet2[i];
Console.Write(vetsoma[i] + "\t");
}
Console.WriteLine();
}
static void menorValor(int[] vetsoma, out int menor)
{
menor = vetsoma[0];
for (int i = 0; i < vetsoma.GetLength(0); i++)
{
if (vetsoma[i] < menor)
{
menor = vetsoma[i];
}
}
}
static void maiorValor(int[] vetsoma, out int maior)
{
maior = vetsoma[0];
for (int i = 0; i < vetsoma.GetLength(0); i++)
{
if (vetsoma[i] > maior)
{
maior = vetsoma[i];
}
}
}
static void valores(int[] vetsoma, out int menor, out int maior)
{
maiorValor(vetsoma, out maior);
menorValor(vetsoma, out menor);
Console.Write("-------------------------------------------------------");
Console.WriteLine("\nO menor valor do vetor é: " + menor);
Console.WriteLine("O maior valor do vetor é: " + maior);
Console.Write("-------------------------------------------------------");
}
static void Main(string[] args)
{
int[] vet1;
int[] vet2;
int[] vetsoma;
int menor, maior;
CriaVetor(out vet1, out vet2, out vetsoma);
PreencheVetor(vet1, vet2);
somaVetores(vet1, vet2, vetsoma);
valores(vetsoma, out menor, out maior);
Console.ReadKey();
}
}
}
Obs: Precisa de uma direção, pois não estou sabendo por onde começar.
Desde já agradeço.
Respostas
-
Olá,
Meu caro, acredito que o melhor começo é entender os conceitos. No fórum de C#, existe um tópico com vários compilados de tutoriais, incluindo de orientação a objetos:
Compilação de tutoriais sobre C#
Também tem esse tutorial, presente no site do MSDN:
Programação Orientada a Objetos em .NET – Parte 1
Acredito que lhe será bastante útil. Mas vai ai uma dica para o seu exercício: você tem vetores e um método para os preencher com valores randômicos. Não seria o caso de ter um objeto que encapsule o vetor e tenha um método para gerar esses valores randômicos? Essa mesma classe não poderia também saber informar qual é o maior e menor valor presente no vetor?
Hudson Carvalho
- Marcado como Resposta Robson William SilvaModerator segunda-feira, 27 de março de 2017 13:34