Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AulaVetores {
- public static void main(String[] args) {
- double notas[] = {8.0, 7.5, 8.5, 6.5, 5.1, 10.0, 6.5, 4.4, 5.9, 6.0};
- //Atividades
- //q1
- System.out.println("Média = "+calculaMedia(notas));
- System.out.println("Quantidade de notas abaixo de 6.0: "+quantidadeAbaixo6(notas));
- System.out.println("Quantidade de notas acima/igual de 6.0: "+quantidadeAcimaIgual6(notas));
- System.out.println("Quantidade acima/igual média: "+quantidadeAcimaIgualMedia(notas));
- System.out.println("Maior nota do vetor: "+maiorNota(notas));
- System.out.println("Menor nota do vetor: "+menorNota(notas));
- System.out.println("Existem notas repetidas? "+existemRepetidas(notas));
- }
- public static double calculaMedia(double notas[]) {
- double soma = 0;
- for (int i = 0; i < notas.length; i++) {
- soma = soma + notas[i];
- }
- double media = (double)soma/notas.length;
- return media;
- }
- public static int quantidadeAbaixo6(double[] notas) {
- int contador = 0;
- for (int i = 0; i < notas.length; i++) {
- if (notas[i] < 6.0) contador++;
- }
- return contador;
- }
- public static int quantidadeAcimaIgual6(double[] notas) {
- int contador = 0;
- for (int i = 0; i < notas.length; i++) {
- if (notas[i] >= 6.0) contador++;
- }
- return contador;
- }
- public static int quantidadeAcimaIgualMedia(double[] notas) {
- int contador = 0;
- double media = calculaMedia(notas);
- for (int i = 0; i < notas.length; i++) {
- if (notas[i] >= media) contador++;
- }
- return contador;
- }
- public static double maiorNota(double[] notas) {
- double maior = notas[0];
- for (int i = 0; i < notas.length; i++) {
- if (notas[i]>maior) maior = notas[i];
- }
- return maior;
- }
- public static double menorNota(double[] notas) {
- double menor = notas[0];
- for (int i = 0; i < notas.length; i++) {
- if (notas[i]<menor) menor = notas[i];
- }
- return menor;
- }
- public static boolean existemRepetidas(double[] notas) {
- boolean existe = false;
- for (int i = 0; i < notas.length; i++) {
- for (int j = 0; j < notas.length; j++) {
- if (j != i)
- if (notas[i] == notas[j])
- existe = true;
- if (existe) break;
- }
- if (existe) break;
- }
- return existe;
- }
- public static double[] notaMaisRepetida(double[] notas) {
- double maisRepetida = notas[0];
- double numeroRepeticoes[] = new double[notas.length]; //guarda o numero de repetições de cada item
- for (int i = 0; i < notas.length; i++) {
- for (int j = 0; j < notas.length; j++) {
- if (j != i)
- if (notas[i] == notas[j]) numeroRepeticoes[i]++;
- }
- }
- return numeroRepeticoes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement