Guest User

Untitled

a guest
Jul 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. /**
  2. * Write an application that creates and prints a random phone number of the form XXX - XXX - XXXX.
  3. * Include the dashes in the output. Do not let the first three digits contain an 8 or 9,
  4. * make sure that the second set of three digits is not greater than 742.
  5. */
  6.  
  7. public class Main {
  8.  
  9. public static int generateRandomInteger(int max) {
  10. return (int)(java.lang.Math.random() * (max+1));
  11. }
  12.  
  13. public static int generateRandomInteger(int min, int max) {
  14. return (int) (java.lang.Math.random() * (max+1) / min);
  15. }
  16.  
  17.  
  18. public static void main(java.lang.String[] args) {
  19. System.out.println("Generating telephone number...");
  20. int[] areaCode = new int[3];
  21. int[] firstDigits = new int[3];
  22. int[] lastDigits = new int[4];
  23. String outcome = "";
  24. //Fill the area code
  25. for(int i = 0; i < areaCode.length; i++) {
  26. areaCode[i] = generateRandomInteger(7); //Will allow any number between 0 - 7
  27. outcome += ""+areaCode[i];
  28. }
  29. outcome += " - ";
  30. //Fill the first digits.
  31. for(int i2 = 0; i2 < firstDigits.length; i2++) {
  32. switch(i2) {
  33. case 0:
  34. firstDigits[i2] = generateRandomInteger(2, 7); // Will allow any digits between 2-7
  35. break;
  36. case 1:
  37. firstDigits[i2] = generateRandomInteger(4); /// Will allow any digits between 0-4
  38. break;
  39. case 2:
  40. firstDigits[i2] = generateRandomInteger(2); // Will allow any digits between 0-2;
  41. break;
  42. }
  43. outcome += ""+firstDigits[i2];
  44. }
  45. outcome += " - ";
  46. //Fill the last
  47. for(int i3 = 0; i3 < lastDigits.length; i3++) {
  48. lastDigits[i3] = generateRandomInteger(9); // Allows any digits between 0-9
  49. outcome += ""+lastDigits[i3];
  50. }
  51. //Print it
  52. System.out.println(outcome);
  53. }
  54. }
Add Comment
Please, Sign In to add comment