Created
March 4, 2016 20:21
-
-
Save asterwolf/cdf0f95e1e27d6990f65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DistanceApprox{ | |
public String name; | |
//public double inches; | |
public double feet; | |
public DistanceApprox(String name, double feet){ | |
this.name = name; | |
//this.inches = inches; | |
this.feet = feet; | |
} | |
/* public double feetToInches(double inFeet){ | |
double inches = inFeet / 12; | |
return inches; | |
} | |
public double compareInches(double inInches, DistanceApprox[] list, int arraySize){ | |
for(int i = 0; i < arraySize; i++){ | |
if(list[i] == inInches){ | |
return array[i]; | |
} | |
} | |
} | |
*/ | |
public int compareFeet(double inFeet, DistanceApprox[] list, int arraySize){ | |
for(int i = 0; i < arraySize; i++){ | |
if((int)(list[i].feet * 10) + 1 == (int)(inFeet * 10) || (int)(list[i].feet * 10) + 2 == (int)(inFeet * 10)) { | |
return i ; | |
} | |
} | |
return -1; | |
} | |
public int grabRandomObject(int index){ | |
int rand = (int) Math.random() * 3 + 0; | |
while(rand == index){ | |
rand = (int) Math.random() * 3 + 0; | |
} | |
return rand; | |
} | |
public double compareFeetToObject(double inFeet){ | |
return feet/inFeet; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class DistanceApproxTest { | |
public static void main(String[] args){ | |
Scanner input = new Scanner(System.in); | |
DistanceApprox[] list = new DistanceApprox[4]; | |
list[0] = new DistanceApprox("ShaquilleO'Neal", 7.1); | |
list[1] = new DistanceApprox("Verne Troyer",2.8); | |
list[2] = new DistanceApprox("Football Field", 360.0); | |
list[3] = new DistanceApprox("School Bus", 45.0); | |
System.out.println("Enter in a measurement in feet"); | |
double feet = input.nextDouble(); | |
int matchFound = compareFeet(feet, list, list.length); | |
int rand = grabRandomObject(matchFound); | |
double calc = compareFeetToObject(feet); | |
if(matchFound > 0){ | |
System.out.printf("%d is approximately the size of %s, or is about" + | |
"%f of %s's\n",feet,list[matchFound].name,calc, list[rand].name); | |
} | |
else{ | |
System.out.printf("%d is approximately %.2f %ss.\n",feet, calc, list[rand].name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment