Mostrando entradas con la etiqueta Ejercicios Java. Mostrar todas las entradas
Mostrando entradas con la etiqueta Ejercicios Java. Mostrar todas las entradas

martes, 2 de junio de 2009

Calculadora basica en java

Codigo.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class Main extends Applet implements ActionListener {
Label l1, l2,l3;
TextField t1,t2,t3;
Button b1,b2,b3,b4,b5,b6,b7,b8;

public Main () {
l1 = new Label("Numero 1");
t1 = new TextField(8);
l2 = new Label("Numero 2");
t2 = new TextField(8);
b1 = new Button("Suma");
b2 = new Button("Resta");
b3 = new Button("Multiplica");
b4 = new Button("Divide");
b5 = new Button("Raiz de 1");
b6 = new Button("Raiz de 2");
b7 = new Button("Mayor");
b8 = new Button("Limpia");
l3 = new Label("Resultado");
t3 = new TextField(8);

add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(l3);
add(t3);

b1. addActionListener(this);
b2. addActionListener(this);
b3. addActionListener(this);
b4. addActionListener(this);
b5. addActionListener(this);
b6. addActionListener(this);
b7. addActionListener(this);
b8. addActionListener(this);
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int suma = 0;

suma = n1 + n2;
t3.setText("" + suma);
}
if (ae.getSource() == b2) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int resta = 0;

resta = n2 - n1;
t3.setText("" + resta);
}
if (ae.getSource() == b3) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int multi = 0;

multi = n1 * n2;
t3.setText("" + multi);
}
if (ae.getSource() == b4) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int div = 0;

div = n1 / n2;
t3.setText("" + div);
}

if (ae.getSource() == b5) {
int n1 = Integer.parseInt(t1.getText());
double raiz = 0;

raiz = Math.sqrt(n1);
t3.setText("" + raiz);
}
if (ae.getSource() == b6) {
int n2 = Integer.parseInt(t2.getText());
double raiz = 0;

raiz = Math.sqrt(n2);
t3.setText("" + raiz);
}
if (ae.getSource() == b7) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int mayor = 0;

if (n1 > n2)
mayor = n1;
else
mayor = n2;

t3.setText("" + mayor);
}

if (ae.getSource() == b8) {
t1.setText("");
t2.setText("");
t3.setText("");
}
}
}

Resultado:

miércoles, 7 de enero de 2009

Ejercicio Como programar en java 5.20

5.20 Escriba una aplicacion que imprima la siguiente figura de rombo.

El siguiente codigo es para realizar la figura de un rombo en java.


import javax.swing.JOptionPane;

public class Rombo {

public static void main(String[] args) {

int linea, ancho,numero, inicio, fin, contador;

numero = Integer.parseInt(JOptionPane.showInputDialog("Ingrese un numero"));

ancho = (numero * 2) - 1;
inicio = ancho - numero + 1;

for(contador=1; contador<=inicio-1; contador++)
System.out.print(" ");
System.out.println("*");
inicio--;

fin = inicio;

for(linea = 2; linea<=numero-1; linea++)
{
for(contador=1; contador<=inicio-1;contador++)
System.out.print(" ");
for(contador= inicio; contador<=fin; contador += 2)
{
System.out.print("* ");
}
System.out.println("*");
inicio--;
fin++;
}

inicio = 1;
fin = ancho - 1;
for(contador = inicio; contador<=fin; contador += 2)
{
System.out.print("* ");
}

System.out.println("*");
inicio++;
fin -= 2;

for(linea =numero + 1; linea <=ancho-1; linea++)
{
for(contador=1; contador<=inicio-1; contador++)
System.out.print(" ");
for(contador = inicio; contador<=fin; contador +=2)
{
System.out.print("* ");
}
System.out.println("*");
inicio++;
fin--;
}

inicio = ancho - numero + 1;

for(contador=1; contador<=inicio - 1; contador++)
System.out.print(" ");
System.out.println("*");
}
}