TrabalhosGratuitos.com - Trabalhos, Monografias, Artigos, Exames, Resumos de livros, Dissertações
Pesquisar

Exercicios De Java

Pesquisas Acadêmicas: Exercicios De Java. Pesquise 860.000+ trabalhos acadêmicos

Por:   •  2/12/2013  •  5.025 Palavras (21 Páginas)  •  1.276 Visualizações

Página 1 de 21

Quarta lista Exercícios AEDS

Pollyanna Abreu

NOME : ______________________________

NOME : ______________________________

Belo Horizonte , 2013

QUARTA LISTA DE EXERCÍCIOS DE AEDSMATRIZ EM JAVA

1 - Faça um programa em Java para ler um conjunto valores do tipo inteiro, armazenar em uma matriz quadrada e imprimir todos os valores armazenados na matriz.

package Matriz;

import java.io.PrintStream;

import java.util.Scanner;

public class Ex1

{

public Ex1()

{

}

public static void main(String args[])

{

Scanner input = new Scanner(System.in);

int m[][] = new int[2][2];

for(int i = 0; i < m.length; i++)

{

System.out.printf("Informe os elementos da %d linha.\n", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < m.length; j++)

{

System.out.printf("Posi\347\343o dos valores: [%d][%d] = ", new Object[] {

Integer.valueOf(i), Integer.valueOf(j)

});

m[i][j] = input.nextInt();

}

System.out.printf("\n", new Object[0]);

}

for(int i = 0; i < m.length; i++)

{

System.out.printf("Valores: da %d. linha: ", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < m.length; j++)

System.out.printf("%d ", new Object[] {

Integer.valueOf(m[i][j])

});

System.out.printf("\n", new Object[0]);

}

}

}

2-Faça um programa em Java para ler um conjunto de valores do tipo double, e armazenar em uma matriz quadrada e imprimir somente os elementos da diagonal principal.

package Matriz;

import java.io.PrintStream;

import java.util.Scanner;

public class Ex2

{

public Ex2()

{

}

public static void main(String args[])

{

Scanner input = new Scanner(System.in);

double m[][] = new double[3][3];

for(int i = 0; i < m.length; i++)

{

System.out.printf("Informe os elementos da %d linha.\n", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < m.length; j++)

{

System.out.printf("Posi\347\343o dos valores: [%d][%d] = ", new Object[] {

Integer.valueOf(i), Integer.valueOf(j)

});

m[i][j] = input.nextDouble();

}

System.out.printf("\n", new Object[0]);

}

for(int i = 0; i < m.length; i++)

{

System.out.printf("Valores: da %d. linha: ", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < m.length; j++)

System.out.printf("[%.0f]", new Object[] {

Double.valueOf(m[i][j])

});

System.out.printf("\n", new Object[0]);

}

System.out.println("______________________");

System.out.print("Diagonal principal: ");

for(int i = 0; i < m.length; i++)

{

for(int j = 0; j < m.length; j++)

if(i == j)

System.out.printf("[%.0f]", new Object[] {

Double.valueOf(m[i][j])

});

}

}

}

3 - Faça um programa em Java que peça aousuário para digitar um conjunto valores inteiro e inserir na matriz A, em seguida digitar outros valores e inserir na matriz B. Multiplicar os valores de cada posição da matriz A [0][0] pelo elemento que corresponde a mesma posição na matriz B [0][0] fazendo isso para todos as posições e inserir o resultado na matriz C na posição correspondente. Imprimiras três matrizes. Todas as matrizes são 3x3.

package Matriz;

import java.io.PrintStream;

import java.util.Scanner;

public class Ex3

{

public Ex3()

{

}

public static void main(String args[])

{

Scanner input = new Scanner(System.in);

int m1[][] = new int[3][3];

int m2[][] = new int[3][3];

int m3[][] = new int[3][3];

for(int i = 0; i < m1.length; i++)

{

System.out.printf("Informe os elementos da %d linha.\n", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < m1.length; j++)

{

System.out.printf("Posi\347\343o dos valores: [%d][%d] = ", new Object[] {

Integer.valueOf(i), Integer.valueOf(j)

});

m1[i][j] = input.nextInt();

}

System.out.printf("\n", new Object[0]);

}

for(int i = 0; i < m2.length; i++)

{

System.out.printf("Informe os elementos da %d linha:\n", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < m2.length; j++)

{

System.out.printf("Posi\347\343o dos valores: [%d][%d] = ", new Object[] {

Integer.valueOf(i), Integer.valueOf(j)

});

m2[i][j] = input.nextInt();

}

System.out.printf("\n", new Object[0]);

}

for(int i = 0; i < m3.length; i++)

{

for(int j = 0; j < m3.length; j++)

m3[i][j] = m1[i][j] * m2[i][j];

}

for(int i = 0; i < m1.length; i++)

{

System.out.printf("Valores da 1\272MATRIZ %d linha: ", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < m1.length; j++)

System.out.printf("[%d]", new Object[] {

Integer.valueOf(m1[i][j])

});

System.out.printf("\n", new Object[0]);

}

System.out.println("---------------------------");

for(int i = 0; i < m2.length; i++)

{

System.out.printf("Valores da 2\272MATRIZ %d linha: ", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < m2.length; j++)

System.out.printf("[%d]", new Object[] {

Integer.valueOf(m2[i][j])

});

System.out.printf("\n", new Object[0]);

}

System.out.println("-----------Multiplica\347\343o--------");

for(int i = 0; i < m3.length; i++)

{

System.out.printf("Valores da 3\272MATRIZ %d linha: ", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < m3.length; j++)

System.out.printf("[%d]", new Object[] {

Integer.valueOf(m3[i][j])

});

System.out.printf("\n", new Object[0]);

}

}

}

4 - Faça um programa em Java que leia um conjunto de valores, insira na matriz A e somar todos os elementos da matriz A por 2, e insira os resultados na matriz B.

package Matriz;

import java.io.PrintStream;

import java.util.Scanner;

public class Ex4

{

public Ex4()

{

}

public static void main(String args[])

{

int matrizA[][] = new int[2][2];

int matrizB[][] = new int[2][2];

Scanner input = new Scanner(System.in);

for(int i = 0; i < matrizA.length; i++)

{

System.out.printf("Informe os elementos da %d linha. \n", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < matrizA.length; j++)

{

System.out.printf("Posicao dos valores: [%d][%d]:", new Object[] {

Integer.valueOf(i), Integer.valueOf(j)

});

matrizA[i][j] = input.nextInt();

matrizA[i][j] += 2;

}

System.out.printf("\n", new Object[0]);

}

System.out.printf("Valores da matriz +2 = ", new Object[0]);

for(int i = 0; i < matrizB.length; i++)

{

for(int j = 0; j < matrizB.length; j++)

{

matrizB[i][j] = matrizA[i][j];

System.out.printf("[%d]", new Object[] {

Integer.valueOf(matrizB[i][j])

});

}

}

}

}

5 - Faça um programa em Java que peça ao usuário para digitar um conjunto valores inteiro e inserir na matriz, em seguida verificar se existe o elemento 10 inserido na matriz, se não for encontrado o elemento 10 na matriz imprimir uma mensagem.

package Matriz;

import java.io.PrintStream;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class Ex5

{

public Ex5()

{

}

public static void main(String args[])

{

int matriz[][] = new int[2][2];

try

{

Scanner input = new Scanner(System.in);

for(int i = 0; i < matriz.length; i++)

{

System.out.printf("Informe o elemento da %d linha. \n", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < matriz.length; j++)

{

System.out.printf("Posicao do valor [%d][%d] = ", new Object[] {

Integer.valueOf(i), Integer.valueOf(j)

});

matriz[i][j] = input.nextInt();

}

}

for(int i = 0; i < 1; i++)

{

for(int j = 0; j < 1; j++)

if(matriz[i][j] == 10)

System.out.println("Existe o elemento 10 na matriz.");

else

System.out.println("N\343o existi o elemento 10 na matriz.");

}

}

catch(Exception erro)

{

JOptionPane.showMessageDialog(null, (new StringBuilder("Erro interno no sistema.")).append(erro).toString());

}

}

}

6 - Faça um programa em java, a fim de criar e imprimir uma matriz B, 5 x 5, tal que as 4 primeiras colunas sejam formadas pelos elementos correspondentes de A e a 5a coluna seja formada pelo maior elemento de cada linha correspondente de A.

Exemplo:

package Matriz;

import java.io.PrintStream;

import java.util.Scanner;

public class Ex6

{

public Ex6()

{

}

public static void main(String args[])

{

int matrizA[][] = new int[5][4];

int matrizB[][] = new int[5][5];

int maior0 = 0;

int maior1 = 0;

int maior2 = 0;

int maior3 = 0;

int maior4 = 0;

Scanner input = new Scanner(System.in);

for(int i = 0; i < 5; i++)

{

System.out.printf("Informe o valor da %d linha. \n", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < 4; j++)

{

System.out.printf("posicao [%d][%d] = ", new Object[] {

Integer.valueOf(i), Integer.valueOf(j)

});

matrizA[i][j] = input.nextInt();

}

System.out.println("\n");

}

for(int i = 0; i < 5; i++)

{

System.out.printf("Elementos da MA %d linha: ", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < 4; j++)

System.out.printf("{%d} ", new Object[] {

Integer.valueOf(matrizA[i][j])

});

System.out.printf("\n", new Object[0]);

}

System.out.println("-------");

for(int i = 0; i < 5; i++)

{

System.out.printf("Elementos da MB %d linha: ", new Object[] {

Integer.valueOf(i + 1)

});

for(int j = 0; j < 5; j++)

{

matrizB[i][0] = matrizA[i][0];

matrizB[i][1] = matrizA[i][1];

matrizB[i][2] = matrizA[i][2];

matrizB[i][3] = matrizA[i][3];

if(matrizB[0][3] > maior0)

{

maior0 = matrizB[i][j];

matrizB[0][4] = maior0;

}

if(matrizB[1][3] > maior1)

{

maior1 = matrizB[1][j];

matrizB[1][4] = maior1;

}

if(matrizB[2][3] > maior2)

{

maior2 = matrizB[i][j];

matrizB[2][4] = maior2;

}

if(matrizB[3][3] > maior3)

{

maior3 = matrizB[i][j];

matrizB[3][4] = maior3;

}

if(matrizB[4][3] > maior4)

{

maior4 = matrizB[i][j];

matrizB[4][4] = maior4;

}

System.out.printf("{%d} ", new Object[] {

Integer.valueOf(matrizB[i][j])

});

}

System.out.printf("\n", new Object[0]);

}

}

}

7 - Matriz transposta, em matemática, é o resultado da troca de linhas por colunas em uma determinada matriz. Uma matriz simétrica é toda a matriz que é igual à sua transposta. Neste exercício, a matriz transposta de uma matriz será representada por . Outras formas de representação encontradas na literatura são e .

Faça um programa em java que entre com dados digitados pelos usuários para preencher a matriz. Imprima a matriz preenchida e em seguida calcule e imprima a matriz transposta da matriz A. (mat 3x3)

import javax.swing.JOptionPane;

public class Ex7 {

public static void main(String[] args) {

int MA[][]= new int[3][3];

int MT[][]= new int[3][3];

int i=0;

int j=0;

for ( i=0; i♥; i++){

for ( j=0; j♥; j++){

MA[i][j]= Integer.parseInt(JOptionPane.showInputDialog(" Entre um valor inteiro "));

}

}

for (i=2; i>=0; i--){

for ( j=2; j>=0; j--){

MT[0][0]=MA[0][0];

MT[0][1]=MA[1][0];

MT[0][2]=MA[2][0];

MT[1][0]=MA[0][1];

MT[1][1]=MA[1][1];

MT[1][2]=MA[2][1];

MT[2][0]=MA[0][2];

MT[2][1]=MA[1][2];

MT[2][2]=MA[2][2];

System.out.println ();

System.out.println ( " os valores na matriz transposta da é : [ "+ i +"]" + "[" + j + "] : " + MT[i][j] );

System.out.println ( " os valores na matriz A da é : [ "+ i +"]" + "[" + j + "] : " + MA[i][j] );

}

}

}

}

8 - Matriz identidade : A matriz identidade In é a matriz quadrada n × n em que todas as entradas da diagonal principal são iguais a 1 e as demais são iguais a zero, por exemplo.

Ela é chamada de matriz identidade pois, multiplicá-la por outra matriz não altera a matriz: MIn = ImM = M para qualquer matriz M de ordem m por n.

Faça um programa em java que imprima uma matriz identidade 3x3 que entre com dados 1 na diagonal principal e com 0 nas demais posições.

public class MIdentidadeouDiagonal {

public static void main(String[] args) {

// TODO Auto-generated method stub

int MIDiag[][]= new int [3][3];

int i,j=0;

for ( i=0; i♥; i++){

for ( j=0; j♥; j++){

if ( i==j){

MIDiag[i][j]=1;

}

}

}

for ( i=0; i♥; i++){

System.out.println( );

System.out.println( " Os elementos na linha %d são: ");

for ( j=0; j♥; j++){

System.out.println( );

System.out.printf ( " Posicao [%d][%d]: " ,i,j);

System.out.printf ( " %d" ,MIDiag[i][j] );

System.out.println( );

}

}

}

}

...

Baixar como  txt (15.6 Kb)  
Continuar por mais 20 páginas »