Skip to content

Instantly share code, notes, and snippets.

@iagosousadev
Created August 30, 2013 13:42
Show Gist options
  • Save iagosousadev/6389967 to your computer and use it in GitHub Desktop.
Save iagosousadev/6389967 to your computer and use it in GitHub Desktop.
/* Defina uma classe para representar um Vetor multi dimensional, ou seja, um vetor definito por:
* V = (a1, a2, a3, ..., an)
* Sua classe deverá receber um array de números inteiros na construção, representando seus componentes.
*
* Sua Classe deverá ter um método para calcular o produto escalar entre o vetor atual (this) e um vetor informado como parametro através da formula:
*
* V1 = (a1, a2, ..., an)
* V2 = (b1, b2, ..., bn)
* for (int i = 0; i < n; i++) {
* V1xV2[i] += a[i] x b[i]
* }
* */
// MAIN CLASS
public class main {
public static void main(String[] args) {
Vetor v1 = new Vetor (new int[] {1, 2 ,3});
Vetor v2 = new Vetor (new int[] {4, 5, 6});
System.out.print("O produto é: " + v1.produtoEscalar(v2));
}
}
//VETOR CLASS
public class Vetor {
private int[] vetor;
public Vetor (int[] vet) {
this.vetor = vet;
}
public int[] getvet () {
return vetor;
}
public int produtoEscalar (Vetor vet1) {
int[] v1 = this.vetor;
int[] v2 = vet1.getvet();
int soma = 0;
for (int i = 0; i < v2.length; i++) {
soma += v1[i] * v2[i];
}
return soma;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment