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 Lab-1.04.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>
74 changes: 74 additions & 0 deletions src/com/ironhack/labs/lab_104/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.ironhack.labs.lab_104;

import java.util.Arrays;

public class Main {
public static void main(String[] args){
// TASK ONE
int difference = 0;
int[] sampleArray = {1,8,5,4,9,-12,34,21,0,3,4,65};
difference = returnDifference(sampleArray);
System.out.println("The difference between the largest and smallest value of the Array is: " + difference);

// TASK TWO
printTwoSmallest(sampleArray);

// TASK THREE
int x = 25;
int y = 13;
double res = 0;
res = calculateMathematicalExpression(x,y);
System.out.println("The result of the Mathematical Expression when x="+x+" and y="+y+" is equal to: "+res);

}

private static double calculateMathematicalExpression(int x, int y) {
double res = 0;
res = Math.pow(x,2)+Math.pow((4*(double)y/5) - (double)x,2);
return res;
}

private static void printTwoSmallest(int[] sampleArray) {
int min1 = sampleArray[0];
int min2 = sampleArray[1];
//MODO LOOP
for (int i = 0; i < sampleArray.length; i++) {
if(sampleArray[i]<min1){ // SMALLER THAN THE SMALLEST, MUST SHIFT BOTH
min2 = min1;
min1 = sampleArray[i];
}else if(sampleArray[i]<min2){ // SMALLER THAN SECOND SMALLEST, JUST SWITCH
min2 = sampleArray[i];
}
}

/*MODO SORT
Arrays.sort(sampleArray);
min1 = sampleArray[0];
min2 = sampleArray[1];
*/
System.out.println("The smallest element is: "+ min1 +" and the second smallest element is: "+ min2);

}

private static int returnDifference(int[] sampleArray) {
int result=0;
// MODO LOOP
int max = sampleArray[0];
int min = sampleArray[0];
for (int i = 0; i < sampleArray.length; i++) {
if(sampleArray[i]<min){
min = sampleArray[i];
}
if(sampleArray[i]>max){
max = sampleArray[i];
}
}
result = max - min;
return result;
/* MODO SORT
Arrays.sort(sampleArray);
result = sampleArray[sampleArray.length-1] - sampleArray[0];
return result;
*/
}
}