jueves, 13 de octubre de 2011

Programas de Programacion Orientada a Objetos.


PROGRAMAS DE PROGRAMACION ORIENTADA A OBJETOS 

                                             Programa #1

 Realizar una clase que permita realizar la suma de dos números.

Clase Sumador.
package packagesumador;
public class Sumador {
    //atributos-variables de instancia.
    private int num1;
    private int num2;
    //metodos (constructor-convencional).
    public Sumador(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }
    public Sumador()
    {
        this.num1=0;
        this.num2=0;
    }
    public int GenerarSuma()
    {
        int sum=this.num1 + this.num2;
        return sum;
    }
}
Clase Principal
package packagesumador;

import java.io.*;
public class Principal {
// objetos leer datos teclado.
    public  static InputStreamReader Leer = new InputStreamReader(System.in);
    public static BufferedReader Teclado = new BufferedReader (Leer);
    public static void main(String[] args)
    throws IOException{
        System.out.print("Ingrese valor 1: ");
        int valor1= Integer.parseInt(Teclado.readLine());

        System.out.print("Ingrese valor 2: ");
        int valor2= Integer.parseInt(Teclado.readLine());

        Sumador Obj1 = new Sumador(valor1,valor2);

        int resu1 = Obj1.GenerarSuma();
        System.out.println("Resultado =" + resu1);
    }
}
Ejecución:
 
                                                       Programa #2

Implementar un programa en el cual se implemente un proceso que permita leer 10 valores numéricos desde el teclado, verificar cuales  y cuántos son números perfectos.

Nota.-un numero perfecto es igual a la suma de sus divisiones excepto así  mismo.

Clase  perfecto

package PACKNUMPERF;
public class PERFECTO {
    private int valor;
    public PERFECTO(){
        this.valor=0;
    }
    public boolean VerificarPerfecto(int valor)
    {
        this.valor=valor;
        int sum=0,t=1;
        while(t<this.valor)
        {
            if(this.valor % t == 0)
                sum+=t;
            t++;
        }
        if(sum==this.valor)
            return true;
        else
            return false;
    }
}

Clase Principal

package PACKNUMPERF;

import java.io.*;
public class PRINCIPAL {

    public static InputStreamReader Leer = new InputStreamReader (System.in);
    public static BufferedReader Teclado = new BufferedReader (Leer);
    public static void main(String[] args)throws IOException {
      System.out.println("ingresar 10 valores.....!");
      int num=0,cont=0;
      PERFECTO miercoles = new PERFECTO();
      for(int i=1;i<=10;i++){
          num = Integer.parseInt(Teclado.readLine());
          if(miercoles.VerificarPerfecto(num)){

                  System.out.println("Numero Perfecto=" + num);
      cont++;}
    }
System.out.println("Existen numeros perfecto: " + cont);
}

}
Ejecución.

                                                    Programa #3

Implementar una clase que permita leer 10 valores numéricos aleatorios, determinar cuáles de ellos son números primos.

Nota.- Un número primo es aquel que es divisible para 1 y para sí mismo…Números aleatorios aquel valor que es generado al azar…

Clase aleatorio.

package packaleatorio;
public class Aleatorio {
private int valor;

public Aleatorio(){

           this.valor=0;
}

    public void setValor(int valor) {
        this.valor = valor;
    }
public boolean VerificarSiNoPrimo()
{
    int cont=0;
    for (int p=1; p<=this.valor; p++)
    {

        if (this.valor % p ==0)
            cont++;
    }
    if (cont<=2)
        return true;
    else
        return false;
}
}
Clase Principal

package packaleatorio;

import java.util.Random;
public class principal {

    public static void main(String[] args) {
        // TODO code application logic here

        Random rnd =new Random();
        Aleatorio viernes = new Aleatorio();
        for(int i=1; i<=10; i++)
        {
        int num = rnd.nextInt(30);
        viernes.setValor(num);
        if(viernes.VerificarSiNoPrimo())

             System.out.println("Es un Numero Primo:  " + num);
        else
             System.out.println("No es un Numero Primo:  " + num);
   
        }
    }
}
Ejecución:




                                                      Programa #4

Implementar una clase que permita calcular la distancia entre dos puntos.

Formula: 
  
Clase Punto
package punto;

public class Punto {
    private int x1;
    private int x2;
    private int y1;
    private int y2;

    public Punto(int x1, int x2, int y1, int y2){
        this.x1=x1;
        this.x2=x2;
        this.y1=y1;
        this.y2=y2;
    }
    public double res(int x1,int x2,int y1,int y2){
       
        int r1=this.x2-this.x1;
        int r2=this.y2-this.y1;
         r1=r1*r1;
        r2=r2*r2;
        int r3=r1+r2;
        double res=Math.sqrt(r3);
        return res;


    }
}
Clase Principal


package punto;
  import java.io.*;
public class Principal {


    public static InputStreamReader Leer=new InputStreamReader(System.in);
    public static BufferedReader Teclado = new BufferedReader(Leer);
    public static void main(String[] args) throws IOException {
      System.out.println("ingrese x2");
      int x2=Integer.parseInt(Teclado.readLine());
      System.out.println("Ingrese x1 ");
      int x1=Integer.parseInt(Teclado.readLine());

       System.out.println("Ingrese y2 ");
      int y2=Integer.parseInt(Teclado.readLine());
       System.out.println("Ingrese y1 ");
      int y1=Integer.parseInt(Teclado.readLine());

      Punto obj1=new Punto( x1, x2, y1, y2);
     double p1=obj1.res(x1, x2, y1, y2);
      System.out.println("la distancia entre los dos puntos es  "+p1);

    }

}
Ejecución: 
 
Programa #5
Implementar una clase que permita realizar una cuenta de ahorro.

Clase Cuenta
private double saldo;

    public cuenta(double saldo) {
        this.saldo = saldo;
    }

  

   
    public double depositar(double deposito){
      
        this.saldo=deposito+this.saldo;
        return this.saldo;
    }
   
    public double retirar(double reti){
       this.saldo=this.saldo-reti;
        return this.saldo;
    }
}
Clase Principal

package pckbanco;

import java.io.*;
public class principal {

    public static InputStreamReader leer=new InputStreamReader(System.in);
    public static BufferedReader teclado=new BufferedReader(leer);

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

        System.out.println("Ingrese su numero de cuenta");
        int cuenta=Integer.parseInt(teclado.readLine());
        System.out.println("Ingrese su saldo actual");
       double saldoact=Integer.parseInt(teclado.readLine());


  Objeto para depositar.
          System.out.println("Desea depositar[1/0]  ");
         double deposit=Integer.parseInt(teclado.readLine());
          if(deposit==1){
              System.out.println("Ingrese monto a depositar");
              double monto=Integer.parseInt(teclado.readLine());
                  cuenta obj1=new cuenta(saldoact);
                  double respa=obj1.depositar(monto);
                  System.out.println("Su saldo actual es"+respa);
              }
          else

              System.out.println("Operacion no realizada");


  Objeto para retirar.
         System.out.println("Desea retirar [1/0]");
         int re=Integer.parseInt(teclado.readLine());
         if(re==1){
          System.out.println("Ing monto a retirar");
          double monto1=Integer.parseInt(teclado.readLine());
          while(monto1<=saldoact){
              cuenta obj2= new cuenta(saldoact);
              double repta=obj2.retirar(deposit);
              System.out.println("Su saldo actual es :"+repta);
          }
          }
         else
             System.out.println("operacion no realizada");
        
    }


}
Ejecución:

 

Programa #6


Implementar una clase que realice el velocímetro de un vehículo.

Clase Vehículo.

package velocimetro;

public class Vehiculo {
   
    private  int veh;
   
    public Vehiculo(int veh) {
        this.veh = veh;
     
       
   
    }
    public int arrancar(int ace){
      this.veh=ace;
        return this.veh;
    }
   
     public int frenar(int fre){
  
        this.veh=this.veh-fre;
       return this.veh;
    
    }  
      public int acelerar(int ace){
  
        this.veh=this.veh+ace;
       return this.veh;
}
}
Clase Principal.

package velocimetro;


import java.io.*;

public class Principal {
public static InputStreamReader Leer = new InputStreamReader(System.in);
    public static BufferedReader Teclado = new BufferedReader(Leer);
    public static void main(String[] args)throws IOException {
 
       
        System.out.println("El auto esta estacionado");
   
    System.out.println("desea arrancar[1/0]");

    double arran=Integer.parseInt(Teclado.readLine());
    if(arran==1){
    System.out.println("ingrese la velocidad con la que desea arrancar");
   
    int ac = Integer.parseInt(Teclado.readLine());
   
        System.out.println(ac+"km/h");
     Vehiculo obj1 = new Vehiculo(ac);
     int ar=obj1.arrancar(ac);
     System.out.println("su velosidad es"+ar);
 
  
   
    System.out.println("desea acelerar[1/0]");

    int ace=Integer.parseInt(Teclado.readLine());
    if(ace==1){
   
    System.out.println("ingrese la cantidad que  desea acelerar");
     int a=Integer.parseInt(Teclado.readLine());
         
     int acel=obj1.acelerar(a);
     ac=acel;
   
   System.out.println(ac+"km/h");
    
       System.out.println("su velocidad es "+ac);
     }
    System.out.println("desea frenar [1/0]");
     int f=Integer.parseInt(Teclado.readLine());
    if(f==1){
    System.out.println("cuantos km/h desea frenar]");
     int d=Integer.parseInt(Teclado.readLine());
     if(d<=ac){
    int r=obj1.frenar(d);
       ac=r;
    
    System.out.println(ac+"km/h");}
       System.out.println("su velocidad es actual es"+ac);
       if(ac==0) System.out.println("El vehiculo esta estacionado");
      
     }else System.out.println("no se puede frenar");
   
    }}}
Ejecución.



                                                       Programa #7

Implementar una clase que permita que permita realizar el exponente de un numero por medio de sumas. Ejemplo.
    
Clase Exponente  

package exponentepormediodesumas;


public class Exponente {

private int num;
private int exp;

    public Exponente(int num, int exp) {
        this.num = num;
        this.exp = exp;
    }

public void setvalor (int num, int exp) {
        this.num = num;
        this.exp = exp;
    }

public int Exponent(){
int a=0,b=1;
for(int i=0;i<num;i++){a=0;
for(int j=0;j<exp;j++){
a+=b;
}
b=a;
}
return(b);
}}

Clase Principal

package exponentepormediodesumas;

import java.io.*;
public class Principal {

   public static InputStreamReader Leer=new InputStreamReader(System.in);
public static BufferedReader Teclado=new BufferedReader(Leer);

    public static void main(String[] args)throws IOException {
        System.out.println("Ingrese la base ");
    int a=Integer.parseInt(Teclado.readLine());
    System.out.println("Ingrese la potencia ");
    int b=Integer.parseInt(Teclado.readLine());
    Exponente obj1=new Exponente(a,b);
    int result=obj1.Exponent();
    System.out.println("el resultado es:   "+result);
    }

}
Ejecución



Programa#8

Implementar una clase  realizar una división por medio de restas.

Clase división
package dividirxrestas;


public class divicion {

private int n1;
private int n2;

    public divicion(int n1, int n2) {
        this.n1 = n1;
        this.n2 = n2;
    }
 public void setvalor (int n1, int n2) {
        this.n1 = n1;
        this.n2 = n2;
    }

public int divi(int n1,int n2){
int  i=0;
while(this.n1>=this.n2){
this.n1=this.n1-this.n2;
    i++;
    }
return(i);


}
}
Clase Principal

import java.io.*;
public class Principal {

    public static InputStreamReader Leer=new InputStreamReader(System.in);
public static BufferedReader Teclado=new BufferedReader(Leer);

    public static void main(String[] args)throws IOException {
           System.out.println("DIVIDIR DOS NUMEROS POR MEDIO DE RESTAS");
       System.out.println("Ingrese el divisor ");
    int c=Integer.parseInt(Teclado.readLine());
    System.out.println("Ingrese el dividendo ");
    int d=Integer.parseInt(Teclado.readLine());
     divicion obj1=new divicion(c,d);
    int r=obj1.divi(c, d);
    System.out.println("El resultado es:   "+r);


}
}
Ejecución


Programa#9

Implementar una clase que permita realizar conversiones de binario a decimal y octal.

Clase  Transformación
package Packtransformacion;

public class Transformacion {

    private int numero;

    public Transformacion() {
        this.numero = 0;
           
    }

    public void setValor(int numero) {
        this.numero = numero;
    }
   
   
   
   
    public int [] TransformarBinario(){
    int b=0;
    int []bin=new int[20];
    while (this.numero>0){
        bin[b]=this.numero%2;
        this.numero=this.numero/2;
    b++;
    }
    while (b<20){
        bin[b]=2;
    b++;
    }
    return bin;
    }
   
    public int [] TransformarOctal(){
    int b=0;
    int []o=new int[20];
    while (this.numero>0){
        o[b]=this.numero%8;
        this.numero=this.numero/8;
    b++;
    }
    while (b<20){
        o[b]=8;
    b++;
    }
    return o;
    }}
   Clase Principal

package Packtransformacion;

import java.io.*;

public class Principal {


 public static InputStreamReader Leer = new InputStreamReader(System.in);
 public static BufferedReader Teclado = new BufferedReader(Leer);

    public static void main(String[] args)throws IOException
    {
        int []b= new int [20];
        int []o= new int [20];
            Transformacion bin = new Transformacion();
        System.out.print("Ingrese valor: ");
        int valor = Integer.parseInt(Teclado.readLine());
        bin.setValor(valor);
        System.out.print("Precione 1 para transformar a binario o 2 para transformar a octal:");
        int v = Integer.parseInt(Teclado.readLine());
     
        switch(v){
           
            case 1:
        b=bin.TransformarBinario();
      for (int i=19;i>=0;i--){
          if (b[i]!=2){
       System.out.print(b[i]); }}
       break;  
            case 2:
      o=bin.TransformarOctal();
      for (int i=19;i>=0;i--){
          if (o[i]!=8){
       System.out.print(o[i]); }}
        break;
       }     
    }
}
Ejecución







Programa#10

Implementar una clase que permita comprobar la teoría de los conjuntos

TEORÍA DE LOS CONJUNTOS
UNION:
En la teoría de conjuntos, la unión de dos (o más) conjuntos es una operación que resulta en otro conjunto cuyos elementos son los elementos de los conjuntos iniciales. Por ejemplo, el conjunto de los números naturales es la unión del conjunto de los números pares positivos P y el conjunto de los número impares positivos I:
P = {2, 4, 6, ...}
I = {1, 3, 5, ...}
N = {1, 2, 3, 4, 5, 6, ...}

INTERCESSION
En teoría de conjuntos, la intersección de dos (o más) conjuntos es una operación que resulta en otro conjunto que contiene los elementos comunes a los conjuntos de partida. Por ejemplo, dado el conjunto de los números pares P y el conjunto de los cuadrados C de números naturales, su intersección es el conjunto de los cuadrados pares D :
P = {2, 4, 6, 8, 10,...}
C = {1, 4, 9, 16, 25, ...}
D = {4, 16, 36, 64, ...}

DIFERENCIA
En teoría de conjuntos, la diferencia entre dos conjuntos es una operación que resulta en otro conjunto, cuyos elementos son todos aquellos en el primero de los conjuntos iníciales que no estén en el segundo. Por ejemplo, la diferencia entre el conjunto de los números naturales N y el conjunto de los números pares P es el conjunto de los números que no son pares, es decir, los impares I:
N = {1, 2, 3, 4, 5, ...}
P = {2, 4, 6, 8,...}
I = {1, 3, 5, 7, ...}

COMPLEMENTO
El conjunto complementario de un conjunto dado es otro conjunto que contiene todos los elementos que no están en el conjunto original. Para poder definirlo es necesario especificar qué tipo de elementos se están utilizando, o de otro modo, cuál es el conjunto universal.
P = {2, 3, 5, 7, ...}
C = {1, 4, 6, 8, 9,...}

Clase Conjuntos

package Packrandom;
import java.io.*;
import java.util.Random;
public class Conjuntos {
    public static InputStreamReader Leer = new InputStreamReader (System.in);
        public static BufferedReader Teclado = new BufferedReader (Leer);
     private int [] a=new int [5];
     private int [] b=new int [5];
    public Conjuntos() {
        this.a[0]=0;
        this.b[0]=0;
    }
    public void LLenar(){
        int n=0,t=0;
    Random rnd =new Random();
while(t<5){
    int r=0;
    n = rnd.nextInt(10);
        for(int i=0;i<5;i++){
       if(this.a[i]!=n){
   r++;
    }  }
    if(r==5){
      a[t]=n; 
        t++;}
        }
int o=0;
    while(o<5){
    int r=0;
    n = rnd.nextInt(10);
    for(int i=0;i<5;i++){
       if(this.b[i]!=n){
   r++;
    }  }
    if(r==5){
      b[o]=n; 
        o++;}
        } }
public void Mostrar() throws IOException{
        System.out.println("PRIMER VECTOR ");
    for(int i=0;i<5;i++){
    System.out.println(this.a[i]);
    }
    System.out.println("SEGUNDO VECTOR ");
    for(int o=0;o<5;o++){
    System.out.println(this.b[o]);
    }
    }
   public void Producto(){
    int [] c=new int [5];

    for (int h=0;h<5;h++){
    for(int i=0;i<5;i++){
     c[h]+=this.a[h]*this.b[i];
    }  }
 
   
   for (int h=0;h<5;h++){
      System.out.println(c[h]);
      }}
   public void iguales()throws IOException{
       int []l=new int[10];
int u=0;
     for (int h=0;h<5;h++){
    for(int i=0;i<5;i++){
        if(this.a[h]==this.b[i]){
        l[u]=this.a[h];
      u++;break;
        }  } }
 System.out.println("LOS NUMEROS REPETIDOS EN LOS DOS VECTORES SON:");
    for (int h=0;h<10;h++){
        if(l[h]!=0){
        System.out.println(l[h]);
    } }
   }
      public void Union()throws IOException{
       int l=0;
       int []f=new int[5];
       for (int h=0;h<5;h++){
           f[h]=b[h];}
  for (int h=0;h<5;h++){    for(int i=0;i<5;i++){
        if(this.a[h]==f[i]){
        f[i]=11;
        l++;
        }  } }
int [] c=new int [10-l];     int i;
      for( i=0;i<5;i++){if(i<=5){
          c[i]=a[i];}
      }int v=5;
        for(int m=0;m<5;m++){
      if(f[m]<11){
          c[v]=f[m];v++;}}
         System.out.println("UNION DE  VECTOR ");
    for(int d=0;d<10-l;d++){
    System.out.println(c[d]);
    }}}

Clase Principal

package Packrandom;

import java.io.*;
public class Principal {
    public static InputStreamReader Leer = new InputStreamReader (System.in);
        public static BufferedReader Teclado = new BufferedReader (Leer);

    public static void main(String[] args) throws IOException {
        Conjuntos miercoles = new Conjuntos ();
       miercoles.LLenar();
       miercoles.Mostrar();
      System.out.println("PRODUCTO DE PLANO");
     
       miercoles.Producto();
miercoles.iguales();
       System.out.println("UNION");
     
     
 miercoles.Union();  
    }

}

Ejecución






PROGRAMACION GRAFICA

Programa#11

Mediante la programación grafica implementar una clase  que permita dibujar el Área y distancia de un circulo.

Clase Cálculos

package appdibujarcirculocalculararea;


public class Calculos
{
    private int Xo,Yo;
    private int X1,Y1;
   
    public Calculos(int Xo, int Yo, int X1, int Y1)
    {
        this.Xo = Xo;
        this.Yo = Yo;
        this.X1 = X1;
        this.Y1 = Y1;
    }
    private double DistanciaPuntos()
    {
        double d = Math.sqrt((Math.pow((this.X1 - this.Xo), 2)) + (Math.pow((this.Y1 - this.Yo), 2)));
        return d;
    }
    public double AreaCirculo()
    {
       double distancia = this.DistanciaPuntos();
       double radio = distancia / 2;
       double area = Math.PI * Math.pow(radio, 2);
       return area;
    }
   
}
Clase Panel


package appdibujarcirculocalculararea;

import java.awt.Graphics;
import java.awt.*;


public class Panel extends javax.swing.JPanel {

    private int Xo,Yo;
    private int X1,Y1;

    public void setX1(int X1) {
        this.X1 = X1;
    }

    public void setXo(int Xo) {
        this.Xo = Xo;
    }

    public void setY1(int Y1) {
        this.Y1 = Y1;
    }

    public void setYo(int Yo) {
        this.Yo = Yo;
    }


    public Panel() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
   
                 

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.red);
        g.drawOval(this.Xo, this.Yo, this.X1, this.Y1);
       
        Calculos Obj = new Calculos(this.Xo, this.Yo, this.X1, this.Y1);
        double a = Obj.AreaCirculo();
        String cadena = "Area = " + String.valueOf(a);
        g.drawString(cadena, 10, 15);
    }
    public void DibujarCirculo()
    {
      repaint();
    }



    // Variables declaration - do not modify                    
    // End of variables declaration                  

}

Clase  JFrame(Principal)

Fuente.
package appdibujarcirculocalculararea;


public class Principal extends javax.swing.JFrame {

    /** Creates new form Principal */
    public Principal() {
        initComponents();
    }

   
    @SuppressWarnings("unchecked")
private void btngraficarActionPerformed(java.awt.event.ActionEvent evt) {                                           
       int Xo = Integer.parseInt(this.textX0.getText());
       int Yo = Integer.parseInt(this.texty0.getText());
       int X1 = Integer.parseInt(this.textX1.getText());
       int Y1 = Integer.parseInt(this.texty1.getText());

       panel1.setXo(Xo);
       panel1.setYo(Yo);
       panel1.setX1(X1);
       panel1.setY1(Y1);

       panel1.DibujarCirculo();

    }      
/**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JButton btngraficar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private appdibujarcirculocalculararea.Panel panel1;
    private javax.swing.JTextField textX0;
    private javax.swing.JTextField textX1;
    private javax.swing.JTextField texty0;
    private javax.swing.JTextField texty1;
    // End of variables declaration                  

}
Diseño.           

 
Ejecución.




 
Programa #12

Implementar una clase mediante la programación grafica permita dibujar la ecuación de le recta.

Clase EcuacionRecta

package ecuaciondelarecta;


public class EcuacionRecta {
    private int X1,Y1;
    private int X2,Y2;

    public EcuacionRecta(int X1, int Y1, int X2, int Y2) {
        this.X1 = X1;
        this.Y1 = Y1;
        this.X2 = X2;
        this.Y2 = Y2;
    }
  
     public double Distancia()
    {
         int X=this.X2-this.X1;
        int Y=this.Y2-this.Y1;
         X=X*X;
        Y=Y*Y;
        int r3=X+Y;
        double res=Math.sqrt(r3);
        return res;
    }
     public double CordenadasEnX(){
         double X=(this.X2-this.X1)/2;
         return X;
    
     }
     public double CordenadasEnY(){
         double Y=(this.Y2-this.Y1)/2;
         return Y;

     }
    public double PuntoMedio()
    {
      
      double c=(this.Distancia());
      double s=(c/2);
      return s;
     
    }

}



Clase Panel
package ecuaciondelarecta;

import java.awt.Graphics;
import java.awt.*;
public class Panel extends javax.swing.JPanel {
    private int X1,Y1;
    private int X2,Y2;

    public void setX1(int X1) {
        this.X1 = X1;
    }

    public void setX2(int X2) {
        this.X2 = X2;
    }

    public void setY1(int Y1) {
        this.Y1 = Y1;
    }

    public void setY2(int Y2) {
        this.Y2 = Y2;
    }

  
    public Panel() {
        initComponents();
    }

 
    @SuppressWarnings("unchecked")
                    

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.gray);
        g.drawLine(this.X1,this.Y1,this.X2,this.Y2);

        EcuacionRecta recta=new EcuacionRecta(this.X1,this.Y1,this.X2,this.Y2);
        double dist=recta.Distancia();
        double X=recta.CordenadasEnX();
        double Y=recta.CordenadasEnY();
        double pun=recta.PuntoMedio();
      

       
         String cadena1="La distancia de la recta es:" + String.valueOf(dist);
         g.drawString(cadena1, 10,15);
         String cadena2="Las cordenadas en X es:" + String.valueOf(X);
         g.drawString(cadena2, 10,35);
         String cadena3="Las cordenadas en Y es:" + String.valueOf(Y);
         g.drawString(cadena3, 10,55);
         String cadena4="El punto medio es:" + String.valueOf(pun);
         g.drawString(cadena4, 10,75);
      
    }
 public void DibujarRecta(){
        repaint();

    // Variables declaration - do not modify                    
    // End of variables declaration                  

}}
Clase JFrame(Principal)

Fuente
package ecuaciondelarecta;


public class Principal extends javax.swing.JFrame {

   
    public Principal() {
        initComponents();
    }

   
    @SuppressWarnings("unchecked")
   
    private void CalcularActionPerformed(java.awt.event.ActionEvent evt) {                                        
      int X1=Integer.parseInt(this.txtX1.getText());
      int Y1=Integer.parseInt(this.txtY1.getText());
      int X2=Integer.parseInt(this.txtX2.getText());
      int Y2=Integer.parseInt(this.txtY2.getText());


panel1.setX1(X1);
panel1.setY1(Y1);
panel1.setX2(X2);
panel1.setY2(Y2);

panel1.DibujarRecta();
    }                                       

  
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JButton Calcular;
    private javax.swing.JPanel Panel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private ecuaciondelarecta.Panel panel1;
    private javax.swing.JTextField txtX1;
    private javax.swing.JTextField txtX2;
    private javax.swing.JTextField txtY1;
    private javax.swing.JTextField txtY2;
    // End of variables declaration                  

}

Diseño


 
Ejecución.



HERENCIA

Programa # 13

 Implementar un programa de Herencia. Y como se trata de una herencia contamos con varias clases normales y una principal.

Clase empleado
public class Empleado extends Persona
{
   private String Cargo;
   private String Departamento;
  
    public Empleado(String Cargo, String Departamento,String Cedula, String Nombres, int Edad) {
        super(Cedula, Nombres, Edad);
        this.Cargo = Cargo;
        this.Departamento = Departamento;
    }

    @Override
    public void DatosInformativos() {
        super.DatosInformativos();
        System.out.println("Cargo: " + this.Cargo);
        System.out.println("Departamento: " + this.Departamento);
    }
   
     public String CalcularSueldo(int numHoras, double valorHora)
    {
      double sueldo = numHoras * valorHora;
      String cadSueldo = " Sueldo: " + String.valueOf(sueldo);
      return cadSueldo;
    }
}

Clase estudiante
public class Estudiante extends Persona
{
    private String Colegio;
    private String Semestre;
    private String Especialidad;

    public void setEspecialidad(String Especialidad) {
        this.Especialidad = Especialidad;
    }
    public Estudiante(String Colegio, String Semestre, String Cedula, String Nombres, int Edad) {
        super(Cedula, Nombres, Edad);
        this.Colegio = Colegio;
        this.Semestre = Semestre;
    }

    @Override
    public void DatosInformativos() {
        super.DatosInformativos();
        System.out.println("Colegio: " + this.Colegio);
        System.out.println("Semestre: " + this.Semestre);
        System.out.println("Especialidad: " + this.Especialidad);
    }  
}

Clase persona
public class Persona
{
    private String Cedula;
    private String Nombres;
    private int Edad;

    public Persona(String Cedula, String Nombres, int Edad) {
        this.Cedula = Cedula;
        this.Nombres = Nombres;
        this.Edad = Edad;
    }
   
    public void DatosInformativos()
    {
      System.out.println("Cedula Identidad: " + this.Cedula);
      System.out.println("Nombres: " + this.Nombres);
      System.out.println("Edad: " + this.Edad);
    }
}

Clase principal
public class Principal {

   
    public static void main(String[] args)
    {
        System.out.println("Creando Objeto Empleado....!");
        //String Cargo, String Departamento,long Cedula, String Nombres, int Edad
        Empleado Juan = new Empleado("Director", "Sistemas","12034576891","Mariuxi Lopez",35);
        Juan.DatosInformativos();
        System.out.println(Juan.CalcularSueldo(60,18));
       
        System.out.println("Creando Objeto Estudiante....!");
        //String Colegio, String Semestre, String Cedula, StriSng Nombres, int Edad
        Estudiante Pedro = new Estudiante("Monterrey","Octavo","1207895431","Carlos Torres", 15);
        Pedro.setEspecialidad("Telecomunicacion");
        Pedro.DatosInformativos();
    }
}

Ejecución




 

Programación Orientada a Objetos © 2008. Design By: SkinCorner