viernes, 3 de enero de 2014

[Ejercicio resuelto Java] Sumar dos matrices de orden 2x2 – Texas – Platform


Anteriormente escribí una entrada en el blog en la cual hablo un poco de algunas formas de manejar lasmatrices en java. En esta entrada veremos como sumar los elementos de dos matrices de orden 2x2 (2 filas, 2 columnas).

Enunciado:
"Realizar un programa en java el cual sume dos matrices de orden 2x2".

SumMatrices.java
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;

public class SumMatrices {
private Scanner tec;
private int[][] matrizA;
private int[][] matrizB;
private int[][] matrizC;


public void CargarElementos(){
tec = new Scanner(System.in);
matrizA = new int[2][2];
matrizB = new int[2][2];
System.out.println("Ingrese los elementos de la matriz A: \n");
System.out.println("Ingrese el elemento a11 de la matriz");
matrizA[0][0] = tec.nextInt();
System.out.println("Ingrese el elemento a12 de la matriz");
matrizA[0][1] = tec.nextInt();
System.out.println("Ingrese el elemento a21 de la matriz");
matrizA[1][0] = tec.nextInt();
System.out.println("Ingrese el elemento a22 de la matriz");
matrizA[1][1] = tec.nextInt();
//Matriz A:
System.out.println("A= " + matrizA[0][0] + " " + matrizA[0][1]);
System.out.println(" " + matrizA[1][0] + " " + matrizA[1][1]);
//----------------------------------------------------------------
System.out.println("\nIngrese los elementos de la matriz B: \n");
System.out.println("Ingrese el elemento b11 de la matriz");
matrizB[0][0] = tec.nextInt();
System.out.println("Ingrese el elemento b12 de la matriz");
matrizB[0][1] = tec.nextInt();
System.out.println("Ingrese el elemento b21 de la matriz");
matrizB[1][0] = tec.nextInt();
System.out.println("Ingrese el elemento b22 de la matriz");
matrizB[1][1] = tec.nextInt();
//Matriz B:
System.out.println("B= " + matrizB[0][0] + " " + matrizB[0][1]);
System.out.println(" " + matrizB[1][0] + " " + matrizB[1][1]);
//-----------------------------------------------------------------
}

public void Suma(){
matrizC = new int[2][2];
matrizC[0][0] = matrizA[0][0] + matrizB[0][0];
matrizC[0][1] = matrizA[0][1] + matrizB[0][1];
matrizC[1][0] = matrizA[1][0] + matrizB[1][0];
matrizC[1][1] = matrizA[1][1] + matrizB[1][1];
}

public void Imprimir(){
System.out.println("La Suma de ambas matrices es: \n");
//Matriz C:
System.out.println("C= " + matrizC[0][0] + " " + matrizC[0][1]);
System.out.println(" " + matrizC[1][0] + " " + matrizC[1][1]);
//-----------------------------------------------------------------
System.out.println("\n Elementos de la matriz C: \n");
System.out.println("Elemento c11: " + matrizC[0][0]);
System.out.println("Elemento c12: " + matrizC[0][1]);
System.out.println("Elemento c21: " + matrizC[1][0]);
System.out.println("Elemento c22: " + matrizC[1][1]);
}

public static void main(String[] ar){
SumMatrices sm = new SumMatrices();
sm.CargarElementos();
sm.Suma();
sm.Imprimir();
}
}

Explicación:
Declaramos tres matrices del tipo entero:
private int[][] matrizA;
private int[][] matrizB;
private int[][] matrizC;

Sí lo desean los elementos pueden ser también número reales, es decir, del tipo float.

Definimos el tamaño de nuestras matrices:
matrizA = new int[2][2];
matrizB = new int[2][2];

Luego, empezamos a cargar los elementos de ambas matrices, al finalizar la carga de los elementos de cada matriz, estos serán mostrados en pantalla para su comprobación.

El método Suma() será el encargado de sumar los elementos de ambas matrices y el resultado almacenarlo en una nueva matriz llamadamatrizC.

public void Suma(){
matrizC = new int[2][2];
matrizC[0][0] = matrizA[0][0] + matrizB[0][0];
matrizC[0][1] = matrizA[0][1] + matrizB[0][1];
matrizC[1][0] = matrizA[1][0] + matrizB[1][0];
matrizC[1][1] = matrizA[1][1] + matrizB[1][1];
}

Finalmente imprimimos los resultados en pantalla con el métodoImprimir().

Salida:

Psd to WordPress AUTO ACCIDENT ATTORNEY Computer science classes online personal injury law firm maritime lawyer houston Casino reviews Business VOIP Solutions Online Criminal Justice Degree asbestos exposure lawyers harddrive data recovery services motorcycle accident attorney chicago Donate Old Cars to Charity Online casino Donating a car in Maryland Mobile casino Casino Live casino Psd to html mesothelioma attorneys cheaper insurance companies How to Donate A Car in California Custom WordPress theme designer Donate a car in Maryland Best social media platforms for business WEBEX COSTS Criminal Defense Attorneys Florida Dwi lawyer Hire php developers auto accident attorney Torrance Make money online Australia Online Christmas cards mesotheioma Seo companies Photo Christmas cards Hire php developer Holland Michigan College mesothelioma attorney directory DONATE YOUR CAR FOR MONEY google affiliate WordPress theme designers AUTOMOBILE ACCIDENT ATTORNEY structured settlement broker Service business software Better conferencing calls selling structured settlement mesothelioma law suit EMAIL BULK SERVICE mesothelioma lawyer houston Massage School Dallas Texas asbestos lawyers DUI lawyer Hire php programmers CHEAP AUTO INSURANCE IN VA Html email best mesothelioma lawyers Criminal lawyer Donate your car for money Auto Mobile Shipping Quote Email Bulk Service low credit line credit cards New social media platforms best consolidation loan student Virtual Data Rooms orlando criminal attorney Sell Annuity Payment Social media strategies car insurance companies CAR INSURANCE QUOTES UTAH colorado mesothelioma lawyers Php programmers for hire HOW TO DONATE A CAR IN CALIFORNIA Criminal defense lawyer buy structured settlements DONATE CARS IN MA google adsense Donate old cars to charity Insurance Companies WebEx costs Paperport Promotional Code car accident lawyers Bankruptcy lawyer SELL ANNUITY PAYMENT Car Insurance Quotes Colorado CAR INSURANCE QUOTES PA federal criminal defense attorney Php programmers Adobe illustrator classes california motorcycle accident lawyer Met auto NEUSON hosted predictive dialers illinois law lemon Social media platforms Best social media platforms purchase structured settlements onlineclasses alcohol rehab center in florida Online colledges Business finance group Forex Trading Platform Mesothelioma Law Firm ONLINE MOTOR INSURANCE QUOTES mesothelioma lawyer virginia life insurance quotes Social media platforms for business Home Phone Internet Bundle FUTURISTIC ARCHITECTURE Business management software Seo services CAR DONATE WordPress hosting HOME PHONE INTERNET BUNDLE cash out structured settlement CAR INSURANCE IN SOUTH DAKOTA Best Seo company personal injury solicitor Cheap Auto Insurance in VA Motor Replacements structured settlement purchasers Seo company Custom Christmas cards DALLAS MESOTHELIOMA ATTORNEYS car insurance quotes mesothelioma Insurance WordPress themes for designers personal injury attorney ocala fl DONATING A CAR IN MARYLAND Social media examiner futuristic architecture truck accident attorney texas ANNUITY SETTLEMENT Social media management Tech school Car insurance quotes Colorado Attorney DONATE OLD CARS TO CHARITY dallas mesothelioma attorneys Cheap Domain Registration Hosting Christmas cards Proud Italian cook Italian cooking school Injury Lawyers Auto Accident Attorney DONATE CAR FOR TAX CREDIT pharmacist jobs in chicago Learning adobe illustrator PhD in counseling education mesothelioma information auto accident lawyer san francisco Social media tools Donate cars Illinois california law lemon how to donate a car in california Social media campaigns mesotheloma Donate car for tax credit Car Insurance Companies baltimore mesothelioma attorneys donating used cars to charity Asbestos Lawyers Donate Car to Charity California Donate Car for Tax Credit PHD on Counseling Education structered settlement Donate Cars in MA Donate Your Car Sacramento washington mesothelioma attorney injury lawyers mesothelioma symptoms caraccidentlawyer mesothelioma ct Donate Your Car for Kids Structures Annuity Settlement Online Colleges Annuity Settlements Nunavut Culture Cheap Car Insurance for Ladies Dayton Freight Lines motorcycle lawyer los angeles mesothelioma lawyers san diego Car Insurance Quotes Utah Hard drive Data Recovery Services Car Insurance Quotes PA Donate a Car in Maryland Mortgage Adviser colorado auto accident attorney Donating a Car in Maryland emergency response plan ppt Donate Cars Illinois Best Criminal Lawyers in Arizona buying structured settlements Life Insurance Co Lincoln tennessee mesothelioma lawyer Online Motor Insurance Quotes auto accident attorney boulder personal injury lawyers quotes car Online Classes HARDDRIVE DATA RECOVERY SERVICES World Trade Center Footage Personal Injury Lawyer Car insurance quotes Utah car accident lawyers west palm beach Psychic for Free Low Credit Line Credit Cards Dallas Mesothelioma Attorneys Car Insurance Quotes MN Donate your Car for Money CAR ACCIDENT LAWYERS motor insurance quotes Register Free Domains Met Auto Forensics Online Course MASSAGE SCHOOL DALLAS TEXAS Donating Used Cars to Charity Neuson REGISTER FREE DOMAINS Royalty Free Images Stock Psychic for free Car Insurance in South Dakota saskatchewan auto insurance CHEAP CAR INSURANCE FOR LADIES compare life assurance illinois mesothelioma lawyer Webex Costs Cheap Car Insurance in Virginia Better Conference Calls Futuristic Architecture ashely madis Dallas mesothelioma attorneys Car Donate Online College Course mesothelioma lawyer california Car Accident Lawyers Automobile Accident Attorney business email web hosting mesothelioma lawyer asbestos cancer lawsuit Online motor insurance quotes car crash attorneys Data Recovery Raid Criminal lawyer Miami Motor Insurance Quotes Personal Injury Lawyers Car Insurance Quotes ASBESTOS LAWYERS average mesothelioma settlement mesothelioma law firm Asbestos Lung Cancer Online Stock Trading

No hay comentarios:

Publicar un comentario