0% acharam este documento útil (0 voto)
3 visualizações10 páginas

Exercícios de Programação em Java

Enviado por

Magnum Opus
Direitos autorais
© All Rights Reserved
Levamos muito a sério os direitos de conteúdo. Se você suspeita que este conteúdo é seu, reivindique-o aqui.
Formatos disponíveis
Baixe no formato DOCX, PDF, TXT ou leia on-line no Scribd
0% acharam este documento útil (0 voto)
3 visualizações10 páginas

Exercícios de Programação em Java

Enviado por

Magnum Opus
Direitos autorais
© All Rights Reserved
Levamos muito a sério os direitos de conteúdo. Se você suspeita que este conteúdo é seu, reivindique-o aqui.
Formatos disponíveis
Baixe no formato DOCX, PDF, TXT ou leia on-line no Scribd

A fazer:

● 30

Questão 1 -
package exercicios1i;
import [Link];
public class exercicios {
public static void main(String[] args) {
Scanner teclado = new Scanner ([Link]);
int[] x = new int[10];
for (int i = 0; i<=9; i++) {
[Link]("Digite um valor: ");
x[i] = [Link]();
}
[Link]("O primeiro valor é " + x[0]+ " e o quinto
valor é " + x[4]);

[Link]();
}
}

Questão 2 -
package exercicios1i;
import [Link];
public class exercicios {
public static void main(String[] args){
Scanner teclado = new Scanner([Link]);
int[] x = new int[10];
for(int i = 0; i <=9; i++){
[Link]("Digite um valor:");
x[i] = [Link]();
}
for(int i = 0; i <= 9; i++){
if(x[i] > 50){
[Link](x[i]);
}
}
[Link]();
}
}

Questão 3 -
package exercícios1i;
import [Link];
public class Ibianca {
public static void main(String[] args) {
Scanner teclado = new Scanner ([Link]);
int soma=0;
int [] x = new int[10];
int [] y = new int[10];
[Link]("Digite os 10 valores: ");
for(int i = 0; i<[Link]; i++) {
int num = [Link]();
if(num%2==0) {
y[i] = num;
soma++;
}
}
for(int i = 0; i<[Link]; i++) {
if(y[i]!=0) {
[Link]("Números pares: " + y[i]);
}
[Link]();
}
}
}

Questão 4 -
package exercícios;
import [Link];
public class Ibianca {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
int soma = 0;int[] a = new int[10];
for(int i = 0;i<10;i++) {
[Link]("Valor " + (i+1) + ": ");
a[i] = [Link]();
soma = soma+a[i];
}
[Link]("Soma dos valores: " + soma);
[Link]("Valores Digitados: ");
for(int i = 0;i<10;i++) {
[Link](a[i] + " ");
}
[Link]();
}
}

Questão 5 -
package exercícios;
import [Link];
public class Ibianca {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
int soma = 0;
int maior = 0;
int[ ] y = new int[10];
int[ ] x = new int[10];
for (int i = 0 ; i <= 9 ; i++){
[Link]("Digite um valor: ");
x[i] = [Link]();
soma = soma + x[i];
}
int media = soma/10;
for(int i = 0; i<=9;i++) {
if(x[i]>media) {
maior++;
y[i] = x[i];
}
}
[Link]("Média: "+ media);
[Link]("Números maiores que a média: "+maior);
for(int i = 0; i<10; i++) {
if(y[i]>0) {
[Link](y[i]+" ");
}
}
[Link]();
}
}

Questão 9 -
package exercicios1i;
import [Link];
public class exercicios {
public static void main(String[] args) {
Scanner teclado = new Scanner ([Link]);
int soma =0, divisor=0;
int[] x = new int[10];
for (int i = 0; i<=9; i++) {
[Link]("Digite um valor: ");
x[i] = [Link]();
soma = soma + x[i];
if (x[i]!=0) {
divisor++;
}
}
int media = soma/divisor;
[Link]("Média: "+ media);
[Link]();
}
}

Questão 10 -
package exercicios1i;
import [Link];
public class prova {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
String[] nomes = new String[5];
for(int i = 0;i < 6;i++) {
[Link]("Nome " + (i+1) + ": ");
nomes[i] = [Link]();
}
[Link]("Nomes Registrados: ");
for(int i = 0;i<6;i++) {
[Link]((i+1) + "° nome: " + nomes[i]);
}
[Link]();
}
}

Questão 11 -
package exercícios1i;
import [Link];
public class Ibianca {
public static void main(String[] args) {
int [] x = new int[10];
for(int i = 0; i<[Link]; i++) {
x[i]=30;
}
for(int i = 0; i<[Link]; i++) {
[Link](i+1 + " = "+x[i]);
}
}
}

Questão 12 -
package exercícios;
import [Link];
public class I1 {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
int[] x = new int[10];
for(int i = 0;i<=9;i++) {
[Link]("Valor " + (i+1) + ": ");
x[i] = [Link]();
}
[Link]("Valores dos índices pares: ");
for(int i = 0;i<=9;i++) {
if(i%2==0) {
[Link](x[i]);
}
}
[Link]();
}
}

Questão 14 -
package exercícios1i;
import [Link];
public class Ibianca {
public static void main(String[] args) {
Scanner teclado = new Scanner ([Link]);
int soma = 0;
int [] x = new int [10];
for (int i =0; i<[Link]; i++) {
[Link]("Digite o números: ");
x[i] = [Link]();
}
[Link]("Digite o número de comparação: ");
int comp = [Link]();
for(int i = 0; i<[Link]; i++) {
if(comp == x[i]) {
soma++;
}
}
[Link]("Ocorre " + soma + " vezes");
[Link]();
}
}
Questão 15 -
package exercícios1i;
import [Link];
public class Ibianca {
public static void main(String[] args) {
Scanner teclado = new Scanner ([Link]);
int soma = 0;
int [] x = new int [10];
for (int i =0; i<[Link]; i++) {
[Link]("Digite o números: ");
x[i] = [Link]();

}
[Link]("Digite o número para ser encontrado: ");
int enc = [Link]();
for(int i = 0; i<[Link]; i++) {
if(enc == x[i]) {
[Link]("x["+ i + "]");
}
}
[Link]();
}
}

Questão 21 (Revisar/refazer) -
package exercícios1i;
import [Link];
import [Link];
public class Ibianca {
public static void main(String[] args) {
Scanner teclado = new Scanner ([Link]);
int soma = 0;
int [] x = new int [10];
int [] y = new int [[Link]];
for (int i =0; i<[Link]; i++) {
[Link]("Digite o números: ");
x[i] = [Link]();
}
for(int i = [Link] - 1; i>=0; i--)
{
y[i] = x[[Link] - i -1];

}
[Link]([Link](x) + "\n" +
[Link](y));
[Link]();
}
}

Questão 22 -
import [Link];
public class Exercícios {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
[Link]("Tamanho da Array: ");
int a = [Link]();
int[] x5 = new int[a];
int[] x = new int[a];
for(int i=0; i<[Link] ; i++) {
[Link]((i+1) + "º valor: ");
x[i] = [Link]();
}
[Link]("Valores divisíveis por 5: ");
for(int i = 0; i<[Link];i++) {
if(x[i]%5 ==0) {
[Link](x[i] + " ");
}
}
[Link]();
}
}

Questão 23 -
package exercícios;
import [Link];
public class I1 {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
[Link]("Tamanho: ");
int a = [Link]();
int x[]= new int[a];
for(int i = 0 ; i<a; i++) {
[Link]("Digite o valor: ");
x[i]= [Link]();

}
for(int i = 0; i<a; i++) {
[Link](x[i]+ " ");
}
[Link]();
for(int i = 0 ; i<a; i++) {
if(x[i]==0) {
x[i]=-99;
[Link](x[i] + " ");
}else {
[Link](x[i] + " ");
}
}

[Link]();
}
}

Questão 26 -
package exercícios;
import [Link];
public class I1 {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
int x[] = new int[10];
for(int i = 0; i<10;i++) {
[Link]("Digite o valor da temperatura: ");
x[i] = [Link]();
}
for(int i = 0; i<10; i++) {
[Link](x[i]+ " ");
}
[Link]();
[Link]("Maiores que 15: ");
for(int i = 0; i<10; i++) {
if(x[i]>15) {
[Link](x[i]);
}
}
[Link]();
}
}

Questão 28 -
package exercícios;
import [Link];
public class I1 {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
int x[] = new int[100];
for(int i = 0; i<100;i++) {
[Link]("Digite o valor: ");
x[i] = [Link]();
if(x[i]<0) {
x[i]= 999;
}
}
for(int i = 0; i<100; i++) {
[Link](x[i]+ " ");
}
[Link]();
}
}

Questão 32 -
import [Link];
public class Exercícios {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
int[] x = new int[5];
int menor = 20000, maior = 0, igual = 1;
for(int i = 0; i<[Link]; i++) {
[Link]("Insira o número: ");
x[i] = [Link]();
if(x[i]> maior){
maior = x[i];
}
if(x[i]<menor) {
menor = x[i];
} else { if(menor==x[i]) {
igual++;
}
}
}
[Link]("O menor número é: "+ menor);
[Link]("O menor número aparece "+ igual + "
vezes");
[Link]();
}
}

Questão 41 -
import [Link];
public class Main {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
String[] nome = new String[2];
for(int i = 0; i<[Link]; i++){
[Link]("Digite um nome: ");
nome[i] = [Link]();
}
for(int i = 0; i<[Link]; i++){
[Link]((i+1)+"º "+ nome[i]);
}
[Link]();
}

Questão 42 -
import [Link];
public class Main {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
int[] num = new int[15];
for(int i = 0; i<[Link]; i++){
[Link]("Digite um número: ");
num[i] = [Link]();
}
for(int i = 0; i<[Link]; i++){
[Link]((i+1)+"º: "+ num[i]);
if (num[i]%2==0){
[Link]("Par");
}else{
[Link]("Ímpar");
}
}
[Link]();
}
}

Questão 43 -
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
int soma = 0;
int[] num = new int[8];
for(int i = 0; i<[Link]; i++){
[Link]("Digite um número: ");
num[i] = [Link]();
}
for(int i = 0; i<[Link]; i++){
if (num[i]%6==0){
soma++;
}
}
[Link]([Link](num));
[Link]("Múltiplos de 6 digitados: "+soma);
[Link]();
}
}
Questão 56 -
import [Link];
public class Main {
public static void main(String[] args) {
Scanner teclado = new Scanner([Link]);
int [] x = new int[10];
int soma = 0, soma30 = 0, maior=0, igual=0;
double media;
for(int i = 0; i < [Link]; i++){
[Link]("Digite um número: ");
x[i] = [Link]();
soma = soma + x[i];
if(x[i]==30){
soma30++;
}
}
media = soma / [Link];
for(int i = 0; i < [Link]; i++){
if(x[i] > media){
maior++;
}else{
if(x[i] == media){
igual++;
}
}
}
[Link]("O número 30 apareceu: "+ soma30 + " vezes");
[Link]("Sendo a média dos números "+ media + ": "+ "\n" + maior + "
números são maiores que a média" + "\n" + igual+ " números são iguais a média");
[Link]();
}
}

Common questions

Com tecnologia de IA

The programs generally lack explicit error handling and sufficient user guidance, with 'Questão 1' and 'Questão 4' offering only basic instructions for inputs without checks for invalid data. The absence of input validation and exception handling across these programs indicates an area for improvement. Implementing try-catch blocks and providing user feedback enhances reliability and user experience. However, none of these programs currently exhibit best practices, highlighting the need for improved error management strategies in the programming process .

Not validating array indices before access, as in 'Questão 10', can lead to ArrayIndexOutOfBoundsException errors, which terminate the program unexpectedly. This reflects a critical flaw where program stability and reliability are compromised. Ensuring index validity through boundary checks or conditional logic preempts such errors, safeguarding against code crashes and enhancing overall software robustness .

'Questão 21' showcases array manipulation through reversing elements of an array into another. The technique involves copying elements from the end of the original array to the start of the new array, effectively reversing their order. This is significant in programming for tasks such as data processing where reversing order can be critical for algorithms that depend on sequence alteration. Mastery of array manipulation is essential in optimizing and implementing various algorithmic solutions .

The program in 'Questão 3' could be improved by adding error handling for non-integer inputs or a validation mechanism to ensure only numeric inputs are accepted, thus preventing runtime errors. Additionally, giving clearer instructions or feedback on the expected input format could enhance user experience. Implementing these improvements would make the software more robust and user-friendly .

The program in 'Questão 1' enhances user interaction by prompting the user to input ten integer values through the console. It stores these values in an array, and the primary outputs are the first and fifth values entered by the user. This interaction allows users to see specific values of interest from their inputs, which can be useful for quick checks or validations .

In 'Questão 5', the program calculates the average of ten integers by dividing the sum of these numbers by ten. It then iterates over the array to count how many numbers exceed this average. This operation helps in identifying outliers or number disparities within the user inputs, which can highlight variations or trends in the data provided .

'Questão 12' uses conditional statements to filter and display elements at even indices of an array. It iterates through the array and employs modulus operator logic (i % 2 == 0) to identify even indices, outputting only those array values. This demonstrates using conditionals to selectively process data, which is vital in applications requiring specific value retrieval or categorization .

The implications of incorrect indexing in 'Questão 10' are potential runtime errors, such as ArrayIndexOutOfBoundsException, which occur because the program attempts to access an invalid index (i=6 in a 5-element array). This error disrupts program execution and can cause the software to crash, highlighting a crucial aspect of array manipulation where boundary checks are essential to ensure reliable program functionality .

'Questão 15' utilizes a linear search mechanism to locate a user-specified number within an array of ten integers. It iterates over each element, checking for equality with the search number, and outputs the index of any matching elements. This simple yet effective approach quickly identifies the presence and position of data within the array .

The program in 'Questão 9' computes the average by summing up all values in an array and dividing by the count of non-zero elements encountered (relying on a divisor variable). If no non-zero elements are present, the divisor would be zero, causing a division by zero error. This error would trigger an ArithmeticException, potentially terminating the program abruptly, indicating the necessity for preventive checks before division operations .

Você também pode gostar