prime trabajo lp

Upload: wachiruphay

Post on 03-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Prime Trabajo LP

    1/18

    EJERCICIOS D IRIGIDOS

    Nota: Implemente los ejercicios utilizando Java

    1. Defina una Clase con un mtodo pblico que permite dibujar el triangulo de Floyd, ademsel mtodo tendr que recibir como argumento la altura del tringulo:

    11 21 2 31 2 3 41 2 3 4 5

    2. Implementar el siguiente ejercicio con POO12 23 3 3

    4 4 4 43 3 32 21

    package ejercicio01;

    public class Triangulo_Floyd {

    public void Triangulo_Floyd(int k){

    for (int i = 1; i

  • 8/12/2019 Prime Trabajo LP

    2/18

    3. Defina una Clase y un dos mtodo para convertir un nmero decimal a nmero binario y debinario a decimal.

    package ejercicio02;

    public class Numeros {

    public void Numeros(int k){

    for (int i = 1; i = 1; j--) {

    System.out.print(i+" ");}

    System.out.println("");

    }

    }

    }

    package ejercicio02;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class TestNumeros {static int n;

    public static void main(String[] args)throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Ingrese altura: ");

    n=Integer.parseInt(br.readLine());

    System.out.println("");

    Numeros onumero = new Numeros();

    onumero.Numeros(n);

    }

    }

  • 8/12/2019 Prime Trabajo LP

    3/18

    package ejercicio03;

    public class Binario {

    public void conversion(int k){

    int r,q, i=1, suma=0;

    int array[]=new int [10000000];q=k/2;

    r=k%2;

    array[0]=r;

    k=q;

    while(q>1){

    q=k/2;

    r=k%2;

    k=q;

    array[i]=r;

    i++;

    }array[i]=q;

    System.out.print("El numero decimal a binario es: ");

    for (int j = i; j >= 0; j--) {

    System.out.print(array[j]);

    }

    System.out.println("");

    for (int j = i; j >= 0; j--) {

    suma = suma + (int)(Math.pow(2, j)*array[j]);

    }

    System.out.println("El numero de binario a decimal es: "+suma);

    }}

    package ejercicio03;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class TestBinario {

    static int numero;

    public static void main(String[] args)throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.print("Ingrese numero: ");

    numero=Integer.parseInt(br.readLine());

    Binario obinario = new Binario();

    obinario.conversion(numero);

    }

    }

  • 8/12/2019 Prime Trabajo LP

    4/18

    4. Defina una Clase con sus respectivos mtodos que permita determinar la suma de los Nprimero trminos de la serie de Fibonacci, y mostrar sus valores considerando que N seamayor que 1. La secuencia de la serie es la siguiente: 0, 1, 1, 2, 3, 5, 8, 13, ...

    5. Defina una Clase con sus respectivos mtodos que permita determinar la cantidad dedgitos que tiene un nmero entero y adems mostrar la suma de los dgitos pares e

    impares. Considerar al cero como dgito par.

    package ejercicio04;

    public class Fibonacci {

    public void Fibonacc(int n){

    int a=0,b=1, suma=0;

    System.out.print(a+", "+b);

    suma=a+b;

    for (int i = 0; i < (n-2); i++) {

    int m=a+b;

    a=b;

    b=m;

    suma=suma+m;

    System.out.print(", "+m);

    }

    System.out.println("");

    System.out.println("la suma es: "+suma);

    }

    }

    package ejercicio04;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class TestFibonacci {static int n;

    public static void main(String[] args) throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Ingrese la cantidad de terminos: ");

    n=Integer.parseInt(br.readLine());

    Fibonacci ofibonacci = new Fibonacci();

    ofibonacci.Fibonacc(n);

    }

    }

  • 8/12/2019 Prime Trabajo LP

    5/18

    package ejercicio05;

    public class par_impar {

    public void digitos(int n){

    String w=Integer.toString(n);

    int x=w.length();System.out.println("La cantidad de digitos que tiene el numero es: "+x);

    }

    public void pares(int n){

    int suma=0;

    String w=Integer.toString(n);

    int a[]= new int [w.length()];

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

    a[i]=Integer.parseInt(w.substring(i, (i+1)));

    if(a[i]%2==0){

    suma = suma+a[i];

    }

    }

    System.out.println("La suma de numeros pares es: "+suma);}

    public void impares(int n){

    int suma=0;

    String w=Integer.toString(n);

    int a[]= new int [w.length()];

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

    a[i]=Integer.parseInt(w.substring(i, (i+1)));

    if(a[i]%2!=0){

    suma = suma+a[i];

    }

    }

    System.out.println("La suma de numeros impares es: "+suma);

    }

    }

    package ejercicio05;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class Test_par_impar {

    static int numero;

    public static void main(String[] args)throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.print("Ingrese numero: ");

    numero=Integer.parseInt(br.readLine());

    par_impar onumero = new par_impar();

    onumero.digitos(numero);

    onumero.pares(numero);

    onumero.impares(numero);

    }

    }

  • 8/12/2019 Prime Trabajo LP

    6/18

    6. Implementar el siguiente ejercicio con POO

    Mostrar los N primeros trminos de la serie: 7, 6, 8, 5, 9, 4, 10, 3,.

    Mostrar los K primeros trminos de la serie: 5, 7, 10, 14, 19,

    package ejercicio06;

    public class Series {public void serieN (int k){

    int a=7, b=6;

    System.out.print(a+", "+b);

    if(k%2==0){

    for (int i = 0; i < ((k/2)-1); i++) {

    a=a+1;

    System.out.print(", "+a);

    b=b-1;

    System.out.print(", "+b);

    }

    System.out.println("");

    }else if(k%2!=0){for (int i = 0; i < ((k/2)-1); i++) {

    a=a+1;

    System.out.print(", "+a);

    b=b-1;

    System.out.print(", "+b);

    }

    System.out.println(", "+(7+(k/2)));

    }

    }

    public void serieK(int m){

    int w=0;

    for (int i = 1; i < m; i++) {

    w=((i*i)+i+8)/2;System.out.print(w+" ,");

    }

    System.out.println("");

    }

    }

    package ejercicio06;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class TestSeries {

    static int N;

    static int K;

    public static void main(String[] args)throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Ingrese cantidad de terminos para la primera serie: ");

    N=Integer.parseInt(br.readLine());

    Series oserie = new Series();

    oserie.serieN(N);

    System.out.print("Ingrese cantidad de terminos para la segunda serie: ");

    K=Integer.parseInt(br.readLine());

    oserie.serieK(K);

    }

    }

  • 8/12/2019 Prime Trabajo LP

    7/18

    7. Defina una Clase que simule el apagn de un edificio que tiene 30 bombillas de luz y todas

    estn encendidas, la clase debe tener los siguientes mtodos mostrarEstado,

    mostrarTotalMalogradas (debe mostrar el nmero total de bombillas malogradas para su

    reposicin), MostrarTotalIntactas (debe mostrar el nmero total de bobillas buenas)

    package ejercicio07;

    public class Edificio {

    public void mostrarEstado(String a[], int b[]){

    int m=0;

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

    int x=(int)(Math.random()*2)+1;

    if(x==1){

    a[i]="Bombilla malograda";

    m++;

    }else if(x==2){

    a[i]="Bombilla Intacta";

    }

    }b[0]=m;

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

    System.out.println("Bombilla ["+(i+1)+"]: "+a[i]);

    }

    }

    public void mostrarTotalMalogradas(int b[]){

    System.out.println("Total de bombillas malogradas: "+b[0]);

    }

    public void mostrarTotalIntactas(int b[]){

    int w=30-b[0];

    System.out.println("Total de bombillas intactas: "+w);

    }

    }

    package ejercicio07;

    public class TestEdificio {

    public static void main(String[] args) {

    String array[]=new String[30];

    int b[]=new int[1];

    Edificio oedificio = new Edificio();

    oedificio.mostrarEstado(array, b);

    oedificio.mostrarTotalMalogradas(b);

    oedificio.mostrarTotalIntactas(b);}

    }

  • 8/12/2019 Prime Trabajo LP

    8/18

    8. Implemente una Clase Registro con los siguientes atributos y mtodos.

    package ejercicio08;

    public class Registro {

    private int NotaMayor;

    private int NotaMenor;

    private int Ascendente[];

    private int PorcentajeAprobados;

    private int PorcentajeDesaprobados;

    public int getNotaMayor() {

    return NotaMayor;

    }

    public void setNotaMayor(int notas[]) {

    int mayor = notas[0];

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

    if(mayornotas[i]){

    menor=notas[i];

    }}

    this.NotaMenor = menor;

    }

    public int[] getAscendente() {

    return Ascendente;

    }

  • 8/12/2019 Prime Trabajo LP

    9/18

    public void setAscendente(int[] notas) {

    for (int i = 1; i < notas.length; i++) {

    for (int j = 0; j < (notas.length-i); j++) {

    if(notas[j]>notas[j+1]){

    int aux = notas[j+1];

    notas[j+1]=notas[j];

    notas[j]=aux;

    }

    }

    }

    this.Ascendente = notas;

    }

    public int getPorcentajeAprobados() {

    return PorcentajeAprobados;

    }

    public void setPorcentajeAprobados(int notas[]) {

    int w=0;for (int i = 0; i < notas.length; i++) {

    if(notas[i]>=10.5){

    w++;

    }

    }

    this.PorcentajeAprobados = w*100/notas.length;

    }

    public int getPorcentajeDesaprobados() {

    return PorcentajeDesaprobados;

    }

    public void setPorcentajeDesaprobados(int notas[]) {int w=0;

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

    if(notas[i]

  • 8/12/2019 Prime Trabajo LP

    10/18

    9. Implemente la siguiente clase que mediante un mtodo elimine los elementos repetidos enun arreglo y adems a travs de un mtodo muestre un detalle de la cantidad de un mismonmero. Ejemplo:

    package ejercicio08;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class TestRegistro {

    public static void main(String[] args)throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int c;

    System.out.print("Ingrese cantidad de notas: ");

    c=Integer.parseInt(br.readLine());

    int notas[] = new int [c];

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

    System.out.print("Nota ["+(i+1)+"]: ");

    notas[i]=Integer.parseInt(br.readLine());

    if(notas[i]20){

    System.out.println("Ingrese una nota valida");

    i--;

    }}

    Registro oregistro = new Registro();

    oregistro.setNotaMayor(notas);

    System.out.println("La nota mayor es: "+oregistro.getNotaMayor());

    oregistro.setNotaMenor(notas);

    System.out.println("La nota menor es: "+oregistro.getNotaMenor());

    oregistro.setAscendente(notas);

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

    System.out.println("nora ["+(i+1)+"]: "+notas[i]);

    }

    oregistro.setPorcentajeAprobados(notas);

    System.out.println("El porcentaje de aprobados es: "+oregistro.getPorcentajeAprobados()+"%");

    oregistro.setPorcentajeDesaprobados(notas);System.out.println("El porcentaje de desaprobados es: "+oregistro.getPorcentajeDesaprobados()+"%");

    }

    }

  • 8/12/2019 Prime Trabajo LP

    11/18

    package ejercicio09;

    public class Elementos {

    public void Repetidos(int k[], int q[]){

    int s=0;

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

    for (int j = (i+1); j < k.length; j++) {if(k[j]==k[i]){

    k[j]=0;

    s++;

    }

    }

    q[i]=(s+1);

    s=0;

    }

    System.out.println("");

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

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

    if(k[i]!=0){

    System.out.println(k[i]);}

    }

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

    }

    public void Detalle(int k[], int q[]){

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

    if(k[i]!=0){

    System.out.println(k[i]+"---->"+q[i]);

    }

    }

    }

    }

    package ejercicio09;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class TestElementos {

    static int n;

    public static void main(String[] args)throws IOException{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Ingrese cantidad del arreglo: ");

    n=Integer.parseInt(br.readLine());

    int a[]=new int[n];

    int b[]=new int[n];

    for (int i = 0; i < n; i++) {

    System.out.print("Elemento["+(i+1)+"]: ");

    a[i]=Integer.parseInt(br.readLine());

    }

    Elementos oelemento = new Elementos();

    oelemento.Repetidos(a, b);

    oelemento.Detalle(a, b);

    }

    }

  • 8/12/2019 Prime Trabajo LP

    12/18

    10. Crear una Clase que permita ingresar un nmero y desarrollar dos mtodos numeroMayor

    y numeroMenor que lo conforma.

    package ejercicio10;

    public class Numero {

    public void numeroMayor(int n){

    String m = Integer.toString(n);

    int a[]=new int [m.length()];

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

    a[i]=Integer.parseInt(m.substring(i, i+1));

    }

    int w=a[0];

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

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

    if(wa[j]){

    w=a[j];

    }

    }

    }

    System.out.println("Cifra menor: "+w);

    }

    }

    package ejercicio10;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class TestNumero {static int n;

    public static void main(String[] args)throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Ingrese numero: ");

    n=Integer.parseInt(br.readLine());

    Numero onumero = new Numero();

    onumero.numeroMayor(n);

    onumero.numeroMenor(n);

    }

  • 8/12/2019 Prime Trabajo LP

    13/18

  • 8/12/2019 Prime Trabajo LP

    14/18

    12. El gobierno ha implementado como parte de su programa social, un subsidio familiar bajo

    la siguiente reglamentacin:

    a. Las familias que tienen hasta 2 hijos reciben S/. 70.00, las que tienen entre 3 y 5 hijos

    reciben S/. 90.00 y las que tienen 6 hijos o ms reciben S/.120.00 mensual.

    b. Si la madre de familia fuera viuda, la familia recibe S/. 20.00 adicionales.

    Determinar el monto mensual que recibir una familia de acuerdo a su propia realidadfamiliar.

    public void setCubosPerfectos(int n) {

    String m, k="";

    int suma=0;

    m=Integer.toString(n);

    int a[]=new int[m.length()];

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

    a[i]=Integer.parseInt(m.substring(i, (i+1)));

    }

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

    suma = suma + (int)(Math.pow(a[i], 3));

    }

    if(suma==n){

    k="es un Cubo Perfecto";

    }else {

    k="no es un Cubo Perfecto";

    }

    this.CubosPerfectos = k;

    }

    }

    package ejercicio11;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class TestMatematica {

    public static void main(String[] args)throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n;

    System.out.print("Ingrese numero: ");

    n=Integer.parseInt(br.readLine());

    Matematica omate = new Matematica();

    omate.setNumeroPerfecto(n);

    System.out.println("El numero "+n+" "+omate.getNumeroPerfecto());

    omate.setCubosPerfectos(n);

    System.out.println("El numero "+n+" "+omate.getCubosPerfectos());

    }

    }

  • 8/12/2019 Prime Trabajo LP

    15/18

    13. Simular la disponibilidad de asientos en un avin que consta de 15 filas, 2 lados y en cada

    lado 4 asientos por fila. Sugerencia: Para cada lado generar un nmero de filas aleatorias,

    identificando su posicin aleatoriamente; hacer lo mismo para los asientos en cada fila,

    asignado a cada posicin de asiento la letra R de Reservado. Mostrar en pantalla el

    resultado de la simulacin pudiendo ser la siguiente:

    package ejercicio12;

    public class ej_12 {

    public void proceso(int n, int m, int a, int b, int c){

    int w=0;

    if(m==1){

    if(n>=1 && n=3 && n=6){

    w=c+20;

    }

    System.out.println("La cantidad que recibe la familia es: "+w);

    }else if(m==2){

    if(n>=1 && n=3 && n=6){

    w=c;

    }System.out.println("La cantidad que recibe la familia es: "+w);

    }

    }

    }

    package ejercicio12;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class Test_Ejercicio12 {

    static int n, a=70, b=90, c=120, madre;

    public static void main(String[] args)throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Madre viuda (SI: 1 / NO: 2): ");

    madre=Integer.parseInt(br.readLine());

    System.out.print("Cantidad de Hijos: ");

    n=Integer.parseInt(br.readLine());

    ej_12 oejercicio = new ej_12();

    oejercicio.proceso(n, madre, a, b, c);

    }

    }

  • 8/12/2019 Prime Trabajo LP

    16/18

    package ejercicio13;

    public class Avion {

    public void Asientos(String a[][]){

    for (int i = 0; i < 15; i++) {for (int j = 0; j < 8; j++) {

    int x=(int)(Math.random()*2)+1;

    if(x==1){

    a[i][j]=" * ";

    }else if(x==2){

    a[i][j]=" R ";

    }

    }

    }

    System.out.println("===========RESERVACION DE ASIENTOS DE AVION==========");

    System.out.println("");

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

    System.out.println("Fila Lado Izquierdo Lado Derecho");System.out.println("====================================");

    System.out.println(" 1 2 3 4 1 2 3 4 ");

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

    for (int i = 0; i < 9; i++) {

    System.out.print(i+1+" || ");

    for (int j = 0; j < 8; j++) {

    System.out.print(a[i][j]+" ");

    }

    System.out.println("");

    }

    for (int i = 9; i < 15; i++) {

    System.out.print(i+1+"|| ");

    for (int j = 0; j < 8; j++) {

    System.out.print(a[i][j]+" ");

    }

    System.out.println("");

    }

    }

    }

  • 8/12/2019 Prime Trabajo LP

    17/18

    14. Implemente la clase Alumno con el atributo definidos en la clase (edad).

    package ejercicio13;

    public class TestAvion {

    static String a[][]=new String[15][8];

    public static void main(String[] args){

    Avion oavion = new Avion();

    oavion.Asientos(a);

    }

    }

    package ejercicio14;

    public class Alumno {

    public void calcularSuma(double edad[]){

    double suma=0;

    System.out.println("");

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

    suma = suma + edad[i];

    }

    System.out.println("La suma es: "+suma);

    }

    public void calcularPromedio(double edad[]){

    double suma=0;

    double promedio=0;

    System.out.println("");

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

    suma = suma + edad[i];

    }

    promedio = suma/edad.length;

    System.out.println("El promedio es: "+promedio);

    }

    public void calcularMayor(double edad[]){double mayor = edad[0];

    System.out.println("");

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

    if(mayor

  • 8/12/2019 Prime Trabajo LP

    18/18

    public void calcularMenor(double edad[]){

    double menor = edad[0];

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

    if(menor>edad[i]){

    menor=edad[i];

    }

    }

    System.out.println("La menor edad es: "+menor);

    }public void ordenarAsc(double edad[]){

    System.out.println("");

    for (int i = 1; i < edad.length; i++) {

    for (int j = 0; j < (edad.length-i); j++) {

    if(edad[j]>edad[j+1]){

    double aux=edad[j+1];

    edad[j+1]=edad[j];

    edad[j]=aux;

    }

    }

    }

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

    System.out.println("Edad ["+(i+1)+"]: "+edad[i]);}

    }

    }

    package ejercicio14;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class TestAlumno {

    static int n;

    public static void main(String[] args)throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Ingrese cantidad de Alumnos: ");

    n=Integer.parseInt(br.readLine());

    double edad[]=new double[n];

    for (int i = 0; i < n; i++) {

    System.out.print("Edad ["+(i+1)+"]: ");

    edad[i]=Integer.parseInt(br.readLine());

    if(edad[i]35){

    System.out.println("Un alumno no puede ser tan viejo");i--;

    }

    }

    Alumno oalumno = new Alumno();

    oalumno.calcularSuma(edad);

    oalumno.calcularPromedio(edad);

    oalumno.calcularMayor(edad);

    oalumno.calcularMenor(edad);

    oalumno.ordenarAsc(edad);

    }

    }