Los programas siguientes utilizan esta clase Leer para que funcione, esta clase debe estar en la misma carpeta donde este cada programa. Todos los programas a continuación deben tener esta clase.
Crear este archivo java con el siguiente código
import java.io.*;
public class Leer{
public static String dato() {
String sdato = "";
try
{
// Definir un flujo de caracteres de entrada: flujoE
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader flujoE = new BufferedReader(isr);
// Leer. La entrada finaliza al pulsar la tecla Entrar
sdato = flujoE.readLine();
}
catch(IOException e) {
System.err.println("Error: " + e.getMessage());
}
return sdato; // devolver el dato tecleado
}
public static short datoShort() {
try
{
return Short.parseShort(dato());
}
catch(NumberFormatException e) {
return Short.MIN_VALUE; // valor más pequeño
}
}
public static int datoInt() {
try {
return Integer.parseInt(dato());
}
catch(NumberFormatException e) {
return Integer.MIN_VALUE; // valor más pequeño
}
}
public static long datoLong() {
try
{
return Long.parseLong(dato());
}
catch(NumberFormatException e) {
return Long.MIN_VALUE; // valor más pequeño
}
}
public static float datoFloat()
{
try
{
return Float.parseFloat(dato());
}
catch(NumberFormatException e)
{
return Float.NaN; // No es un Número; valor float.
}
}
public static double datoDouble() {
try {
return Double.parseDouble(dato());
}
catch(NumberFormatException e) {
return Double.NaN; // No es un Número; valor double.
}
}
}
__________________
public class Producto {
public static void main(String[] args) {
int valor1=0;
int valor2=0;
int resultado=0;
System.out.println("Dame el primer numero =>"); valor1 = Leer.datoInt();
System.out.println("Dame el segundo numero =>"); valor2 = Leer.datoInt();
resultado= valor1 * valor2;
System.out.println();
System.out.println("El resultado es =" + resultado);
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
panel11 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
X1 = new javax.swing.JTextField();
X2 = new javax.swing.JTextField();
Y1 = new javax.swing.JTextField();
Y2 = new javax.swing.JTextField();
Pedir los coeficientes de una ecuación se 2º grado, y muestre sus soluciones . Si no existen indicarlo. formulas:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaApplication1 {
public static void main(String[] args) throws IOException {
double a2,b2, d,x1 = 0,x2;
System.out.println("Introduzaca primer coheficiente a:");
System.out.println("Introduzaca primer coheficiente b:");
System.out.println("Introduzaca primer coheficiente c:");
BufferedReader Entrada = new BufferedReader(new InputStreamReader(System.in));
Dada la siguiente sucesión de números: 2, 4, 8, 6, 36, 72, 70, 4900, 9800,... mediante el uso de funciones, mostrar en pantalla los términos de esta serie y calcular la suma de N (N se indica por teclado) elementos, es decir, SUMA=2+4+8+6+.....
EXPLICACION DEL PROGRAMA: El programa calcula la suma de la serie anterior para n terminos. Para ello, genera cada uno de los valores de dicha serie hasta el limite ingresado por el usuario. La logica que sigue la serie es de elevar al cuadrado, multiplicar por dos y restar dos.
😐😉
import java.io.*;
public class a9 {
public static void main(String args[])throws IOException {
BufferedReader in; int s=0,a=2;
in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Introduzca el limite N");
int n=Integer.parseInt(in.readLine());
for(int i=0;i<n;) {
System.out.println(a);
s=s+a; i++; a=op1(a);
if(i==n)
break;
else {
System.out.println(a);
s=s+a;
a=op2(a); i++;
}
if(i==n) break;
else {
System.out.println(a);
s=s+a;
a=op3(a); i++; }
}
System.out.println("La suma de la serie es: "+s);
}
public static int op1 (int b) {
return b*b;
}
public static int op2 (int b) {
return b*2;
}
public static int op3 (int b) {
return b-2;}
}
--------------------Configuration: <Default>--------------------
Introduzca el limite N
4
2
4
8
6
La suma de la serie es: 20
private void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2; // Below step sorts the left side of the array doMergeSort(lowerIndex, middle); // Below step sorts the right side of the array doMergeSort(middle + 1, higherIndex); // Now merge both sides mergeParts(lowerIndex, middle, higherIndex);
} }
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex; int j = middle + 1; int k = lowerIndex;