Primitive Data Types
public void randomIntFinder(Scanner scanner)
{
try
{
System.out.printf("Enter 2 numbers and get a random " + "integer between those two numbers!\n");
scanner = new Scanner(System.in);
System.out.printf("Enter the first integer: ");
int intRange1 = scanner.nextInt();
System.out.printf("Enter the next integer: ");
int intRange2 = scanner.nextInt();
Random random = new Random();
int randomInt = random.nextInt(intRange1, intRange2 + 1);
System.out.printf("Random number between %d and %d: %d\n\n", intRange1, intRange2, randomInt);
}
catch (IllegalArgumentException e)
{
System.out.printf("Error: The last number must be greater than the first!\n");
}
catch (InputMismatchException e)
{
System.out.printf("Error: Must be type integer!\n");
}
}
public void doubleAddingCalculator(Scanner scanner)
{
try
{
System.out.printf("Enter 2 decimals to get their sum!\n");
System.out.printf("Enter the first double: ");
double firstDouble = scanner.nextDouble();
System.out.printf("Enter the next double: ");
double lastDouble = scanner.nextDouble();
firstDouble += lastDouble;
System.out.printf("Sum: %f\n\n", firstDouble);
}
catch (InputMismatchException e)
{
System.out.printf("Error: Must be type double!\n");
}
}
public void getRomanNumerals(Scanner scanner)
{
System.out.print("Enter any number: ");
int num = scanner.nextInt();
String numeral = convertRomanNumeral(num);
System.out.print("Roman numeral: " + numeral);
}
/*
* Takes the number passed in and returns a string of the Roman Numeral
*/
public static String convertRomanNumeral(int number)
{
// calculates number of 1000s
int _1000 = (number / 1000);
String value = numToRN(_1000 * 1000);
number -= _1000 * 1000;
// calculates number of 100s
int _100 = (number / 100);
String value1 = numToRN(_100 * 100);
number -= _100 * 100;
// calculates number of 10s
int _10 = (number / 10);
String value2 = numToRN(_10 * 10);
number -= _10 * 10;
// calculates number of 1s
int _1 = number;
String value3 = numToRN(_1);
number -= _1;
return value + value1 + value2 + value3;
}
/*
* A table of all the
*/
private static String numToRN (int number)
{
switch (number)
{
case 1: return "I";
case 2: return "II";
case 3: return "III";
case 4: return "IV";
case 5: return "V";
case 6: return "VI";
case 7: return "VII";
case 8: return "VIII";
case 9: return "IX";
case 10: return "X";
case 20: return "XX";
case 30: return "XXX";
case 40: return "XL";
case 50: return "L";
case 60: return "LX";
case 70: return "LXX";
case 80: return "LXXX";
case 90: return "XC";
case 100: return "C";
case 200: return "CC";
case 300: return "CCC";
case 400: return "CD";
case 500: return "D";
case 600: return "DC";
case 700: return "DCC";
case 800: return "DCCC";
case 900: return "CM";
case 1000: return "M";
case 2000: return "MM";
case 3000: return "MMM";
}
return "";
}
Scanner scanner = new Scanner(System.in);
PrimitivesTest primitivesTest = new PrimitivesTest();
primitivesTest.randomIntFinder(scanner);
primitivesTest.doubleAddingCalculator(scanner);
primitivesTest.getRomanNumerals (scanner);
scanner.close();