Created
March 12, 2018 19:48
-
-
Save jimywork/6584d4e8e66a9f85d1ea681c902af673 to your computer and use it in GitHub Desktop.
Exercicio Vetor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fabio Castro:: | |
import java.util.Arrays; | |
public class MeuVetor { | |
private double[] notas = new double[5]; | |
public MeuVetor() { | |
limpar(); | |
} | |
public void limpar() { | |
for (int i = 0; i < notas.length; i++) { | |
notas[i] = -1; | |
} | |
} | |
public void incluir(int pos, double valor) { | |
notas[pos] = valor; | |
} | |
public boolean valorExiste(double valor) { | |
boolean existe = false; | |
for (int i = 0; i < notas.length; i++) { | |
if (notas[i] == valor) { | |
existe = true; | |
break; | |
} | |
} | |
return existe; | |
} | |
public int posicaoValor(double valor) { | |
if (valorExiste(valor)) { | |
for (int i = 0; i < notas.length; i++) { | |
if (notas[i] == valor) { | |
return i; | |
} | |
} | |
} | |
return notas.length; | |
} | |
public int campoVazio() { | |
for (int i = 0; i < notas.length; i++) { | |
if (notas[i] == -1) { | |
return i; | |
} | |
} | |
return notas.length; | |
} | |
public void excluir(int pos) { | |
notas[pos] = -1; | |
} | |
public String listar() { | |
return Arrays.toString(notas); | |
} | |
public double listar(int pos) { | |
return notas[pos]; | |
} | |
} | |
Fabio Castro: | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
MeuVetor mv = new MeuVetor(); | |
Scanner sc = new Scanner(System.in); | |
boolean controleLoop = true; | |
while (controleLoop) { | |
System.out.println("1 - Inserir dados 2 - Excluir 3 - Alterar 4 - Listar dados 5 - Sair Opção: "); | |
int op = sc.nextInt(); | |
switch (op) { | |
case 1: | |
System.out.println("Valor: "); | |
double valor = sc.nextDouble(); | |
if (mv.valorExiste(valor)){ | |
System.out.println("Valor já existe!"); | |
} else { | |
int posDisponivel = mv.campoVazio(); | |
if (posDisponivel != 5) { | |
mv.incluir(posDisponivel, valor); | |
} else { | |
System.out.println("Não há posições disponiveis"); | |
} | |
} | |
break; | |
case 2: | |
System.out.println("1 - Excluir valor 2 - Excluir posição Opção: "); | |
int pos = sc.nextInt(); | |
switch (pos) { | |
case 1: | |
System.out.println("Valor: "); | |
mv.excluir(mv.posicaoValor(sc.nextDouble())); | |
break; | |
case 2: | |
System.out.println("Posição: "); | |
mv.excluir(sc.nextInt()); | |
break; | |
default: | |
System.out.println("Opção inválida!"); | |
} | |
break; | |
case 3: | |
System.out.println("1 - Alterar valor 2 - Alterar posição Opção: "); | |
int opcao = sc.nextInt(); | |
switch (opcao) { | |
case 1: | |
System.out.println("Valor: "); | |
int altPos = mv.posicaoValor(sc.nextDouble()); | |
if (altPos != 5) { | |
System.out.println("Valor: "); | |
mv.incluir(altPos, sc.nextDouble()); | |
} | |
break; | |
case 2: | |
System.out.println("Posição: "); | |
int posValor = mv.posicaoValor(mv.listar(sc.nextInt())); | |
if (posValor != 5) { | |
System.out.println("Novo valor: "); | |
mv.incluir(posValor, sc.nextDouble()); | |
} | |
break; | |
default: | |
System.out.println("Opção inválida!"); | |
} | |
break; | |
case 4: | |
System.out.println("1 - Listar todos 2 - Listar único Opção: "); | |
int listarOp = sc.nextInt(); | |
switch (listarOp) { | |
case 1: | |
System.out.println(mv.listar()); | |
break; | |
case 2: | |
System.out.println("Posição: "); | |
System.out.println(mv.listar(sc.nextInt())); | |
break; | |
default: | |
System.out.println("Opção inválida!"); | |
} | |
break; | |
case 5: | |
controleLoop = false; | |
break; | |
default: | |
System.out.println("Opção inválida!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment