miércoles, 16 de marzo de 2011

Calculadora




/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* Cal.java
*
* Created on 24-feb-2011, 13:02:17
*/

package calculadorablog;

/**
*
* @author MAURICIO HERNANDEZ
*/
public class Cal extends javax.swing.JFrame {

/** Creates new form Cal */
char operacion;
String temp;
double num1, num2, resultado;
private String numeroantes;


public Cal() {
initComponents();
}

private void EscogerOperacion(char op) throws NumberFormatException {
operacion = op;
num1 = Double.parseDouble(jTResultado.getText());
jTResultado.setText("");
// TODO add your handling code here:
}

private void NumeroDigitado(String numero) {
temp = jTResultado.getText();
numeroantes = temp;
temp = temp + numero;
jTResultado.setText(temp);
// TODO add your handling code here:
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {

jButton10 = new javax.swing.JButton();
jButton24 = new javax.swing.JButton();
jTResultado = new javax.swing.JTextField();
jRHex = new javax.swing.JRadioButton();
jRDec = new javax.swing.JRadioButton();
jROct = new javax.swing.JRadioButton();
jRBin = new javax.swing.JRadioButton();
jBA = new javax.swing.JButton();
jBB = new javax.swing.JButton();
jBC = new javax.swing.JButton();
jBD = new javax.swing.JButton();
jBE = new javax.swing.JButton();
jBF = new javax.swing.JButton();
jB7 = new javax.swing.JButton();
jB8 = new javax.swing.JButton();
jB9 = new javax.swing.JButton();
jBDividir = new javax.swing.JButton();
jB4 = new javax.swing.JButton();
jB5 = new javax.swing.JButton();
jB6 = new javax.swing.JButton();
jBMultiplicar = new javax.swing.JButton();
jB1 = new javax.swing.JButton();
jB2 = new javax.swing.JButton();
jB3 = new javax.swing.JButton();
jBRestar = new javax.swing.JButton();
jB0 = new javax.swing.JButton();
jBpunto = new javax.swing.JButton();
jBSumar = new javax.swing.JButton();
jBIgual = new javax.swing.JButton();
jBCE = new javax.swing.JButton();
jBClear = new javax.swing.JButton();
jBreturn = new javax.swing.JButton();

jButton10.setText("jButton10");

jButton24.setText("jButton24");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTResultado.setFont(new java.awt.Font("Tahoma", 0, 36));
jTResultado.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

jRHex.setText("Hex");
jRHex.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRHexActionPerformed(evt);
}
});

jRDec.setText("Dec");

jROct.setText("Oct");

jRBin.setText("Bin");

jBA.setBackground(new java.awt.Color(204, 204, 255));
jBA.setText("A");
jBA.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBAActionPerformed(evt);
}
});

jBB.setBackground(new java.awt.Color(204, 204, 255));
jBB.setText("B");

jBC.setBackground(new java.awt.Color(204, 204, 255));
jBC.setText("C");

jBD.setBackground(new java.awt.Color(204, 204, 255));
jBD.setText("D");

jBE.setBackground(new java.awt.Color(204, 204, 255));
jBE.setText("E");

jBF.setBackground(new java.awt.Color(204, 204, 255));
jBF.setText("F");

jB7.setBackground(new java.awt.Color(204, 204, 255));
jB7.setText("7");
jB7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB7ActionPerformed(evt);
}
});

jB8.setBackground(new java.awt.Color(204, 204, 255));
jB8.setText("8");
jB8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB8ActionPerformed(evt);
}
});

jB9.setBackground(new java.awt.Color(204, 204, 255));
jB9.setText("9");
jB9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB9ActionPerformed(evt);
}
});

jBDividir.setBackground(new java.awt.Color(204, 204, 255));
jBDividir.setText("/");
jBDividir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBDividirActionPerformed(evt);
}
});

jB4.setBackground(new java.awt.Color(204, 204, 255));
jB4.setText("4");
jB4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB4ActionPerformed(evt);
}
});

jB5.setBackground(new java.awt.Color(204, 204, 255));
jB5.setText("5");
jB5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB5ActionPerformed(evt);
}
});

jB6.setBackground(new java.awt.Color(204, 204, 255));
jB6.setText("6");
jB6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB6ActionPerformed(evt);
}
});

jBMultiplicar.setBackground(new java.awt.Color(204, 204, 255));
jBMultiplicar.setText("*");
jBMultiplicar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBMultiplicarActionPerformed(evt);
}
});

jB1.setBackground(new java.awt.Color(204, 204, 255));
jB1.setText("1");
jB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB1ActionPerformed(evt);
}
});

jB2.setBackground(new java.awt.Color(204, 204, 255));
jB2.setText("2");
jB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB2ActionPerformed(evt);
}
});

jB3.setBackground(new java.awt.Color(204, 204, 255));
jB3.setText("3");
jB3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB3ActionPerformed(evt);
}
});

jBRestar.setBackground(new java.awt.Color(204, 204, 255));
jBRestar.setText("-");
jBRestar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBRestarActionPerformed(evt);
}
});

jB0.setBackground(new java.awt.Color(204, 204, 255));
jB0.setText("0");
jB0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB0ActionPerformed(evt);
}
});

jBpunto.setBackground(new java.awt.Color(204, 204, 255));
jBpunto.setText(".");

jBSumar.setBackground(new java.awt.Color(204, 204, 255));
jBSumar.setText("+");
jBSumar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBSumarActionPerformed(evt);
}
});

jBIgual.setBackground(new java.awt.Color(204, 204, 255));
jBIgual.setText("=");
jBIgual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBIgualActionPerformed(evt);
}
});

jBCE.setBackground(new java.awt.Color(204, 204, 255));
jBCE.setText("CE");
jBCE.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBCEActionPerformed(evt);
}
});

jBClear.setBackground(new java.awt.Color(204, 204, 255));
jBClear.setText("C");

jBreturn.setBackground(new java.awt.Color(204, 204, 255));
jBreturn.setText("Re");
jBreturn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBreturnActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTResultado, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jROct)
.addComponent(jRDec)
.addComponent(jRBin)
.addComponent(jRHex))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jBD, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBC, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBB, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBA, javax.swing.GroupLayout.Alignment.LEADING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jB0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBpunto))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jB1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB3))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jB4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB6))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jB7)
.addComponent(jBCE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jB8)
.addComponent(jBClear))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jB9)
.addComponent(jBreturn)))))
.addComponent(jBF))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBSumar)
.addComponent(jBRestar, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
.addComponent(jBMultiplicar)
.addComponent(jBDividir, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
.addComponent(jBIgual))
.addContainerGap())
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jB1, jB2, jB3, jB4, jB5, jB6, jB7, jB8, jB9, jBA, jBB, jBC, jBCE, jBClear, jBD, jBE, jBF, jBIgual, jBMultiplicar, jBRestar, jBSumar, jBpunto, jBreturn});

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jTResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(15, 15, 15)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(jRHex)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRDec)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jROct)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRBin))
.addGroup(layout.createSequentialGroup()
.addComponent(jBA)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBB)
.addComponent(jBCE)
.addComponent(jBClear)
.addComponent(jBreturn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBC)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBD)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBF))
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jBDividir)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBMultiplicar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBRestar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBSumar))
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB7)
.addComponent(jB8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jB9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB4)
.addComponent(jB5)
.addComponent(jB6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB1)
.addComponent(jB2)
.addComponent(jB3))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB0)
.addComponent(jBIgual)
.addComponent(jBpunto))))
.addContainerGap())
);

pack();
}//


private void jBAActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jBCEActionPerformed(java.awt.event.ActionEvent evt) {
num2 = 0;
jTResultado.setText(String.valueOf(num1));
// TODO add your handling code here:
}

private void jB0ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("0");
}

private void jRHexActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jB2ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("2");
// TODO add your handling code here:
}

private void jB1ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("1");
// TODO add your handling code here:
}

private void jB3ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("3");
// TODO add your handling code here:
}

private void jB4ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("4");
// TODO add your handling code here:
}

private void jB5ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("5");
// TODO add your handling code here:
}

private void jB6ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("6");
// TODO add your handling code here:
}

private void jB7ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("7");
// TODO add your handling code here:
}

private void jB8ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("8");
// TODO add your handling code here:
}

private void jB9ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("9");
// TODO add your handling code here:
}

private void jBSumarActionPerformed(java.awt.event.ActionEvent evt) {
EscogerOperacion('+');
}

private void jBIgualActionPerformed(java.awt.event.ActionEvent evt) {
num2 = Double.parseDouble(jTResultado.getText());

switch (operacion){
case '+':
resultado = num1 + num2;
break;

case '-':
resultado = num1 - num2;
break;

case '*':
resultado = num1 * num2;
break;

case '/':
resultado = num1 / num2;
break;
}
jTResultado.setText(String.valueOf(resultado));


// TODO add your handling code here:
}

private void jBRestarActionPerformed(java.awt.event.ActionEvent evt) {
EscogerOperacion('-');
// TODO add your handling code here:
}

private void jBMultiplicarActionPerformed(java.awt.event.ActionEvent evt) {
EscogerOperacion('*'); // TODO add your handling code here:
}

private void jBDividirActionPerformed(java.awt.event.ActionEvent evt) {
EscogerOperacion('/'); // TODO add your handling code here:
}

private void jBreturnActionPerformed(java.awt.event.ActionEvent evt) {
temp = numeroantes;
jTResultado.setText(temp);
// TODO add your handling code here:
}

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

// Variables declaration - do not modify
private javax.swing.JButton jB0;
private javax.swing.JButton jB1;
private javax.swing.JButton jB2;
private javax.swing.JButton jB3;
private javax.swing.JButton jB4;
private javax.swing.JButton jB5;
private javax.swing.JButton jB6;
private javax.swing.JButton jB7;
private javax.swing.JButton jB8;
private javax.swing.JButton jB9;
private javax.swing.JButton jBA;
private javax.swing.JButton jBB;
private javax.swing.JButton jBC;
private javax.swing.JButton jBCE;
private javax.swing.JButton jBClear;
private javax.swing.JButton jBD;
private javax.swing.JButton jBDividir;
private javax.swing.JButton jBE;
private javax.swing.JButton jBF;
private javax.swing.JButton jBIgual;
private javax.swing.JButton jBMultiplicar;
private javax.swing.JButton jBRestar;
private javax.swing.JButton jBSumar;
private javax.swing.JButton jBpunto;
private javax.swing.JButton jBreturn;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton24;
private javax.swing.JRadioButton jRBin;
private javax.swing.JRadioButton jRDec;
private javax.swing.JRadioButton jRHex;
private javax.swing.JRadioButton jROct;
private javax.swing.JTextField jTResultado;
// End of variables declaration

}

Raíz Cuadrada


Es un pequeño programa para sacar la raíz cuadrada de un numero


package raiz;

import javax.swing.JOptionPane;

public class Main {
public static void main(String[] args) {
// TODO code application logic here
int numero,raiz = 0;
numero=Integer.parseInt(JOptionPane.showInputDialog("ingrese la numero "));
raiz= (int) (Math.sqrt(numero) );
JOptionPane.showMessageDialog(null, "la raiz cuadrada de " + numero + " es " +Math.sqrt(numero));
System.out.println("la raiz cuadrada de " +raiz+ " es " +Math.sqrt(numero));
}

}

Perímetro triangulo


Programa de un perímetro de un triangulo con coordenadas en x,y


package triangulo_end;

import javax.swing.JOptionPane;

public class Triangulo_END {
private static String raiz;

public static void main(String[] args) {
// TODO code application logic here
int a,b,c,x,y,z,coordenadaenx1,coordenadaenx2,coordenadaenx3,coordenadaeny1,coordenadaeny2,coordenadaeny3; //Determinar las variables
float perimetro_triangulo;

coordenadaenx1=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en x1 ")); //variables
coordenadaeny1=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en y1 ")); //variables
coordenadaenx2=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en x2 ")); //variables
coordenadaeny2=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en y2 ")); //variables
coordenadaenx3=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en x3 ")); //variables
coordenadaeny3=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en y3 ")); //variables

if (coordenadaenx2-coordenadaenx1==coordenadaenx3-coordenadaenx2 && coordenadaeny1==coordenadaeny3){ //determinar condicion si es cuadrado
a=((coordenadaenx3-coordenadaenx1));
x= (int) +Math.sqrt(a);
System.out.println(" lado1 " +a+ " es ");

b= (((((coordenadaenx2-coordenadaenx1)*(coordenadaenx2-coordenadaenx1)))+((coordenadaeny2-coordenadaeny1)*(coordenadaeny2-coordenadaeny1))));
y= (int) +Math.sqrt(b);
System.out.println(" lado2 " +b+ " es " +Math.sqrt(b));

c=(((((coordenadaenx3-coordenadaenx2)*(coordenadaenx3-coordenadaenx2))+((coordenadaeny2-coordenadaeny3)*(coordenadaeny2-coordenadaeny3)))));
z= (int) +Math.sqrt(c);
System.out.println(" lado3 " +c+ " es " +Math.sqrt(c));

perimetro_triangulo= (float) (x+y+z);//calcular el perimetro



System.out.println("Perimetro del triangulo es "+perimetro_triangulo+ " ") ;
JOptionPane.showMessageDialog(null, "Perimetro del triangulo es " + perimetro_triangulo + " "); //mostrar en pantalla el area del cuadrado


}else{
JOptionPane.showMessageDialog(null, "NO ES TRIANGULO"); //mostra en pantalla si los datos son erronios
JOptionPane.showMessageDialog(null, "INGRESE VALORES CORRECTOS"); //mostrar en pantalla ingresar datos correctos
}
}
}

Área del triangulo


CÓDIGO DE UN TRIANGULO BÁSICO


package areatriangulo;

import javax.swing.JOptionPane;

public class Main {
public static void main(String[] args) {
// TODO code application logic here
int base,altura;
float area_triangulo;
base=Integer.parseInt(JOptionPane.showInputDialog("ingrese la base "));
altura=Integer.parseInt(JOptionPane.showInputDialog("ingrese la altura "));

area_triangulo = (base*altura)/2;
area_triangulo = (float)(base*altura)/2;
JOptionPane.showMessageDialog(null, "area rectangulo " + area_triangulo + "");

}

}

Programación para encontrar el área de un cuadrado o un rectángulo


Aquí encontraran ejemplos que les podrían ayudar a desarrollar sus programas.

Ares de un cuadrado o un rectángulo

import javax.swing.JOptionPane;
public class Area_END {


public static void main(String[] args) {
int area_cuadrado,area_rectangulo,coordenadaenx1,coordenadaenx2,coordenadaenx3,coordenadaenx4,coordenadaeny1,coordenadaeny2,coordenadaeny3,coordenadaeny4; //Determinar las variables

coordenadaenx1=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en x1 ")); //variables
coordenadaeny1=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en y1 ")); //variables
coordenadaenx2=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en x2 ")); //variables
coordenadaeny2=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en y2 ")); //variables
coordenadaenx3=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en x3 ")); //variables
coordenadaeny3=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en y3 ")); //variables
coordenadaenx4=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en x4 ")); //variables
coordenadaeny4=Integer.parseInt(JOptionPane.showInputDialog("ingrese coordenada en y4 ")); //variables

if (coordenadaenx4-coordenadaenx1==coordenadaeny3-coordenadaeny4 && coordenadaenx3-coordenadaenx2==coordenadaeny2-coordenadaeny1){ //determinar condicion si es cuadrado
area_cuadrado= ((coordenadaenx4-coordenadaenx1)*(coordenadaeny3-coordenadaeny4)); //calcular si es cuadrado
JOptionPane.showMessageDialog(null, "Area del cuadrado " + area_cuadrado + ""); //mostrar en pantalla el area del cuadrado
System.out.println("Area del cuadrado es "+area_cuadrado+"");
}else if (coordenadaenx4-coordenadaenx1==coordenadaenx3-coordenadaenx2 && coordenadaeny2-coordenadaeny1==coordenadaeny3-coordenadaeny4){ //determinar condicion si es cuadrado
area_rectangulo= ((coordenadaenx4-coordenadaenx1)*(coordenadaeny3-coordenadaeny4)); //calcular si es rectangulo
System.out.println("Area del rectangulo es "+area_rectangulo+"");
JOptionPane.showMessageDialog(null, "Area del rectangulo " + area_rectangulo + ""); //mostrar en pantalla el area del rectangulo
}else{
JOptionPane.showMessageDialog(null, "NO ES CUADRADO NI RECTANGULO"); //mostra en pantalla si los datos son erronios
JOptionPane.showMessageDialog(null, "INGRESE VALORES CORRECTOS"); //mostrar en pantalla ingresar datos correctos
}

}
}