Post on 11-Apr-2015
DESARROLLO DE PRÀCTICA Nº 1 DE JAVA
NOMBRE: DIANA CECILIA MUÑOZ CASANOVA
1.A)ORDENA DE MAYOR A MENOR Y DE MENOR A MAYOR
public class ordenar {public static void main(String[] args) {
int[]num={3,5,9,1,2,4,5,6}; ordename(num); mostrarvectora(num);
} static void mostrarvectora(int num[]) {
int k;int l;System.out.print("Ordenar de menor a mayor: ");
for(k=0;k<num.length;k++){
System.out.print(+num[k]); }
System.out.println("=========="); System.out.println("Ordenar de mayor a menor"); for(l=7;l<num.length;l--) { System.out.print(+num[l]); }
}
static void ordename(int num[]) {
int i, j; int temp; int N=num.length;
for (i=1; i<N; i++){ for (j=N-1; j>=i;j--) {
if (num[j] < num[j-1]) {
temp = num[j]; num[j] = num[j-1]; num[j-1] = temp;
} }
} } }
CONSOLA:
-1-
Ordenar de menor a mayor: 12345569=========Ordenar de mayor a menor96554321
1.B)ORDENAR DE MENOR A MAYOR
public class ordenar {public static void main(String[] args) {
int k;int[]num={3,5,9,1,2,4,5,6};System.out.println("Ordenar de menor a mayor");
ordename(num); for(k=0;k<num.length;k++){
System.out.print(+num[k]); }
}
static void ordename(int num[]) {
int i, j; int temp; int N=num.length;
for (i=1; i<N; i++){ for (j=N-1; j>=i;j--) {
if (num[j] < num[j-1]) {
temp = num[j]; num[j] = num[j-1]; num[j-1] = temp;
} }
} }}
CONSOLA:Ordenar de menor a mayor12345569
2.A) UNIÒN DE DOS LISTAS public class mayor {
public static void main(String[] args) { int[] a={3,5,9,1,2,4,5,6}; int[] b={8,2,1,8,2,9,4,5};
System.out.println("Uniòn de dos Listas"); mostrarvectora(a); mostrarvectorb(b);
}
-2-
static void mostrarvectora(int a[]){
int i;for(i=0;i<a.length;i++){
System.out.print(+a[i]); }
}
static void mostrarvectorb(int b[]){
int i;for(i=0;i<b.length;i++){
System.out.print(+b[i]); }
}
}
CONSOLA:
Unión de dos Listas3591245682182945
2.B) INTERSECCIÓN DE DOS LISTAS
public class compara {int i;int j;
public static void main(String[] args) { int[] a={3,5,9,1,2,4,5,6}; int[] b={8,2,1,8,2,9,4,5}; ordename(a);
int k; for(k=0;k<a.length;k++){
System.out.print(+a[k]); }
System.out.println(" "); ordenabe(b); int p; for(p=0;p<b.length;p++){
System.out.print(+b[p]); }
System.out.println("La intersecciòn es:"); int[] c= new int[a.length]; for (int i=0; i<c.length; i++)
{
if(a[i]==b[i]) {
c[i]= a[i];
-3-
System.out.print(+a[i]); }
} }
static void ordename(int a[])
{ int i, j;
int temp; int N=a.length;
for (i=1; i<N; i++){ for (j=N-1; j>=i;j--) {
if (a[j] < a[j-1]) {
temp = a[j]; a[j] = a[j-1]; a[j-1] = temp;
} }
} }
static void ordenabe(int b[]) {
int i, j; int temp; int N=b.length;
for (i=1; i<N; i++){ for (j=N-1; j>=i;j--) {
if (b[j] < b[j-1]) {
temp = b[j]; b[j] = b[j-1]; b[j-1] = temp;
} }
} }
}
CONSOLA:
12345569 12245889
La intersección es:12459
-4-
2.C) RESTA DE DOS LISTAS
public class resta {int i;int j;
public static void main(String[] args) { int[] a={3,5,9,1,2,4,5,6}; int[] b={8,2,1,8,2,9,4,5};
System.out.println("Resta dos listas (a-b)"); int[] c= new int[a.length];
// Se coloca un ciclo for haciendo que una variable i tome valores // desde 0 hasta a.length-1 for (int i=0; i<c.length; i++) // En cada iteracion se resta los i-esimos elementos de a y b y se asigne // el resultado al i-esimo elemento de c {c[i]= a[i]-b[i];}
// Desplegamos el resultado: for (int j=0; j<c.length; j++) {System.out.println("c["+j+"]= "+c[j]);} }
}
CONSOLA:Resta dos listas (a-b)c[0]= -5c[1]= 3c[2]= 8c[3]= -7c[4]= 0c[5]= -5c[6]= 1c[7]= 1
-5-
4. Pasar a objetos : « El hogar es una casa que tiene una familia y una mascota ».
public class casa {
public static void main(String args[]){ // Crear objeto familia
familia familia1 = new familia("Muñoz","Bruces F-69",1234567);
familia familia2 = new familia("Zarate","Jr. Bolivar 2056",1234567);
//crear objeto mascota mascota mascota1 = new mascota("Fido",7); mascota mascota2 = new mascota("pepino",8);
System.out.println("APELLIDO DE LA FAMILIA1:" +familia1.apellido);
System.out.println("DIRECCION DE LA FAMILIA1:" +familia1.direccion);
System.out.println("TELEFONO DE LA FAMILIA1:" +familia1.telefono);
System.out.println("MASCOTA DE LA FAMILIA1:" +mascota1.nombre);
System.out.println("EDAD DE LA MASCOTA1:" +mascota1.edad); System.out.println("
"); System.out.println("APELLIDO DE LA FAMILIA2:"
+familia2.apellido); System.out.println("DIRECCION DE LA FAMILIA2:"
+familia2.direccion); System.out.println("TELEFONO DE LA FAMILIA2:"
+familia2.telefono); System.out.println("MASCOTA DE LA FAMILIA2:"
+mascota2.nombre);System.out.println("EDAD DE LA MASCOTA2:" +mascota2.edad);
}
}
-6-
public class familia extends casa{public String apellido; public String direccion; public int telefono;
public familia(String papellido, String pdireccion,int ptelefono){
this.apellido = papellido;
this.direccion = pdireccion; this.telefono = ptelefono; }
}
public class mascota extends casa {
public String nombre; public int edad;
public mascota(String pnombre, int pedad){
this.nombre = pnombre; this.edad = pedad; }
}
Consola:
APELLIDO DE LA FAMILIA1:MuñozDIRECCION DE LA FAMILIA1:Bruces F-69TELEFONO DE LA FAMILIA1:1234567MASCOTA DE LA FAMILIA1:FidoEDAD DE LA MASCOTA1:7 APELLIDO DE LA FAMILIA2:ZarateDIRECCION DE LA FAMILIA2:Jr. Bolivar 2056TELEFONO DE LA FAMILIA2:1234567MASCOTA DE LA FAMILIA2:pepinoEDAD DE LA MASCOTA2:8
-7-