Documentos de Académico
Documentos de Profesional
Documentos de Cultura
INTEGRANTES:
JOHN LEIVA
LENIN GOMEZ
CUADERNO DE PROGRAMACION
II SEGUNDO PARCIAL
ENERO 201
Captulo 3: Recursividad----------------------------------------------------------------------3
Captulo 4: Arrays (Arreglos)-----------------------------------------------------------------4
Contenido
Captulo 3: Recursividad................................................................................. 3
Captulo 4: Arrays (Arreglos)..........................................................................4
Captulo 5: Agregacin y Composicion...........................................................4
DEBERES..................................................................................................... 7
EJERCICIOS................................................................................................... 14
Captulo 3: Recursividad
3.1 Defi nicin:
Primero debemos decir que la recursividad no es una estructura de datos,
sino que es una tcnica de programacin que nos permite que un bloque de
instrucciones se ejecute n veces. Remplaza en ocasiones a estructuras
repetitivas.
La recursividad es un concepto difcil de entender en principio, pero luego
de analizar diferentes problemas aparecen puntos comunes.
En Java los mtodos pueden llamarse a s mismos. Si dentro de un mtodo
existe la llamada a s mismo decimos que el mtodo es recursivo.
Cuando un mtodo se llama a s mismo, se asigna espacio en la pila para las
nuevas variables locales y parmetros.
Al volver de una llamada recursiva, se recuperan de la pila las variables
locales y los parmetros antiguos y la ejecucin se reanuda en el punto de
la llamada al mtodo.
3.2 Ejemplo:
Por ejemplo, para escribir un mtodo que calcule el factorial de un nmero entero
no negativo, podemos hacerlo a partir de la definicin de factorial:
Si n = 0 entonces
0! = 1
si n>0 entonces
n! = n * (n1) * (n2) * ... * 3 * 2 * 1
Esto dar lugar a una solucin iterativa en Java mediante un bucle for:
// Mtodo Java no recursivo para calcular el factorial de un nmero
public double factorial(int n){
double fact=1;
int i;
if (n==0)
fact=1;
else
for(i=1;i<=n;i++)
fact=fact*i;
return fact;
}
Pero existe otra definicin de factorial en funcin de s misma:
0! = 1
Sintaxis:
C/C++
tipo[] nombre = new tipo[tamao];
int[] a = new int[10];
int a[10];
float[] fData = new float[40];
float fData[40];
String[] sData = new String[100];
char*sData;
char sData[100];
String sData[100];(crear la clase
String en C++)
5.2 Composicion
5.2.1 Defi nicion:
Composicin es una forma fuerte de composicin donde la vida de la clase
contenida debe coincidir con la vida de la clase contenedor. Los
5.4 En cdigo
Para traducir ambas relaciones a cdigo, podemos utilizar un atributo en la
clase contenedora o compuesta donde almacenaremos una coleccin de los
objetos que la componen, y por otro lado declararemos un mtodo para
agregar elementos a la coleccin. Dependiendo del lenguaje de
programacin empleado, podemos utilizar diferentes estructuras de datos
que nos permitan almacenar esa coleccin de objetos, aunque
generalmente se utilizan arrays (arreglos) para este fin.
Veamos un ejemplo:
DEBERES
Program.java
package jappmatrices;
public class Program {
public static void main(String[] args) {
// TODO code application logic here
MatrixJ A = new MatrixJ();
MatrixJ AT = new MatrixJ();
A.Print();
System.out.printf("\n");
AT.Print();
System.out.printf("\n");
A.Read();
A.Print();
System.out.printf("\n");
//AT = A.Transpose();
AT = A.Transpose(A);
AT.Print();
System.out.printf("\n");
A.OrdenarMatrizFilas();
A.Print();
}
}
MatrixJ.java
package jappmatrices;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MatrixJ
{
int [][] M = new int[4][4];
int [] V = new int[16];
public MatrixJ()
{
int i,j;
for(i=0; i<M.length; i++)
{
for(j=0;j<M.length;j++)
M[i][j] = -1;
}
for(i=0; i<V.length; i++)
V[i] = -1;
}
public void Print()
{
int i, j;
for (i=0; i<M.length;i++)
{
System.out.printf("\n");
for(j=0; j<M.length; j++)
{
System.out.printf("%d\t", M[i][j]);
}
}
}
public MatrixJ Transpose(MatrixJ P)
{
MatrixJ T = new MatrixJ();
int i, j;
for (i=0; i < P.M.length;i++)
{
System.out.printf("\n");
for(j=0; j < P.M.length; j++)
{
T.M[j][i] = P.M[i][j];
}
}
return(T);
}
public MatrixJ Transpose()
{
MatrixJ T = new MatrixJ();
int i, j;
for (i=0; i<M.length;i++)
{
System.out.printf("\n");
for(j=0; j<M.length; j++)
{
T.M[j][i] = M[i][j];
}
}
return(T);
}
public void OrdenarArreglo()
{
int actual, sig, temp;
for(actual = 0; actual < V.length; actual++)
{
for(sig = actual+1; sig < V.length; sig++)
{
if(V[actual] > V[sig])
{
temp = V[actual];
V[actual] = V[sig];
V[sig] = temp;
}
}
}
}
public void CopiarDatosMatrizArreglo()
{
int i, j, k;
for(i = 0, k = 0; i < M.length; i++)
{
}
package JAppArrayStructured;
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
int[] claseInutil = new int[10];
readData(claseInutil);
printData(claseInutil);
}
public static void readData(int[] p) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
System.out.printf("Ingrese el dato %d: ", i+1);
p[i] = sc.nextInt();
}
}
public static void printData(int[] p) {
for (int i = 0; i < 10; i++) {
System.out.printf("%d\n", p[i]);
}
}
public static void ordenarArreglo(int[] p) {
int[] tmp = p;
for (int i = 9; i >= 0; i--)
p[10-i-1] = tmp[i];
}
}
if(matriz1[0].length == matriz2.length) {
int filas1 = matriz1.length;
int filas2 = matriz2.length;
int columnas2 = matriz2[0].length;
int matriz[][] = new int[filas1][columnas2];
int resultado = 0;
for(int i = 0; i < filas1; i++)
for(int j = 0; j < columnas2; j++) {
for(int k = 0; k < filas2; k++)
resultado += (matriz1[i][k]*matriz2[k][j]);
matriz[i][j] = resultado; resultado = 0;
}
imprimir_matriz(matriz);
} else
System.out.printf("\tNo es posible multiplicar\n");
}
public static void calcular_matriz_inversa(int matriz[][]) {
System.out.printf("Matriz inversa:\n");
int filas = matriz.length, columnas = matriz[0].length;
int matrizInversa[][] = new int[columnas][filas];
for(int i = 0; i < filas; i++)
for(int j = 0; j < columnas; j++)
matrizInversa[j][i] = matriz[i][j];
imprimir_matriz(matrizInversa);
}
public static void imprimir_matriz(int matriz[][]) {
int filas = matriz.length; int columnas = matriz[0].length;
for(int i = 0; i < filas; i++) {
for(int j = 0; j < columnas; j++)
System.out.printf("%d\t", matriz[i][j]);
System.out.printf("\n");
}
}
}
EJERCICIOS
Clase Vector:
package javaappspaceshipsgame;
import java.io.*;
public class Vector3D
{
private float mX, mY, mZ;
public float getX()
{ return mX; }
public void setX(float mX)
{ this.mX = mX; }
public float getY()
{ return mY; }
public void setY(float mY)
{ this.mY = mY; }
public float getZ()
{ return mZ; }
public void setZ(float mZ)
{ this.mZ = mZ; }
public Vector3D()
{
setX(0.0f); setY(0.0f); setZ(0.0f);
}
public Vector3D(float[] coords)
{
setX(coords[0]); setY(coords[1]); setZ(coords[2]);
}
public Vector3D(float x, float y, float z)
{
setX(x); setY(y); setZ(z);
}
public boolean Equals(Vector3D rhs)
{
return(getX() == rhs.getX() && getY() == rhs.getY() &&
getZ() == rhs.getZ());
}
public Vector3D Addition(Vector3D rhs)
{
Vector3D Temp = new Vector3D();
Temp.setX(getX() + rhs.getX());
Temp.setY(getY() + rhs.getY());
Temp.setZ(getZ() + rhs.getZ());
return Temp;
}
public Vector3D Subtraction(Vector3D rhs)
{
Vector3D Temp = new Vector3D();
Temp.setX(getX() - rhs.getX());
Temp.setY(getY() - rhs.getY());
Temp.setZ(getZ() - rhs.getZ());
return Temp;
}
public Vector3D ScalarMultiplication(float scalar)
{
Vector3D P = new Vector3D();
P.setX(getX() * scalar);
P.setY(getY() * scalar);
P.setZ(getZ() * scalar);
return P;
}
public float Lenght()
{
return((float)Math.sqrt(getX() * getX() + getY() * getY() +
getZ() * getZ()));
}
public void Normalize()
{
float len = Lenght();
setX(getX() / len); setY(getY() / len); setZ(len);
}
public float DotProduct(Vector3D rhs)
{
float DotP = getX() * rhs.getX() + getY() * rhs.getY() +
getZ() * rhs.getZ();
return DotP;
}
public void Print()
{
System.out.printf("< %f, %f, %f > \n", getX(), getY(),
getZ());
}
public void Input()
{
String sData = "";
try
{
System.out.printf("Ingrese x: ");
InputStreamReader isr = new
InputStreamReader(System.in);
Clase SpaceShip
package javaappspaceshipsgame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Admin
*/
public class SpaceShip
{
private String mFirstName;
private String mLastName;
private int mFuelLevel;
private int mDamage;
private Vector3D mPosicion = new Vector3D(0.0f, 0.0f, 0.0f);
private Vector3D mVelocity = new Vector3D(1.0f, 1.0f, 1.0f);
/**
* @return the mFirstName
*/
public String getmFirstName() {
return mFirstName;
}
/**
* @param mFirstName the mFirstName to set
*/
public void setmFirstName(String mFirstName) {
this.mFirstName = mFirstName;
}
/**
* @return the mLastName
*/
public String getmLastName() {
return mLastName;
}
/**
* @param mLastName the mLastName to set
*/
public void setmLastName(String mLastName) {
this.mLastName = mLastName;
}
/**
* @return the mFuelLevel
*/
public int getmFuelLevel() {
return mFuelLevel;
}
/**
* @param mFuelLevel the mFuelLevel to set
*/
public void setmFuelLevel(int mFuelLevel) {
this.mFuelLevel = mFuelLevel;
}
/**
* @return the mDamage
*/
public int getmDamage() {
return mDamage;
}
/**
* @param mDamage the mDamage to set
*/
public void setmDamage(int mDamage) {
this.mDamage = mDamage;
}
/**
mVelocity.getY(),
mVelocity.getZ());
}
}
Class Program
package javaappspaceshipsgame;
/**
* @author CV & MZ
*/
public class Program
{
private static SpaceShip[] ArraySS = new SpaceShip[2];
//private static SpaceShip ObjSS1;
//private static SpaceShip ObjSS2;
public static void main(String[] args)
{
Initialize();
Print();
}
public static void Initialize()
{
/*
ObjSS1 = new SpaceShip("Cesar", "Villacis", 90, 0, new
Vector3D(5.0f, 6.0f, -3.0f),
new Vector3D(1.0f, 1.0f, 0.0f));
ObjSS2 = new SpaceShip("Margarita", "Zambrano", 90, 0, new
Vector3D(5.0f, 6.0f, -3.0f),
new Vector3D(1.0f, 1.0f, 0.0f));
*/
ArraySS[0] = new SpaceShip("Cesar", "Villacis", 90, 0, new
Vector3D(5.0f, 6.0f, -3.0f),
new Vector3D(1.0f, 1.0f, 0.0f));
ArraySS[1] = new SpaceShip("Margarita", "Zambrano", 90, 0,
new Vector3D(5.0f, 6.0f, -3.0f),
new Vector3D(1.0f, 1.0f, 0.0f));
}
public static void Print()
{
/*
ObjSS1.Draw();
ObjSS1.Fly();
ObjSS1.Attack();
ObjSS1.PrintStatistics();
System.out.printf("\n");
ObjSS2.Draw();
ObjSS2.Fly();
ObjSS2.Attack();
ObjSS2.PrintStatistics();
System.out.printf("\n");
*/
ArraySS[0].Draw();
ArraySS[0].Fly();
ArraySS[0].Attack();
ArraySS[0].PrintStatistics();
System.out.printf("\n");
ArraySS[1].Draw();
ArraySS[1].Fly();
ArraySS[1].Attack();
ArraySS[1].PrintStatistics();
System.out.printf("\n");
}
}
CAPTURAS DE PANTALLA
CIRCUITO COMPLEJO
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jappconsolecomplexcircuit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author DavidAndres
*/
public class Complex {
private double mReal;
private double mImag;
public Complex(){
mReal = 0.0; mImag = 0.0;
}
public Complex(double a, double b){
mReal = a;
mImag = b;
}
public Complex(Complex C){
mReal = C.mReal;
mImag = C.mImag;
}
setReal(Double.parseDouble(sData));
}
catch(IOException e)
{
System.err.println("Error: " +e.getMessage());
}
try
{
System.out.printf("Ingrese el valor imaginario: ");
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader streamReader= new BufferedReader(isr);
sData= streamReader.readLine();
setImag(Double.parseDouble(sData));
}
catch(IOException e)
{
System.err.println("Error: " +e.getMessage());
}
}
public Complex Addition(Complex A, Complex B){
Complex temp = new Complex();
temp.setReal(A.getReal() + B.getReal());
temp.setImag(A.getImag() + B.getImag());
return(temp);
}
public Complex Subtraction(Complex A, Complex B){
Complex temp = new Complex();
temp.setReal(A.getReal() - B.getReal());
temp.setImag(A.getImag() - B.getImag());
return(temp);
}
public Complex Product(Complex A, Complex B){
Complex temp = new Complex();
temp.setReal((A.getReal() * B.getReal()) - (A.getImag() * B.getImag()));
temp.setImag((A.getReal() * B.getImag()) + (A.getImag() *
B.getReal()));
return(temp);
}
public Complex Division(Complex A, Complex B){
Complex temp = new Complex();
return(temp);
}
public double Magnitude(Complex A ){
double temp = 0.0;
temp = Math.sqrt((A.getReal()*A.getReal())+
(A.getImag()*A.getImag()));
return(temp);
}
public double Angle(Complex A ){
double temp = 0.0;
temp = Math.atan(A.getImag()/A.getReal());
return(temp);
}
public void Print() {
if(getImag() >= 0)
System.out.printf(" = %g + %g i \n", getReal(), getImag());
else
System.out.printf(" = %g %g i \n", getReal(), getImag());
}
public void PrintR(double a) {
System.out.printf(" = %g \n",a);
}
public void PrintA(double a) {
System.out.printf(" = %g grados\n",a);
}
/**
* @return the mReal
*/
public double getReal() {
return mReal;
}
/**
* @param mReal the mReal to set
*/
public void setReal(double mReal) {
this.mReal = mReal;
}
/**
* @return the mImag
*/
public double getImag() {
return mImag;
}
/**
* @param mImag the mImag to set
*/
public void setImag(double mImag) {
this.mImag = mImag;
}
} /*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jappconsolecomplexcircuit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author DavidAndres
*/
public class ComplexCircuit {
private Complex R = new Complex(0.0, 0.0);
private Complex C = new Complex(0.0, 0.0);
private Complex L = new Complex(0.0, 0.0);
private Complex Z = new Complex(0.0, 0.0);
private double f = 0.0;
private double z = 0.0;
private double fp = 0.0;
public ComplexCircuit()
{
R = new Complex(0.0, 0.0);
try
{
System.out.printf("Ingrese el valor de la Frecuencia F: ");
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader streamReader= new BufferedReader(isr);
sData= streamReader.readLine();
f = Double.parseDouble(sData);
}
catch(IOException e)
{
System.err.println("Error: " +e.getMessage());
}
try
{
R.setReal(Double.parseDouble(sData));
R.setImag(0.0);
}
catch(IOException e)
{
System.err.println("Error: " +e.getMessage());
}
try
{
System.out.printf("Ingrese el valor de la Capacitancia C: ");
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader streamReader= new BufferedReader(isr);
sData= streamReader.readLine();
C.setImag(Double.parseDouble(sData));
C.setReal(0.0);
}
catch(IOException e)
{
System.err.println("Error: " +e.getMessage());
}
try
{
System.out.printf("Ingrese el valor de la Inductancia L: ");
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader streamReader= new BufferedReader(isr);
sData= streamReader.readLine();
L.setImag(Double.parseDouble(sData));
L.setReal(0.0);
}
catch(IOException e)
{
System.err.println("Error: " +e.getMessage());
}
}
/**
*
* @author DavidAndres
*/
public class Program {
}
CAPTURAS DE PANTALLA