Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
.idea/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
11 changes: 11 additions & 0 deletions Lab104LoopsVersionControl.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
75 changes: 75 additions & 0 deletions src/com/ironhack/lab104/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.ironhack.lab104;

import java.util.Arrays;

public class Main {
public static void main(String[] args) {

System.out.println("************************ Task1 ************************");
System.out.println("La diferencia del número mayor con el menor es: " + getDifference());

System.out.println("\n************************ Task2 ************************");
System.out.println(getSmallest());

System.out.println("\n************************ Task3 ************************");
System.out.println("El resultado de la expresión x^2 + ((4y/5) - x)^2 es: " + getResult(5, 2));

}

// Task 1
// Write a Java method that returns the difference between the largest and smallest values in an array of integers.
// The length of the array must be at least 1.
public static int getDifference() {
int[] integers = {2, 45, 100, 5, 150, 30, 300};

// Inicializo las variables int numMayor y numMenor con la primera posición del array
// Para compararlas después con cada posición al recorrer el array
int numMayor = integers[0];
int numMenor = integers[0];

// Se inicia en el índice 1 porque el 0 ya se se ha usado para inicializar las variables anteriores
for (int i = 1; i < integers.length; i++) {
if (integers[i] > numMayor){
numMayor = integers[i];
} else if (integers[i] < numMenor) {
numMenor = integers[i];
}
}

return numMayor-numMenor;
}

// Task 2
// Write a Java method that finds the smallest and second smallest elements of a given array and prints them to the console.
public static String getSmallest() {
int[] integers = {0, 45, 100, 5, 150, 30, 300};

int smallestFirst = integers[0];
// smallestSecond empieza desde otra posición del array
// para asegurar que ambos valores sean distintos y se puedan actualizar durante el bucle
int smallestSecond = integers[1];
// ARREGLAR ESTO PORQUE PUEDE DAR MAL LA SALIDA SI LOS MÁS PEQUEÑOS SON DEL MISMO VALOR

for (int i = 1; i < integers.length; i++) {
// Comparo el numero actual con el menor encontrado hasta este momento
if (integers[i] < smallestFirst){
// Si el número es más pequeño smallestFirst y smallestSecond se actualizan los 2 con ese valor
smallestSecond = smallestFirst;
smallestFirst = integers[i];
// Si el número es menor que smallestSecond y no es el mismo que smallestFirst, sólo actualizo smallestSecond
} else if (integers[i] < smallestSecond && integers[i] != smallestFirst) {
smallestSecond = integers[i];
}

}

return "First Smallest: " + smallestFirst + " \nSecond Smallest: " + smallestSecond;
}

// Task 3
// Write a Java method that calculates the result of the following mathematical expression, where x and y are two variables that have been pre-set in your code: x^2 + ((4y/5) - x)^2
public static double getResult(double x, double y){

return Math.pow(x, 2) + Math.pow(((4*y/5) - x), 2);
}
}