Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package com.company;
  2. import java.util.Arrays;
  3. import static jdk.nashorn.internal.objects.NativeString.charAt;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. Main lab1 = new Main();
  8. lab1.compulsory();
  9. lab1.optional(args);
  10. }
  11.  
  12. static int computedN() {
  13. int n = (int) (Math.random() * 1_000_000);
  14. n = n * 3;
  15. n = n + 0b10101;
  16. n = n + 0xff;
  17. n = n * 6;
  18. return n;
  19. }
  20.  
  21. static int recSum(int n) {
  22. int sum = 0;
  23. do {
  24. sum = 0;
  25. while (n != 0) {
  26. sum = sum + n % 10;
  27. n = n / 10;
  28. }
  29. System.out.println(sum);
  30. System.out.println('\n');
  31. n = sum;
  32. } while (n > 10);
  33. return n;
  34. }
  35.  
  36. void compulsory() {
  37. System.out.println("Hello World!");
  38. String[] languages = {"C", "C++", "C#", "Python", "Go", "Rust", "JavaScript", "PHP", "Swift", "Java"};
  39. int result = computedN();
  40. result = recSum(result);
  41. System.out.println("Willy-nilly, this semester I will learn " + languages[result] + '.');
  42. }
  43.  
  44. public String[] generate(int n, int k, char[] alphabet) {
  45. String[] words = new String[n];
  46. for (int i = 0; i < n; i++) {
  47. StringBuilder sb = new StringBuilder();
  48. for (int j = 0; j < k; j++) {
  49. int pos = (int) (Math.random() * (alphabet.length + 1)) - 1;
  50. if (pos < 0) pos=0;
  51. sb.append(alphabet[pos]);
  52. }
  53. words[i] = sb.toString();
  54. }
  55. return words;
  56. }
  57.  
  58. void optional(String[] args) {
  59.  
  60. if (args.length < 6) {
  61. System.out.println("Not enough arguments!");
  62. System.exit(-1);
  63. } else {
  64. //alte validari este int sau char
  65.  
  66. int wordLg = Integer.parseInt(args[1]);
  67. int nbWords = Integer.parseInt(args[0]); //how many words to generate
  68. final int alphabetSize = args.length - 2; //how many characters has the alphabet
  69. char[] myAlphabet = new char[alphabetSize]; //create the alphabet array
  70.  
  71. int l,c;
  72. int i;
  73. for (i = 0; i < alphabetSize; i++) //
  74. myAlphabet[i] = args[i + 2].charAt(0);//converteste in char stringul gasit la poz curenta
  75. System.out.println("That is your alphabet : ");
  76. System.out.println(myAlphabet);
  77. String[] words = generate(nbWords, wordLg, myAlphabet);
  78. System.out.println("These are your random generated words : ");
  79. for( i=0;i< nbWords;i++){
  80. System.out.println(words[i]);
  81. }
  82.  
  83. int[][] isAd = new int[nbWords][nbWords];
  84. for (l = 1; l <= nbWords; l++)
  85. for (c = 1; c <= nbWords; c++)
  86. isAd[l][c] = check(words, l,c, wordLg);
  87.  
  88. for (int[] row : isAd)
  89. System.out.println(Arrays.toString(row));
  90. }
  91. }
  92. public int check(String[] words,int i,int j,int n)
  93. {
  94. for (int p1=0;p1<=n;p1++)
  95. for(int p2=0;p2<n;p2++)
  96. if( (int)words[i].charAt(p1)==(int)words[j].charAt(p2)) return 1;
  97. return 0;
  98. }
  99. void bonus() {
  100. //Do stuff
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement