Development of the first 10 programming exercises 1 in Java.

The following article briefly describes the solution to the first 10 exercises of the class guide for Programming I of Systems Engineering and its solution in the most basic way possible. There are multiple ways to solve the exercises, so if you have any for the problems mentioned, share it in the comment box.

1. Write an algorithm that calculates the area of ââa triangle as a function of the base and the height. The area of ââa triangle is equal to the base times the height divided by 2:

``````import javax.swing.JOptionPane;

public class Index {

public static void main(String[] args){
double base, altura, area;

base = Double.parseDouble(
JOptionPane.showInputDialog(null, "Ingrese la base del triangulo:")
);

altura = Double.parseDouble(
JOptionPane.showInputDialog(null, "Ingrese la altura del triangulo:")
);

area = (base * altura) / 2;

JOptionPane.showMessageDialog(null, "El area del triangulo con las dimensiones especificadas es de: " + area + " cm2");
}
}``````

2. Construct an algorithm such that, given the radius of a circle, calculate and print its area and the length of its circumference. The area of ââthe circle is equal to Π * r 2 and the length of the circumference is 2 * Π * r where r is the radius.

``````import javax.swing.JOptionPane;

public class Index {

public static void main(String[] args){
double radio, area, longitudCircunferencia;

JOptionPane.showInputDialog(null, "Ingrese el radio del círculo:")
);

area = Math.PI * (Math.pow(radio, 2));
longitudCircunferencia = 2 * Math.PI * radio;

JOptionPane.showMessageDialog(
null,
"El area del circulo es de: " + area + "\n"
+
"La longitud de la circunferencia es de: " + longitudCircunferencia
);
}
}``````

3. Construct an algorithm such that, given as data the base and the height of a rectangle, calculate the perimeter (sum of sides) and the surface (area) of the same.

``````import javax.swing.JOptionPane;

public class Index {

public static void main(String[] args){
double base, altura, area, perimetro;

base = Double.parseDouble(
JOptionPane.showInputDialog(null, "Ingrese la base del rectángulo:")
);

altura = Double.parseDouble(
JOptionPane.showInputDialog(null, "Ingrese la altura del rectángulo:")
);

perimetro = (base * 2) + (altura * 2);
area = base * altura;

JOptionPane.showMessageDialog(
null,
"El area del rectángulo es de: " + area + " cm2\n"
+
"El perimetro del rectángulo es de: " + perimetro + "cm"
);
}
}``````

4. The relation between the sides (a, b) of a right triangle and the hypotenuse (h) is given by the formula a 2 + b 2 = h 2 . Develop an algorithm that calculates the hypotenuse of a right triangle, from the measurements of its legs:

``````import javax.swing.JOptionPane;

public class Index {

public static void main(String[] args){

JOptionPane.showInputDialog(null, "Ingrese la medida del lado A:")
);

JOptionPane.showInputDialog(null, "Ingrese la medida del lado B:")
);

hipotenusa = Math.sqrt((Math.pow(ladoA, 2)) + (Math.pow(ladoB, 2)));

JOptionPane.showMessageDialog(
null,
"La hipotenusa mide: " + hipotenusa
);
}
}``````

5. Create an algorithm that calculates the volume of a sphere from the given value of its diameter.

``````import javax.swing.JOptionPane;

public class Ejercicio5 {

public static void main(String[] args) {
double diametro, volumen;

diametro = Double.parseDouble(
JOptionPane.showInputDialog(null, "Ingrese el diametro de la esfera:")
);

volumen = (4 * Math.PI *(Math.pow(diametro,3))) / 3;

JOptionPane.showMessageDialog(null, "El volumen de la esfera es de: " + volumen);
}
}``````

6. Construct an algorithm such that given the side of a cube as data, calculate the area of ââthe base, the lateral area (4 faces of the sides), the total area (all the faces) and the volume (side * side * side).

``````import javax.swing.JOptionPane;

public class Index {

public static void main(String[] args){
double lado, areaBase, areaLateral, areaTotal, volumen;

JOptionPane.showInputDialog(null, "Ingrese la medida del del cubo:")
);

areaLateral = areaBase * 4;
areaTotal = (areaBase * 2) + areaLateral;
volumen = Math.pow(lado, 3);

JOptionPane.showMessageDialog(
null,
"El area base mide: " + areaBase + "cm2 \n"
+ "El area lateral mide: " + areaBase + "cm2 \n"
+ "El area total mide: " + areaTotal + "cm2 \n"
+ "El volumen del cubo es de: " + volumen + " cm3"
);
}
}``````

7. The area of ââa triangle whose sides are a, b, and c, can be calculated by the formula:

Write a program that reads the lengths of the three sides of a triangle and calculates the area of ââthe triangle.

``````import javax.swing.JOptionPane;

public class Index {

public static void main(String[] args) {
double a, b, c, s, area;

a = Double.parseDouble(JOptionPane.showInputDialog(null, "Ingrese la medida del lado a: "));
b = Double.parseDouble(JOptionPane.showInputDialog(null, "Ingrese la medida del lado b: "));
c = Double.parseDouble(JOptionPane.showInputDialog(null, "Ingrese la medida del lado c: "));

s = (a + b + c) / 2;

area = Math.sqrt(s * (s - a) * (s - b ) * ( s - c ));

JOptionPane.showMessageDialog(
null,
"El area del triangulo es de: " + area + " centimetros cuadrados"
);
}
}``````

8. At the Winter Olympics, the time spent by participants in track speed competition is measured in minutes, seconds, and hundredths. The distance they travel, on the other hand, is expressed in meters. Construct an algorithm that calculates the speed of the participants in kilometers per hour, by entering the distance traveled in meters and the time taken in minutes, seconds and hundredths (Note that 1 km = 1000 meters, 1 hour = 60 minutes, 1 minute = 60 seconds and 1 second = 100 hundredths):

``````import javax.swing.JOptionPane;

public class Index {

public static void main(String[] args){
// velocidad = distanciarecorrida/tiempo
int distanciaRecorridaMetros, tiempoEnMinutos, tiempoEnSegundos, tiempoEnCentesimas;
double metrosEnKilometros, totalSegundos, segundosEnHoras, velocidad;

distanciaRecorridaMetros = Integer.parseInt(
JOptionPane.showInputDialog(null, "Ingrese la distancia recorrida por el participante en metros: ")
);

tiempoEnMinutos = Integer.parseInt(
JOptionPane.showInputDialog(null, "Ingrese el tiempo tomado en minutos: ")
);

tiempoEnSegundos = Integer.parseInt(
JOptionPane.showInputDialog(null, "Ingrese el tiempo tomado en segundos: ")
);

tiempoEnCentesimas = Integer.parseInt(
JOptionPane.showInputDialog(null, "Ingrese el tiempo tomado en centesimas: ")
);

metrosEnKilometros = distanciaRecorridaMetros / 1000;
totalSegundos = (60 * tiempoEnMinutos) + tiempoEnSegundos + (tiempoEnCentesimas / 100);
segundosEnHoras = totalSegundos / 3600;

velocidad = (metrosEnKilometros / segundosEnHoras);

JOptionPane.showMessageDialog(null,
"En un tiempo de " + tiempoEnMinutos + " minutos, "
+ tiempoEnSegundos + " segundos y "
+ tiempoEnCentesimas + " centesimas se recorrieron " + distanciaRecorridaMetros + " metros. \n"
+ "Es decir el participante se mantuvo en una velocidad de " + velocidad + " Km/H"
);
}
}``````

9. Create a program that asks for the total kilometers traveled, the price of gasoline (per gallon), the gas money spent on the trip, and the time it took (in hours and minutes) and calculates:

• Gasoline consumption (in gallons and pesos) per 100 km traveled.
• Gasoline consumption (in gallons and pesos) for each km traveled.
• Average speed (in km / h and m / s).
``````import javax.swing.JOptionPane;

public class Index {
public static void main(String args[]){
double consumogasolinaenlitrospor100km, consumogasolinaenlitrosporkm, consumogasolinaenpesospor100km,
kilometrosrecorridos, preciogasolinaporgalon,  preciogasolinaporlitro, tiempotardadoenhoras,

kilometrosrecorridos = Double.parseDouble(JOptionPane.showInputDialog(null, "Ingrese el número de Kilometros recorridos en el viaje (p. ej 300):"));

preciogasolinaporgalon = Double.parseDouble(JOptionPane.showInputDialog(null,"Ahora ingrese el precio de gasolina por galón en pesos (p. ej 9000):"));

dinerogastadoengasolina = Double.parseDouble(JOptionPane.showInputDialog(null,"Cuanto dinero gasto en tanquear su vehiculo en pesos (p. ej 27000)?"));

tiempotardadoenhoras = Double.parseDouble(JOptionPane.showInputDialog(null,"Cuantas horas tardó en realizar el viaje sin contar los minutos (p. ej 2)"));

tiempotardadoenminutos = Double.parseDouble(JOptionPane.showInputDialog(null,"Cuantos minutos adicionales tardó en realizar el viaje para complementar las horas (p. ej 60)"));
// Obtener numero de galones tanqueados a partir del precio de la gasolina y de la plata invertida
// Convertir galones gastados a litros
// Calcular el precio del litro de gasolina en pesos colombianos
preciogasolinaporlitro = (preciogasolinaporgalon/galonesalitros(1));
// Calcular cuantos litros de gastaron de gasolina por cada kilometro
consumogasolinaenlitrospor100km = consumogasolinaenlitrosporkm*100;
// Calcular el costo en pesos de la gasolina gastada por kilometros
consumogasolinaenpesosporkm = (consumogasolinaenlitrosporkm*preciogasolinaporlitro);
consumogasolinaenpesospor100km = consumogasolinaenpesosporkm*100;
// Calcular velocidad media

JOptionPane.showMessageDialog(null, "Consumo de combustible por Kilometro: "+consumogasolinaenlitrosporkm+"(Litros/Km) \n" +
"Costo del combustible por Kilometro: \$"+consumogasolinaenpesosporkm+"(COP/Km) \n"
+
"Consumo de combustible por cada 100 Kilometros: "+consumogasolinaenlitrospor100km+"(Litros/100Km) \n"
+
"Costo del combustible por cada 100 Kilometros: \$"+consumogasolinaenpesospor100km+"(COP/Km) \n"
+
"La velocidad promedio en Km/H es de: "+velocidadpromediokmporhora+"Km/H \n"
+
"La velocidad promedio en m/s es de: " + velocidadpromediometrosporsegundo + "m/s"
);
}

// Funcion para convertir galones a litros
public static double galonesalitros(double galones) {
double litros;
litros = galones*3.785;
return litros;
}

// Calcular la cantidad de litros gastados por cada kilometro de una distancia recorrida
public static double litrogasolinaporkilometro(double distancia_recorrida, double litros_gasolina) {
double litros;
litros = (litros_gasolina/distancia_recorrida);
return litros;
}

// Función para convertir minutos a horas
public static double minutosahoras(double minutos) {
double litros;
litros = minutos/60;
return litros;
}

// Función para calcular la velocidad promedio a partir de una distancia recorrida en determinado tiempo
public static double velocidadpromedioenkilometrosporhora(double distancia_recorrida, double tiempo) {
double litros;
litros = distancia_recorrida/tiempo;
return litros;
}

// Función para convertir los kilometros por hora a metros por segundo (Km/H to m/s)
public static double kilometrosporhoraametrosporsegundo(double velocidad_en_kilometros) {
double litros;
return litros;
}
}``````

10. In an exchange office they need to build a program such that given an amount expressed in dollars as data, convert that amount to pesos, euros and Mexican pesos. Build the corresponding Java program. (1 dollar is equivalent to \$ 1965.20 Colombian pesos; 1 euro is equivalent to \$ 2702.46 Colombian pesos; 1 euro is equivalent to \$ 17.96 Mexican pesos):

``````import javax.swing.JOptionPane;

public class Index {

private static final double PRECIO_DOLAR = 1965.20;
private static final double PRECIO_EURO = 2702.46;
private static final double PRECIO_EURO_ENPESOS_MEXICANOS = 17.96;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

double dolares, totalPesos, totalEuros, totalPesosMexicanos;

dolares = Double.parseDouble(
JOptionPane.showInputDialog(null, "Ingrese la cantidad de dolares: ")
);

totalPesos = (dolares * PRECIO_DOLAR);
totalEuros = (totalPesos / PRECIO_EURO);
totalPesosMexicanos = (PRECIO_EURO_ENPESOS_MEXICANOS * totalEuros);

JOptionPane.showMessageDialog(
null,
"Total Pesos: COP " + totalPesos + "\n "
+ "Total Euros: EUR " + totalEuros + "\n "
+ "Total Pesos Mexicanos: MXN " + totalPesosMexicanos
);
}
}
``````