Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 52.52 KB | None | 0 0
  1. "C:\Program Files\Java\jdk1.8.0_131\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=55283:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;D:\Computer Science Strath\workspace\JavaParser\javaparser\JavaParser\out\production\JavaParser;D:\Computer Science Strath\workspace\JavaParser\javaparser-core-3.6.26.jar" Driver
  2.  
  3. Checking class MorpionSolitaire
  4. [MorpionSolitairePanel panel;, public static void main(String[] args) {
  5. JFrame f = new MorpionSolitaire();
  6. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7. f.setVisible(true);
  8. }, public MorpionSolitaire() {
  9. Container content = getContentPane();
  10. content.setLayout(new BorderLayout());
  11. panel = new MorpionSolitairePanel();
  12. content.add(panel, BorderLayout.CENTER);
  13. setTitle("MorpionSolitaire");
  14. pack();
  15. setLocationRelativeTo(null);
  16. }]
  17. public static void main(String[] args)
  18.  
  19. Checking class Phone
  20. [private final String unformattedNumber;, public Phone(String unformattedNumber) {
  21. this.unformattedNumber = unformattedNumber;
  22. }, public String getAreaCode() {
  23. return unformattedNumber.substring(0, 3);
  24. }, public String getPrefix() {
  25. return unformattedNumber.substring(3, 6);
  26. }, public void setNumber(String str) {
  27. unformattedNumber = str;
  28. }]
  29. public String getAreaCode()
  30. public String getPrefix()
  31. public void setNumber(String str)
  32. ****************
  33. CODE SMELL FOUND >>> Class Phone is a data class
  34. ****************
  35.  
  36. Checking class HuffmanNode
  37. [// subtrees
  38. public final HuffmanTree left, right;, public HuffmanNode(HuffmanTree l, HuffmanTree r) {
  39. super(l.frequency + r.frequency);
  40. left = l;
  41. right = r;
  42. }]
  43.  
  44. Checking class Test
  45. [private double mu, sigma;, private double[] state = new double[2];, private int index = state.length;, Test(double m, double s) {
  46. mu = m;
  47. sigma = s;
  48. }, static double[] meanStdDev(double[] numbers) {
  49. if (numbers.length == 0)
  50. return new double[] { 0.0, 0.0 };
  51. double sx = 0.0, sxx = 0.0;
  52. long n = 0;
  53. for (double x : numbers) {
  54. sx += x;
  55. sxx += pow(x, 2);
  56. n++;
  57. }
  58. return new double[] { sx / n, pow((n * sxx - pow(sx, 2)), 0.5) / n };
  59. }, static String replicate(int n, String s) {
  60. return range(0, n + 1).mapToObj(i -> s).collect(joining());
  61. }, static void showHistogram01(double[] numbers) {
  62. final int maxWidth = 50;
  63. long[] bins = new long[10];
  64. for (double x : numbers) bins[(int) (x * bins.length)]++;
  65. double maxFreq = stream(bins).max().getAsLong();
  66. for (int i = 0; i < bins.length; i++) System.out.printf(" %3.1f: %s%n", i / (double) bins.length, replicate((int) (bins[i] / maxFreq * maxWidth), "*"));
  67. System.out.println();
  68. }, @Override
  69. public double getAsDouble() {
  70. index++;
  71. if (index >= state.length) {
  72. double r = sqrt(-2 * log(random())) * sigma;
  73. double x = 2 * PI * random();
  74. state = new double[] { mu + r * sin(x), mu + r * cos(x) };
  75. index = 0;
  76. }
  77. return state[index];
  78. }, // Easy duplicate
  79. public double getAsAnotherDouble() {
  80. index++;
  81. if (index >= state.length) {
  82. double r = sqrt(-2 * log(random())) * sigma;
  83. double x = 2 * PI * random();
  84. state = new double[] { mu + r * sin(x), mu + r * cos(x) };
  85. index = 0;
  86. }
  87. return state[index];
  88. }, // Harder Duplicate
  89. public double getAsYetAnotherDouble() {
  90. index++;
  91. if (index >= state.length) {
  92. double t = sqrt(-2 * log(random())) * sigma;
  93. double y = PI * 2 * random();
  94. state = new double[] { mu + t * sin(y), mu + t * cos(y) };
  95. index = 0;
  96. }
  97. return state[index];
  98. }, public static void main(String[] args) {
  99. Locale.setDefault(Locale.US);
  100. double[] data = DoubleStream.generate(new Test(0.0, 0.5)).limit(100_000).toArray();
  101. double[] res = meanStdDev(data);
  102. System.out.printf("Mean: %8.6f, SD: %8.6f%n", res[0], res[1]);
  103. showHistogram01(stream(data).map(a -> max(0.0, min(0.9999, a / 3 + 0.5))).toArray());
  104. }]
  105. static double[] meanStdDev(double[] numbers)
  106. static String replicate(int n, String s)
  107. static void showHistogram01(double[] numbers)
  108. public double getAsDouble()
  109. public double getAsAnotherDouble()
  110. public double getAsYetAnotherDouble()
  111. public static void main(String[] args)
  112.  
  113. Checking class FloodFill
  114. [public void floodFill(BufferedImage image, Point node, Color targetColor, Color replacementColor) {
  115. int width = image.getWidth();
  116. int height = image.getHeight();
  117. int target = targetColor.getRGB();
  118. int replacement = replacementColor.getRGB();
  119. if (target != replacement) {
  120. Deque<Point> queue = new LinkedList<Point>();
  121. do {
  122. int x = node.x;
  123. int y = node.y;
  124. while (x > 0 && image.getRGB(x - 1, y) == target) {
  125. x--;
  126. }
  127. boolean spanUp = false;
  128. boolean spanDown = false;
  129. while (x < width && image.getRGB(x, y) == target) {
  130. image.setRGB(x, y, replacement);
  131. if (!spanUp && y > 0 && image.getRGB(x, y - 1) == target) {
  132. queue.add(new Point(x, y - 1));
  133. spanUp = true;
  134. } else if (spanUp && y > 0 && image.getRGB(x, y - 1) != target) {
  135. spanUp = false;
  136. }
  137. if (!spanDown && y < height - 1 && image.getRGB(x, y + 1) == target) {
  138. queue.add(new Point(x, y + 1));
  139. spanDown = true;
  140. } else if (spanDown && y < height - 1 && image.getRGB(x, y + 1) != target) {
  141. spanDown = false;
  142. }
  143. x++;
  144. }
  145. } while ((node = queue.pollFirst()) != null);
  146. }
  147. }]
  148. public void floodFill(BufferedImage image, Point node, Color targetColor, Color replacementColor)
  149.  
  150. Checking class Underling
  151. [private Long id;, private String firstName;, private String lastName;, private Manager lineManager;, public Long getId() {
  152. return id;
  153. }, public void setId(Long id) {
  154. this.id = id;
  155. }, public String getFirstName() {
  156. return firstName;
  157. }, public void setFirstName(String firstName) {
  158. this.firstName = firstName;
  159. }, public String getLastName() {
  160. return lastName;
  161. }, public void setLastName(String lastName) {
  162. this.lastName = lastName;
  163. }, public void setManager(Manager boss) {
  164. lineManager = boss;
  165. }, public Manager getManager() {
  166. return lineManager;
  167. }, @Override
  168. public String toString() {
  169. return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", manager=" + lineManager + "]";
  170. }]
  171. public Long getId()
  172. public void setId(Long id)
  173. public String getFirstName()
  174. public void setFirstName(String firstName)
  175. public String getLastName()
  176. public void setLastName(String lastName)
  177. public void setManager(Manager boss)
  178. public Manager getManager()
  179. public String toString()
  180.  
  181. Checking class Basket
  182. [// Basket is envious of item
  183. public float getTotalPrice(Item i) {
  184. float price = i.getPrice() + i.getTax();
  185. if (i.isOnSale())
  186. price = price - i.getSaleDiscount() * price;
  187. return price;
  188. }]
  189. public float getTotalPrice(Item i)
  190.  
  191. Checking class Customer
  192. [private Phone mobilePhone;, public String getMobilePhoneNumber() {
  193. return "(" + mobilePhone.getAreaCode() + ") " + mobilePhone.getPrefix() + "-" + mobilePhone.getNumber();
  194. }]
  195. public String getMobilePhoneNumber()
  196. ****************
  197. CODE SMELL FOUND >>> Class Customer is a data class
  198. ****************
  199.  
  200. Checking class HuffmanLeaf
  201. [// the character this leaf represents
  202. public final char value;, public HuffmanLeaf(int freq, char val) {
  203. super(freq);
  204. value = val;
  205. }]
  206.  
  207. Checking class CipollasAlgorithm
  208. [private static final BigInteger BIG = BigInteger.TEN.pow(50).add(BigInteger.valueOf(151));, private static final BigInteger BIG_TWO = BigInteger.valueOf(2);, private static class Point {
  209.  
  210. BigInteger x;
  211.  
  212. BigInteger y;
  213.  
  214. Point(BigInteger x, BigInteger y) {
  215. this.x = x;
  216. this.y = y;
  217. }
  218.  
  219. @Override
  220. public String toString() {
  221. return String.format("(%s, %s)", this.x, this.y);
  222. }
  223. }, private static class Triple {
  224.  
  225. BigInteger x;
  226.  
  227. BigInteger y;
  228.  
  229. boolean b;
  230.  
  231. Triple(BigInteger x, BigInteger y, boolean b) {
  232. this.x = x;
  233. this.y = y;
  234. this.b = b;
  235. }
  236.  
  237. @Override
  238. public String toString() {
  239. return String.format("(%s, %s, %s)", this.x, this.y, this.b);
  240. }
  241. }, private static Triple c(String ns, String ps) {
  242. BigInteger n = new BigInteger(ns);
  243. BigInteger p = !ps.isEmpty() ? new BigInteger(ps) : BIG;
  244. // Legendre symbol, returns 1, 0 or p - 1
  245. Function<BigInteger, BigInteger> ls = (BigInteger a) -> a.modPow(p.subtract(BigInteger.ONE).divide(BIG_TWO), p);
  246. // Step 0, validate arguments
  247. if (!ls.apply(n).equals(BigInteger.ONE)) {
  248. return new Triple(BigInteger.ZERO, BigInteger.ZERO, false);
  249. }
  250. // Step 1, find a, omega2
  251. BigInteger a = BigInteger.ZERO;
  252. BigInteger omega2;
  253. while (true) {
  254. omega2 = a.multiply(a).add(p).subtract(n).mod(p);
  255. if (ls.apply(omega2).equals(p.subtract(BigInteger.ONE))) {
  256. break;
  257. }
  258. a = a.add(BigInteger.ONE);
  259. }
  260. // multiplication in Fp2
  261. BigInteger finalOmega = omega2;
  262. BiFunction<Point, Point, Point> mul = (Point aa, Point bb) -> new Point(aa.x.multiply(bb.x).add(aa.y.multiply(bb.y).multiply(finalOmega)).mod(p), aa.x.multiply(bb.y).add(bb.x.multiply(aa.y)).mod(p));
  263. // Step 2, compute power
  264. Point r = new Point(BigInteger.ONE, BigInteger.ZERO);
  265. Point s = new Point(a, BigInteger.ONE);
  266. BigInteger nn = p.add(BigInteger.ONE).shiftRight(1).mod(p);
  267. while (nn.compareTo(BigInteger.ZERO) > 0) {
  268. if (nn.and(BigInteger.ONE).equals(BigInteger.ONE)) {
  269. r = mul.apply(r, s);
  270. }
  271. s = mul.apply(s, s);
  272. nn = nn.shiftRight(1);
  273. }
  274. // Step 3, check x in Fp
  275. if (!r.y.equals(BigInteger.ZERO)) {
  276. return new Triple(BigInteger.ZERO, BigInteger.ZERO, false);
  277. }
  278. // Step 5, check x * x = n
  279. if (!r.x.multiply(r.x).mod(p).equals(n)) {
  280. return new Triple(BigInteger.ZERO, BigInteger.ZERO, false);
  281. }
  282. // Step 4, solutions
  283. return new Triple(r.x, p.subtract(r.x), true);
  284. }, public static void main(String[] args) {
  285. System.out.println(c("10", "13"));
  286. System.out.println(c("56", "101"));
  287. System.out.println(c("8218", "10007"));
  288. System.out.println(c("8219", "10007"));
  289. System.out.println(c("331575", "1000003"));
  290. System.out.println(c("665165880", "1000000007"));
  291. System.out.println(c("881398088036", "1000000000039"));
  292. System.out.println(c("34035243914635549601583369544560650254325084643201", ""));
  293. }]
  294. private static Triple c(String ns, String ps)
  295. public static void main(String[] args)
  296.  
  297. Checking class Item
  298. [float price, tax, discount;, boolean onSale;, public float getPrice() {
  299. return price;
  300. }, public float getSaleDiscount() {
  301. return discount;
  302. }, public float getTax() {
  303. return tax;
  304. }, public boolean isOnSale() {
  305. return onSale;
  306. }]
  307. public float getPrice()
  308. public float getSaleDiscount()
  309. public float getTax()
  310. public boolean isOnSale()
  311. ****************
  312. CODE SMELL FOUND >>> Class Item is a data class
  313. ****************
  314.  
  315. Checking class Factory
  316. [BasicStockItem[] hoppers;, public Factory() {
  317. hoppers = new BasicStockItem[10];
  318. }, public Factory(int h) {
  319. hoppers = new BasicStockItem[h];
  320. }, public void fillHoppers() {
  321. for (int i = 0; i < hoppers.length; i = i + 3) hoppers[i] = new Chunks(10 * i, 50);
  322. for (int i = 1; i < hoppers.length; i = i + 3) hoppers[i] = new Chips(5 * i, 40);
  323. for (int i = 2; i < hoppers.length; i = i + 3) hoppers[i] = new Nuts(7 * i, 70);
  324. }, public void checkLevels() {
  325. for (int i = 0; i < hoppers.length; i++) if (hoppers[i].reorder())
  326. System.out.println("Hopper " + i + " low. Reorder more " + hoppers[i].getDescription());
  327. }]
  328. public void fillHoppers()
  329. public void checkLevels()
  330.  
  331. Checking class BarnsleyFern
  332. [BufferedImage img;, public BarnsleyFern() {
  333. final int dim = 640;
  334. setPreferredSize(new Dimension(dim, dim));
  335. setBackground(Color.white);
  336. img = new BufferedImage(dim, dim, BufferedImage.TYPE_INT_ARGB);
  337. createFern(dim, dim);
  338. }, // long method
  339. void createFern(int w, int h) {
  340. double x = 0;
  341. double y = 0;
  342. for (int i = 0; i < 200_000; i++) {
  343. double tmpx, tmpy;
  344. double r = Math.random();
  345. if (r <= 0.01) {
  346. tmpx = 0;
  347. tmpy = 0.16 * y;
  348. } else if (r <= 0.08) {
  349. tmpx = 0.2 * x - 0.26 * y;
  350. tmpy = 0.23 * x + 0.22 * y + 1.6;
  351. } else if (r <= 0.15) {
  352. tmpx = -0.15 * x + 0.28 * y;
  353. tmpy = 0.26 * x + 0.24 * y + 0.44;
  354. } else {
  355. tmpx = 0.85 * x + 0.04 * y;
  356. tmpy = -0.04 * x + 0.85 * y + 1.6;
  357. }
  358. x = tmpx;
  359. y = tmpy;
  360. img.setRGB((int) Math.round(w / 2 + x * w / 11), (int) Math.round(h - y * h / 11), 0xFF32CD32);
  361. }
  362. }, @Override
  363. public void paintComponent(Graphics gg) {
  364. super.paintComponent(gg);
  365. Graphics2D g = (Graphics2D) gg;
  366. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  367. g.drawImage(img, 0, 0, null);
  368. }, public static void main(String[] args) {
  369. SwingUtilities.invokeLater(() -> {
  370. JFrame f = new JFrame();
  371. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  372. f.setTitle("Barnsley Fern");
  373. f.setResizable(false);
  374. f.add(new BarnsleyFern(), BorderLayout.CENTER);
  375. f.pack();
  376. f.setLocationRelativeTo(null);
  377. f.setVisible(true);
  378. });
  379. }]
  380. void createFern(int w, int h)
  381. public void paintComponent(Graphics gg)
  382. public static void main(String[] args)
  383.  
  384. Checking class Company
  385. [public static void main(String[] args) {
  386. Factory nutsChipsandChunks = new Factory(50);
  387. nutsChipsandChunks.fillHoppers();
  388. nutsChipsandChunks.checkLevels();
  389. }]
  390. public static void main(String[] args)
  391.  
  392. Checking class ValuableStockItem
  393. [protected double value;, public ValuableStockItem() {
  394. super();
  395. }, public ValuableStockItem(int s, int r, String d, double v) {
  396. super(s, r, d);
  397. value = v;
  398. }, public double getValue() {
  399. return value;
  400. }, public void setValue(double value) {
  401. this.value = value;
  402. }]
  403. public double getValue()
  404. public void setValue(double value)
  405.  
  406. Checking class Account
  407. []
  408.  
  409. Checking class Account
  410. [protected double balance;, public Account() {
  411. balance = 0;
  412. }, public Account(double bal) {
  413. balance = bal;
  414. }, public double getBalance() {
  415. return balance;
  416. }, public void deposit(double dep) {
  417. balance = balance + dep;
  418. }, public boolean withdraw(double amount) {
  419. if (amount <= balance) {
  420. balance = balance - amount;
  421. return true;
  422. } else
  423. return false;
  424. }, public void serviceCharge() {
  425. balance = balance - 0.50;
  426. }, public String toString() {
  427. return "Balance: " + balance;
  428. }]
  429. public double getBalance()
  430. public void deposit(double dep)
  431. public boolean withdraw(double amount)
  432. public void serviceCharge()
  433. public String toString()
  434.  
  435. Checking class HuffmanTree
  436. [// the frequency of this tree
  437. public final int frequency;, public HuffmanTree(int freq) {
  438. frequency = freq;
  439. }, // compares on the frequency
  440. public int compareTo(HuffmanTree tree) {
  441. return frequency - tree.frequency;
  442. }]
  443. public int compareTo(HuffmanTree tree)
  444.  
  445. Checking class Intermediate
  446. [private Child child;, public Child getChild() {
  447. return child;
  448. }, public void intermediateOp() {
  449. System.out.println("An intermediate operation");
  450. }]
  451. public Child getChild()
  452. public void intermediateOp()
  453.  
  454. Checking class BasicStockItem
  455. [protected String description;, protected int stocklevel;, protected int reorderlevel;, public BasicStockItem() {
  456. stocklevel = 0;
  457. reorderlevel = 0;
  458. }, public BasicStockItem(int s, int r, String d) {
  459. stocklevel = s;
  460. reorderlevel = r;
  461. description = d;
  462. }, public void setReorderLevel(int r) {
  463. reorderlevel = r;
  464. }, public int getReorderLevel() {
  465. return reorderlevel;
  466. }, public void setStockLevel(int s) {
  467. stocklevel = s;
  468. }, public int getStockLevel() {
  469. return stocklevel;
  470. }, public boolean reorder() {
  471. if (stocklevel < reorderlevel)
  472. return true;
  473. else
  474. return false;
  475. }, public String getDescription() {
  476. return description;
  477. }]
  478. public void setReorderLevel(int r)
  479. public int getReorderLevel()
  480. public void setStockLevel(int s)
  481. public int getStockLevel()
  482. public boolean reorder()
  483. public String getDescription()
  484.  
  485. Checking class Client
  486. [private Parent parent;, public void something() {
  487. parent.getIntermediate().getChild().something();
  488. }, public void somethingElse() {
  489. parent.getIntermediate().getChild().somethingElse();
  490. }, public void intermediate() {
  491. parent.getIntermediate().intermediateOp();
  492. }]
  493. public void something()
  494. public void somethingElse()
  495. public void intermediate()
  496.  
  497. Checking class Manager
  498. [private List<Underling> subordinates;, private Long employeeNo;, private String foreName;, private String surName;, private Manager lineManager;, public Long getEmployeeNo() {
  499. return employeeNo;
  500. }, public void setEmployeeNo(Long eNo) {
  501. this.employeeNo = eNo;
  502. }, public String getForeName() {
  503. return foreName;
  504. }, public void setForeName(String foreName) {
  505. this.foreName = foreName;
  506. }, public String getSurNameName() {
  507. return surName;
  508. }, public void setSurName(String surNameName) {
  509. this.surName = surName;
  510. }, public List<Underling> getSubordinates() {
  511. return subordinates;
  512. }, public void setSubordinates(List<Underling> subordinates) {
  513. this.subordinates = subordinates;
  514. }, @Override
  515. public String toString() {
  516. return "Manager [subordinates=" + subordinates + ", details=" + super.toString() + "]";
  517. }]
  518. public Long getEmployeeNo()
  519. public void setEmployeeNo(Long eNo)
  520. public String getForeName()
  521. public void setForeName(String foreName)
  522. public String getSurNameName()
  523. public void setSurName(String surNameName)
  524. public List<Underling> getSubordinates()
  525. public void setSubordinates(List<Underling> subordinates)
  526. public String toString()
  527.  
  528. Checking class Bresenham
  529. [public static void main(String[] args) {
  530. SwingUtilities.invokeLater(Bresenham::run);
  531. }, private static void run() {
  532. JFrame f = new JFrame();
  533. f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  534. f.setTitle("Bresenham");
  535. f.getContentPane().add(new BresenhamPanel());
  536. f.pack();
  537. f.setLocationRelativeTo(null);
  538. f.setVisible(true);
  539. }]
  540. public static void main(String[] args)
  541. private static void run()
  542. [private final int pixelSize = 10;, BresenhamPanel() {
  543. setPreferredSize(new Dimension(600, 500));
  544. setBackground(Color.WHITE);
  545. }, @Override
  546. public void paintComponent(Graphics g) {
  547. super.paintComponent(g);
  548. int w = (getWidth() - 1) / pixelSize;
  549. int h = (getHeight() - 1) / pixelSize;
  550. int maxX = (w - 1) / 2;
  551. int maxY = (h - 1) / 2;
  552. int x1 = -maxX, x2 = maxX * -2 / 3, x3 = maxX * 2 / 3, x4 = maxX;
  553. int y1 = -maxY, y2 = maxY * -2 / 3, y3 = maxY * 2 / 3, y4 = maxY;
  554. // NNE
  555. drawLine(g, 0, 0, x3, y1);
  556. // ENE
  557. drawLine(g, 0, 0, x4, y2);
  558. // ESE
  559. drawLine(g, 0, 0, x4, y3);
  560. // SSE
  561. drawLine(g, 0, 0, x3, y4);
  562. // SSW
  563. drawLine(g, 0, 0, x2, y4);
  564. // WSW
  565. drawLine(g, 0, 0, x1, y3);
  566. // WNW
  567. drawLine(g, 0, 0, x1, y2);
  568. // NNW
  569. drawLine(g, 0, 0, x2, y1);
  570. }, private void plot(Graphics g, int x, int y) {
  571. int w = (getWidth() - 1) / pixelSize;
  572. int h = (getHeight() - 1) / pixelSize;
  573. int maxX = (w - 1) / 2;
  574. int maxY = (h - 1) / 2;
  575. int borderX = getWidth() - ((2 * maxX + 1) * pixelSize + 1);
  576. int borderY = getHeight() - ((2 * maxY + 1) * pixelSize + 1);
  577. int left = (x + maxX) * pixelSize + borderX / 2;
  578. int top = (y + maxY) * pixelSize + borderY / 2;
  579. g.setColor(Color.black);
  580. g.drawOval(left, top, pixelSize, pixelSize);
  581. }, private void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
  582. // delta of exact value and rounded value of the dependent variable
  583. int d = 0;
  584. int dx = Math.abs(x2 - x1);
  585. int dy = Math.abs(y2 - y1);
  586. // slope scaling factors to
  587. int dx2 = 2 * dx;
  588. // avoid floating point
  589. int dy2 = 2 * dy;
  590. // increment direction
  591. int ix = x1 < x2 ? 1 : -1;
  592. int iy = y1 < y2 ? 1 : -1;
  593. int x = x1;
  594. int y = y1;
  595. if (dx >= dy) {
  596. while (true) {
  597. plot(g, x, y);
  598. if (x == x2)
  599. break;
  600. x += ix;
  601. d += dy2;
  602. if (d > dx) {
  603. y += iy;
  604. d -= dx2;
  605. }
  606. }
  607. } else {
  608. while (true) {
  609. plot(g, x, y);
  610. if (y == y2)
  611. break;
  612. y += iy;
  613. d += dx2;
  614. if (d > dy) {
  615. x += ix;
  616. d -= dy2;
  617. }
  618. }
  619. }
  620. }]
  621. public void paintComponent(Graphics g)
  622. private void plot(Graphics g, int x, int y)
  623. private void drawLine(Graphics g, int x1, int y1, int x2, int y2)
  624.  
  625. Checking class AccountManager
  626. [public AccountDataProvider DataProvider;, public AccountManager(AccountDataProvider dataProvider) {
  627. DataProvider = dataProvider;
  628. }, public Account GetAccount(int id) {
  629. return DataProvider.GetAccount(id);
  630. }]
  631. public Account GetAccount(int id)
  632.  
  633. Checking class NBodySim
  634. [private static class Vector3D {
  635.  
  636. double x, y, z;
  637.  
  638. public Vector3D(double x, double y, double z) {
  639. this.x = x;
  640. this.y = y;
  641. this.z = z;
  642. }
  643.  
  644. public Vector3D plus(Vector3D rhs) {
  645. return new Vector3D(x + rhs.x, y + rhs.y, z + rhs.z);
  646. }
  647.  
  648. public Vector3D minus(Vector3D rhs) {
  649. return new Vector3D(x - rhs.x, y - rhs.y, z - rhs.z);
  650. }
  651.  
  652. public Vector3D times(double s) {
  653. return new Vector3D(s * x, s * y, s * z);
  654. }
  655.  
  656. public double mod() {
  657. return Math.sqrt(x * x + y * y + z * z);
  658. }
  659. }, private static final Vector3D origin = new Vector3D(0, 0, 0);, private static class NBody {
  660.  
  661. private double gc;
  662.  
  663. private int bodies;
  664.  
  665. public final int timeSteps;
  666.  
  667. private double[] masses;
  668.  
  669. private Vector3D[] positions;
  670.  
  671. private Vector3D[] velocities;
  672.  
  673. private Vector3D[] accelerations;
  674.  
  675. public NBody(String fileName) throws IOException {
  676. Path path = Paths.get(fileName);
  677. List<String> lines = Files.readAllLines(path);
  678. String[] gbt = lines.get(0).split(" ");
  679. gc = Double.parseDouble(gbt[0]);
  680. bodies = Integer.parseInt(gbt[1]);
  681. timeSteps = Integer.parseInt(gbt[2]);
  682. masses = new double[bodies];
  683. positions = new Vector3D[bodies];
  684. Arrays.fill(positions, origin);
  685. velocities = new Vector3D[bodies];
  686. Arrays.fill(velocities, origin);
  687. accelerations = new Vector3D[bodies];
  688. Arrays.fill(accelerations, origin);
  689. for (int i = 0; i < bodies; ++i) {
  690. masses[i] = Double.parseDouble(lines.get(i * 3 + 1));
  691. positions[i] = decompose(lines.get(i * 3 + 2));
  692. velocities[i] = decompose(lines.get(i * 3 + 3));
  693. }
  694. System.out.printf("Contents of %s\n", fileName);
  695. for (String line : lines) {
  696. System.out.println(line);
  697. }
  698. System.out.println();
  699. System.out.print("Body : x y z |");
  700. System.out.println(" vx vy vz");
  701. }
  702.  
  703. private Vector3D decompose(String line) {
  704. String[] xyz = line.split(" ");
  705. double x = Double.parseDouble(xyz[0]);
  706. double y = Double.parseDouble(xyz[1]);
  707. double z = Double.parseDouble(xyz[2]);
  708. return new Vector3D(x, y, z);
  709. }
  710.  
  711. private void resolveCollisions() {
  712. for (int i = 0; i < bodies; ++i) {
  713. for (int j = i + 1; j < bodies; ++j) {
  714. if (positions[i].x == positions[j].x && positions[i].y == positions[j].y && positions[i].z == positions[j].z) {
  715. Vector3D temp = velocities[i];
  716. velocities[i] = velocities[j];
  717. velocities[j] = temp;
  718. }
  719. }
  720. }
  721. }
  722.  
  723. private void computeAccelerations() {
  724. for (int i = 0; i < bodies; ++i) {
  725. accelerations[i] = origin;
  726. for (int j = 0; j < bodies; ++j) {
  727. if (i != j) {
  728. double temp = gc * masses[j] / Math.pow((positions[i].minus(positions[j])).mod(), 3);
  729. accelerations[i] = accelerations[i].plus(positions[j].minus(positions[i]).times(temp));
  730. }
  731. }
  732. }
  733. }
  734.  
  735. private void computeVelocities() {
  736. for (int i = 0; i < bodies; ++i) {
  737. velocities[i] = velocities[i].plus(accelerations[i]);
  738. }
  739. }
  740.  
  741. private void computePositions() {
  742. for (int i = 0; i < bodies; ++i) {
  743. positions[i] = positions[i].plus(velocities[i]).plus(accelerations[i].times(0.5));
  744. }
  745. }
  746.  
  747. public void simulate() {
  748. computeAccelerations();
  749. computePositions();
  750. computeVelocities();
  751. resolveCollisions();
  752. }
  753.  
  754. public void printResults() {
  755. String fmt = "Body %d : % 8.6f % 8.6f % 8.6f | % 8.6f % 8.6f % 8.6f\n";
  756. for (int i = 0; i < bodies; ++i) {
  757. System.out.printf(fmt, i + 1, positions[i].x, positions[i].y, positions[i].z, velocities[i].x, velocities[i].y, velocities[i].z);
  758. }
  759. }
  760. }, public static void main(String[] args) throws IOException {
  761. String filename = "nbody.txt";
  762. NBody nb = new NBody(filename);
  763. for (int i = 0; i < nb.timeSteps; ++i) {
  764. System.out.printf("\nCycle %s\n", i + 1);
  765. nb.simulate();
  766. nb.printResults();
  767. }
  768. }]
  769. public static void main(String[] args) throws IOException
  770.  
  771. Checking class BoxingTheCompass
  772. [private static String[] points = new String[32];, public static void main(String[] args) {
  773. buildPoints();
  774. double heading = 0;
  775. for (int i = 0; i <= 32; i++) {
  776. heading = i * 11.25;
  777. switch(i % 3) {
  778. case 1:
  779. heading += 5.62;
  780. break;
  781. case 2:
  782. heading -= 5.62;
  783. break;
  784. default:
  785. }
  786. System.out.printf("%s\t%18s\t%s°\n", (i % 32) + 1, initialUpper(getPoint(heading)), heading);
  787. }
  788. }, private static void buildPoints() {
  789. String[] cardinal = { "north", "east", "south", "west" };
  790. String[] pointDesc = { "1", "1 by 2", "1-C", "C by 1", "C", "C by 2", "2-C", "2 by 1" };
  791. String str1, str2, strC;
  792. for (int i = 0; i <= 3; i++) {
  793. str1 = cardinal[i];
  794. str2 = cardinal[(i + 1) % 4];
  795. strC = (str1.equals("north") || str1.equals("south")) ? (str1 + str2) : (str2 + str1);
  796. for (int j = 0; j <= 7; j++) {
  797. points[i * 8 + j] = pointDesc[j].replace("1", str1).replace("2", str2).replace("C", strC);
  798. }
  799. }
  800. }, private static String initialUpper(String s) {
  801. return s.substring(0, 1).toUpperCase() + s.substring(1);
  802. }, private static String getPoint(double degrees) {
  803. double testD = (degrees / 11.25) + 0.5;
  804. return points[(int) Math.floor(testD % 32)];
  805. }]
  806. public static void main(String[] args)
  807. private static void buildPoints()
  808. private static String initialUpper(String s)
  809. private static String getPoint(double degrees)
  810.  
  811. Checking class Nuts
  812. [public Nuts(int s, int r) {
  813. super(s, r, "Nuts");
  814. }]
  815.  
  816. Checking class Grid
  817. [enum Result {
  818.  
  819. GOOD, BAD, UGLY
  820. }, final int EMPTY = 0, POINT = 1, HORI = 2, VERT = 4, DIUP = 8, DIDO = 16, HORI_END = 32, VERT_END = 64, DIUP_END = 128, DIDO_END = 256, CAND = 512, ORIG = 1024, HINT = 2048;, final int[] basePoints = { 120, 72, 72, 975, 513, 513, 975, 72, 72, 120 };, int cellSize, pointSize, halfCell, centerX, centerY, origX, origY;, int minC, minR, maxC, maxR;, int[][] points;, List<Line> lines;, Map<Point, Choice> choices;, List<Choice> candidates;, class Line {
  821.  
  822. final Point p1, p2;
  823.  
  824. Line(Point p1, Point p2) {
  825. this.p1 = p1;
  826. this.p2 = p2;
  827. }
  828. }, class Choice {
  829.  
  830. int[] dir;
  831.  
  832. List<Point> points;
  833.  
  834. Choice(List<Point> p, int[] d) {
  835. points = p;
  836. dir = d;
  837. }
  838. }, Grid(int cs, int ps) {
  839. cellSize = cs;
  840. pointSize = ps;
  841. halfCell = cs / 2;
  842. points = new int[50][50];
  843. minC = minR = 0;
  844. maxC = maxR = 50;
  845. newGame();
  846. }, final void newGame() {
  847. for (int r = minR; r < maxR; r++) for (int c = minC; c < maxC; c++) points[r][c] = EMPTY;
  848. choices = new HashMap<>();
  849. candidates = new ArrayList();
  850. lines = new ArrayList<>();
  851. minC = minR = 18;
  852. maxC = maxR = 31;
  853. // cross
  854. for (int r = 0; r < 10; r++) for (int c = 0; c < 10; c++) if ((basePoints[r] & (1 << c)) != 0)
  855. points[20 + r][20 + c] = POINT;
  856. }, void draw(Graphics2D g, int w, int h) {
  857. centerX = w / 2;
  858. centerY = h / 2;
  859. origX = centerX - halfCell - 24 * cellSize;
  860. origY = centerY - halfCell - 24 * cellSize;
  861. // grid
  862. g.setColor(Color.lightGray);
  863. int x = (centerX - halfCell) % cellSize;
  864. int y = (centerY - halfCell) % cellSize;
  865. for (int i = 0; i <= w / cellSize; i++) g.drawLine(x + i * cellSize, 0, x + i * cellSize, h);
  866. for (int i = 0; i <= h / cellSize; i++) g.drawLine(0, y + i * cellSize, w, y + i * cellSize);
  867. // lines
  868. g.setStroke(new BasicStroke(2));
  869. for (int i = 0; i < lines.size(); i++) {
  870. Line line = lines.get(i);
  871. if (i == lines.size() - 1)
  872. g.setColor(new Color(0x3399FF));
  873. else
  874. g.setColor(Color.orange);
  875. int x1 = origX + line.p1.x * cellSize;
  876. int y1 = origY + line.p1.y * cellSize;
  877. int x2 = origX + line.p2.x * cellSize;
  878. int y2 = origY + line.p2.y * cellSize;
  879. g.drawLine(x1, y1, x2, y2);
  880. }
  881. // points
  882. for (int r = minR; r < maxR; r++) for (int c = minC; c < maxC; c++) {
  883. int p = points[r][c];
  884. if (p == EMPTY)
  885. continue;
  886. if ((p & ORIG) != 0)
  887. g.setColor(Color.red);
  888. else if ((p & CAND) != 0)
  889. g.setColor(Color.green);
  890. else if ((p & HINT) != 0) {
  891. g.setColor(Color.lightGray);
  892. points[r][c] &= ~HINT;
  893. } else
  894. g.setColor(Color.darkGray);
  895. drawPoint(g, c, r);
  896. }
  897. }, private void drawPoint(Graphics2D g, int x, int y) {
  898. x = origX + x * cellSize - (pointSize / 2);
  899. y = origY + y * cellSize - (pointSize / 2);
  900. g.fillOval(x, y, pointSize, pointSize);
  901. }, Result computerMove(int r, int c) {
  902. checkLines(r, c);
  903. if (candidates.size() > 0) {
  904. Choice choice = candidates.get(0);
  905. addLine(choice.points, choice.dir);
  906. return Result.GOOD;
  907. }
  908. return Result.BAD;
  909. }, Result playerMove(float x, float y) {
  910. int r = Math.round((y - origY) / cellSize);
  911. int c = Math.round((x - origX) / cellSize);
  912. // see if inside active area
  913. if (c < minC || c > maxC || r < minR || r > maxR)
  914. return Result.BAD;
  915. // only process when mouse click is close enough to grid point
  916. int diffX = (int) Math.abs(x - (origX + c * cellSize));
  917. int diffY = (int) Math.abs(y - (origY + r * cellSize));
  918. if (diffX > cellSize / 5 || diffY > cellSize / 5)
  919. return Result.BAD;
  920. // did we have a choice in the previous turn
  921. if ((points[r][c] & CAND) != 0) {
  922. Choice choice = choices.get(new Point(c, r));
  923. addLine(choice.points, choice.dir);
  924. for (Choice ch : choices.values()) {
  925. for (Point p : ch.points) points[p.y][p.x] &= ~(CAND | ORIG);
  926. }
  927. choices.clear();
  928. return Result.GOOD;
  929. }
  930. if (points[r][c] != EMPTY || choices.size() > 0)
  931. return Result.BAD;
  932. checkLines(r, c);
  933. if (candidates.size() == 1) {
  934. Choice choice = candidates.get(0);
  935. addLine(choice.points, choice.dir);
  936. return Result.GOOD;
  937. } else if (candidates.size() > 1) {
  938. // we can make more than one line
  939. points[r][c] |= ORIG;
  940. for (Choice ch : candidates) {
  941. List<Point> cand = ch.points;
  942. Point p = cand.get(cand.size() - 1);
  943. if (p.equals(new Point(c, r)))
  944. p = cand.get(0);
  945. points[p.y][p.x] |= CAND;
  946. choices.put(p, ch);
  947. }
  948. return Result.UGLY;
  949. }
  950. return Result.BAD;
  951. }, void checkLine(int dir, int end, int r, int c, int rIncr, int cIncr) {
  952. List<Point> result = new ArrayList<>(5);
  953. for (int i = -4; i < 1; i++) {
  954. result.clear();
  955. for (int j = 0; j < 5; j++) {
  956. int y = r + rIncr * (i + j);
  957. int x = c + cIncr * (i + j);
  958. int p = points[y][x];
  959. if (p != EMPTY && (p & dir) == 0 || (p & end) != 0 || i + j == 0)
  960. result.add(new Point(x, y));
  961. else
  962. break;
  963. }
  964. if (result.size() == 5) {
  965. candidates.add(new Choice(new ArrayList<>(result), new int[] { dir, end }));
  966. }
  967. }
  968. }, void checkLines(int r, int c) {
  969. candidates.clear();
  970. checkLine(HORI, HORI_END, r, c, 0, 1);
  971. checkLine(VERT, VERT_END, r, c, 1, 0);
  972. checkLine(DIUP, DIUP_END, r, c, -1, 1);
  973. checkLine(DIDO, DIDO_END, r, c, 1, 1);
  974. }, void addLine(List<Point> line, int[] dir) {
  975. Point p1 = line.get(0);
  976. Point p2 = line.get(line.size() - 1);
  977. // mark end points for 5T
  978. points[p1.y][p1.x] |= dir[1];
  979. points[p2.y][p2.x] |= dir[1];
  980. lines.add(new Line(p1, p2));
  981. for (Point p : line) points[p.y][p.x] |= dir[0];
  982. // growable active area
  983. minC = Math.min(p1.x - 1, Math.min(p2.x - 1, minC));
  984. maxC = Math.max(p1.x + 1, Math.max(p2.x + 1, maxC));
  985. minR = Math.min(p1.y - 1, Math.min(p2.y - 1, minR));
  986. maxR = Math.max(p1.y + 1, Math.max(p2.y + 1, maxR));
  987. }, List<Point> possibleMoves() {
  988. List<Point> moves = new ArrayList<>();
  989. for (int r = minR; r < maxR; r++) for (int c = minC; c < maxC; c++) {
  990. if (points[r][c] == EMPTY) {
  991. checkLines(r, c);
  992. if (candidates.size() > 0)
  993. moves.add(new Point(c, r));
  994. }
  995. }
  996. return moves;
  997. }, void showHints() {
  998. for (Point p : possibleMoves()) points[p.y][p.x] |= HINT;
  999. }]
  1000. final void newGame()
  1001. void draw(Graphics2D g, int w, int h)
  1002. private void drawPoint(Graphics2D g, int x, int y)
  1003. Result computerMove(int r, int c)
  1004. Result playerMove(float x, float y)
  1005. void checkLine(int dir, int end, int r, int c, int rIncr, int cIncr)
  1006. void checkLines(int r, int c)
  1007. void addLine(List<Point> line, int[] dir)
  1008. List<Point> possibleMoves()
  1009. void showHints()
  1010.  
  1011. Checking class Chunks
  1012. [public Chunks(int s, int r) {
  1013. super(s, r, "Chunks");
  1014. }]
  1015.  
  1016. Checking class Parent
  1017. [private Intermediate intermediateContainer;, public Intermediate getIntermediate() {
  1018. return intermediateContainer;
  1019. }]
  1020. public Intermediate getIntermediate()
  1021. ****************
  1022. CODE SMELL FOUND >>> Class Parent is a data class
  1023. ****************
  1024.  
  1025. Checking class Chips
  1026. [public Chips(int s, int r) {
  1027. super(s, r, "Chips");
  1028. }]
  1029.  
  1030. Checking class Child
  1031. [public void something() {
  1032. System.out.println("Some operation");
  1033. }, public void somethingElse() {
  1034. System.out.println("Some other operation");
  1035. }]
  1036. public void something()
  1037. public void somethingElse()
  1038.  
  1039. Checking class Eertree
  1040. [public static void main(String[] args) {
  1041. List<Node> tree = eertree("eertree");
  1042. List<String> result = subPalindromes(tree);
  1043. System.out.println(result);
  1044. }, private static class Node {
  1045.  
  1046. int length;
  1047.  
  1048. Map<Character, Integer> edges = new HashMap<>();
  1049.  
  1050. int suffix;
  1051.  
  1052. public Node(int length) {
  1053. this.length = length;
  1054. }
  1055.  
  1056. public Node(int length, Map<Character, Integer> edges, int suffix) {
  1057. this.length = length;
  1058. this.edges = edges != null ? edges : new HashMap<>();
  1059. this.suffix = suffix;
  1060. }
  1061. }, private static final int EVEN_ROOT = 0;, private static final int ODD_ROOT = 1;, private static List<Node> eertree(String s) {
  1062. List<Node> tree = new ArrayList<>();
  1063. tree.add(new Node(0, null, ODD_ROOT));
  1064. tree.add(new Node(-1, null, ODD_ROOT));
  1065. int suffix = ODD_ROOT;
  1066. int n, k;
  1067. for (int i = 0; i < s.length(); ++i) {
  1068. char c = s.charAt(i);
  1069. for (n = suffix; ; n = tree.get(n).suffix) {
  1070. k = tree.get(n).length;
  1071. int b = i - k - 1;
  1072. if (b >= 0 && s.charAt(b) == c) {
  1073. break;
  1074. }
  1075. }
  1076. if (tree.get(n).edges.containsKey(c)) {
  1077. suffix = tree.get(n).edges.get(c);
  1078. continue;
  1079. }
  1080. suffix = tree.size();
  1081. tree.add(new Node(k + 2));
  1082. tree.get(n).edges.put(c, suffix);
  1083. if (tree.get(suffix).length == 1) {
  1084. tree.get(suffix).suffix = 0;
  1085. continue;
  1086. }
  1087. while (true) {
  1088. n = tree.get(n).suffix;
  1089. int b = i - tree.get(n).length - 1;
  1090. if (b >= 0 && s.charAt(b) == c) {
  1091. break;
  1092. }
  1093. }
  1094. tree.get(suffix).suffix = tree.get(n).edges.get(c);
  1095. }
  1096. return tree;
  1097. }, private static List<String> subPalindromes(List<Node> tree) {
  1098. List<String> s = new ArrayList<>();
  1099. subPalindromes_children(0, "", tree, s);
  1100. for (Map.Entry<Character, Integer> cm : tree.get(1).edges.entrySet()) {
  1101. String ct = String.valueOf(cm.getKey());
  1102. s.add(ct);
  1103. subPalindromes_children(cm.getValue(), ct, tree, s);
  1104. }
  1105. return s;
  1106. }, // nested methods are a pain, even if lambdas make that possible for Java
  1107. private static void subPalindromes_children(final int n, final String p, final List<Node> tree, List<String> s) {
  1108. for (Map.Entry<Character, Integer> cm : tree.get(n).edges.entrySet()) {
  1109. Character c = cm.getKey();
  1110. Integer m = cm.getValue();
  1111. String pl = c + p + c;
  1112. s.add(pl);
  1113. subPalindromes_children(m, pl, tree, s);
  1114. }
  1115. }]
  1116. public static void main(String[] args)
  1117. private static List<Node> eertree(String s)
  1118. private static List<String> subPalindromes(List<Node> tree)
  1119. private static void subPalindromes_children(final int n, final String p, final List<Node> tree, List<String> s)
  1120.  
  1121. Checking class SavingsAcc
  1122. [protected double interestRate;, public SavingsAcc() {
  1123. super();
  1124. minBalance = 500;
  1125. interestRate = 0.03;
  1126. }, public SavingsAcc(double initBal, double limit, double rate) {
  1127. super(initBal, limit);
  1128. interestRate = rate;
  1129. }, public void computeInterest() {
  1130. balance = balance + balance * interestRate;
  1131. }, // Refused bequest - no-op
  1132. public void serviceCharge() {
  1133. }, public String toString() {
  1134. return super.toString() + " Interest Rate : " + interestRate;
  1135. }]
  1136. public void computeInterest()
  1137. public void serviceCharge()
  1138. public String toString()
  1139.  
  1140. Checking class BarnsleyFernTwo
  1141. [BufferedImage img;, // temporary fields
  1142. double tmpx, tmpy;, public BarnsleyFernTwo() {
  1143. final int dim = 640;
  1144. setPreferredSize(new Dimension(dim, dim));
  1145. setBackground(Color.white);
  1146. img = new BufferedImage(dim, dim, BufferedImage.TYPE_INT_ARGB);
  1147. createFernWithTemp(dim, dim);
  1148. }, // long method
  1149. void createFernWithTemp(int w, int h) {
  1150. double x = 0;
  1151. double y = 0;
  1152. for (int i = 0; i < 200_000; i++) {
  1153. // double tmpx, tmpy;
  1154. double r = Math.random();
  1155. if (r <= 0.01) {
  1156. tmpx = 0;
  1157. tmpy = 0.16 * y;
  1158. } else if (r <= 0.08) {
  1159. tmpx = 0.2 * x - 0.26 * y;
  1160. tmpy = 0.23 * x + 0.22 * y + 1.6;
  1161. } else if (r <= 0.15) {
  1162. tmpx = -0.15 * x + 0.28 * y;
  1163. tmpy = 0.26 * x + 0.24 * y + 0.44;
  1164. } else {
  1165. tmpx = 0.85 * x + 0.04 * y;
  1166. tmpy = -0.04 * x + 0.85 * y + 1.6;
  1167. }
  1168. x = tmpx;
  1169. y = tmpy;
  1170. img.setRGB((int) Math.round(w / 2 + x * w / 11), (int) Math.round(h - y * h / 11), 0xFF32CD32);
  1171. }
  1172. }, @Override
  1173. public void paintComponent(Graphics gg) {
  1174. super.paintComponent(gg);
  1175. Graphics2D g = (Graphics2D) gg;
  1176. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  1177. g.drawImage(img, 0, 0, null);
  1178. }, public static void main(String[] args) {
  1179. SwingUtilities.invokeLater(() -> {
  1180. JFrame f = new JFrame();
  1181. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  1182. f.setTitle("Barnsley Fern");
  1183. f.setResizable(false);
  1184. f.add(new BarnsleyFernTwo(), BorderLayout.CENTER);
  1185. f.pack();
  1186. f.setLocationRelativeTo(null);
  1187. f.setVisible(true);
  1188. });
  1189. }]
  1190. void createFernWithTemp(int w, int h)
  1191. public void paintComponent(Graphics gg)
  1192. public static void main(String[] args)
  1193.  
  1194. Checking class AccountDataProvider
  1195. [private Account accountId;, public AccountDataProvider(Account acc) {
  1196. accountId = acc;
  1197. }, public Account GetAccount(int id) {
  1198. return accountId;
  1199. }]
  1200. public Account GetAccount(int id)
  1201.  
  1202. Checking class Munchausen
  1203. [public static void main(String[] args) {
  1204. for (int i = 0; i <= 5000; i++) {
  1205. int val = String.valueOf(i).chars().map(x -> (int) Math.pow(x - 48, x - 48)).sum();
  1206. if (i == val) {
  1207. System.out.println(i + " (munchausen)");
  1208. }
  1209. }
  1210. }]
  1211. public static void main(String[] args)
  1212.  
  1213. Checking class Consumer
  1214. [public AccountManager AccountManager;, public Consumer(AccountManager accountManager) {
  1215. AccountManager = accountManager;
  1216. }, public void Get(int id) {
  1217. Account account = AccountManager.GetAccount(id);
  1218. }]
  1219. public void Get(int id)
  1220.  
  1221. Checking class Luhn
  1222. [public static void main(String[] args) {
  1223. System.out.println(luhnTest("49927398716"));
  1224. System.out.println(luhnTest("49927398717"));
  1225. System.out.println(luhnTest("1234567812345678"));
  1226. System.out.println(luhnTest("1234567812345670"));
  1227. }, public static boolean luhnTest(String number) {
  1228. int s1 = 0, s2 = 0;
  1229. String reverse = new StringBuffer(number).reverse().toString();
  1230. for (int i = 0; i < reverse.length(); i++) {
  1231. int digit = Character.digit(reverse.charAt(i), 10);
  1232. if (i % 2 == 0) {
  1233. // this is for odd digits, they are 1-indexed in the algorithm
  1234. s1 += digit;
  1235. } else {
  1236. // add 2 * digit for 0-4, add 2 * digit - 9 for 5-9
  1237. s2 += 2 * digit;
  1238. if (digit >= 5) {
  1239. s2 -= 9;
  1240. }
  1241. // dead code - can't ever happen as digit is in the range 0-9
  1242. if (digit > 9) {
  1243. System.out.println("Well that should never have happened!");
  1244. }
  1245. }
  1246. }
  1247. return (s1 + s2) % 10 == 0;
  1248. }, public static void imDeadJim() {
  1249. System.out.println("Somebody call me!");
  1250. }]
  1251. public static void main(String[] args)
  1252. public static boolean luhnTest(String number)
  1253. public static void imDeadJim()
  1254.  
  1255. Checking class SeasonalStockItem
  1256. [protected String season;, public SeasonalStockItem() {
  1257. super();
  1258. }, public SeasonalStockItem(int s, int r, String d, String sn) {
  1259. super(s, r, d);
  1260. season = sn;
  1261. }]
  1262.  
  1263. Checking class StockItem
  1264. [public void setReorderLevel(int r);, public int getReorderLevel();, public void setStockLevel(int s);, public int getStockLevel();, public boolean reorder();, public String getDescription();]
  1265. public void setReorderLevel(int r)
  1266. public int getReorderLevel()
  1267. public void setStockLevel(int s)
  1268. public int getStockLevel()
  1269. public boolean reorder()
  1270. public String getDescription()
  1271.  
  1272. Checking class ChqAcc
  1273. [protected double minBalance;, public ChqAcc() {
  1274. super();
  1275. minBalance = 0;
  1276. }, public ChqAcc(double initBal, double limit) {
  1277. super(initBal);
  1278. minBalance = limit;
  1279. }, // Refused bequest
  1280. public boolean withdraw(double amount) {
  1281. if (balance - amount >= minBalance) {
  1282. balance = balance - amount;
  1283. return true;
  1284. } else
  1285. return false;
  1286. }, public void setLimit(double limit) {
  1287. minBalance = limit;
  1288. }, public double getLimit() {
  1289. return minBalance;
  1290. }, public void serviceCharge() {
  1291. if (balance < 2 * minBalance)
  1292. super.serviceCharge();
  1293. }, public String toString() {
  1294. return super.toString() + " Limit : " + minBalance;
  1295. }]
  1296. public boolean withdraw(double amount)
  1297. public void setLimit(double limit)
  1298. public double getLimit()
  1299. public void serviceCharge()
  1300. public String toString()
  1301.  
  1302. Checking class ManOrBoy
  1303. [interface Arg {
  1304.  
  1305. public int run();
  1306. }, // long parameter list
  1307. public static int A(final int k, final Arg x1, final Arg x2, final Arg x3, final Arg x4, final Arg x5) {
  1308. if (k <= 0)
  1309. return x4.run() + x5.run();
  1310. return new Arg() {
  1311.  
  1312. int m = k;
  1313.  
  1314. public int run() {
  1315. m--;
  1316. return A(m, this, x1, x2, x3, x4);
  1317. }
  1318. }.run();
  1319. }, public static Arg C(final int i) {
  1320. return new Arg() {
  1321.  
  1322. public int run() {
  1323. return i;
  1324. }
  1325. };
  1326. }, public static void main(String[] args) {
  1327. System.out.println(A(10, C(1), C(-1), C(-1), C(1), C(0)));
  1328. }]
  1329. public static int A(final int k, final Arg x1, final Arg x2, final Arg x3, final Arg x4, final Arg x5)
  1330. public static Arg C(final int i)
  1331. public static void main(String[] args)
  1332.  
  1333. Checking class HuffmanCode
  1334. [// input is an array of frequencies, indexed by character code
  1335. public static HuffmanTree buildTree(int[] charFreqs) {
  1336. PriorityQueue<HuffmanTree> trees = new PriorityQueue<HuffmanTree>();
  1337. // one for each non-empty character
  1338. for (int i = 0; i < charFreqs.length; i++) if (charFreqs[i] > 0)
  1339. trees.offer(new HuffmanLeaf(charFreqs[i], (char) i));
  1340. assert trees.size() > 0;
  1341. // loop until there is only one tree left
  1342. while (trees.size() > 1) {
  1343. // two trees with least frequency
  1344. HuffmanTree a = trees.poll();
  1345. HuffmanTree b = trees.poll();
  1346. // put into new node and re-insert into queue
  1347. trees.offer(new HuffmanNode(a, b));
  1348. }
  1349. return trees.poll();
  1350. }, public static void printCodes(HuffmanTree tree, StringBuffer prefix) {
  1351. assert tree != null;
  1352. if (tree instanceof HuffmanLeaf) {
  1353. HuffmanLeaf leaf = (HuffmanLeaf) tree;
  1354. // print out character, frequency, and code for this leaf (which is just the prefix)
  1355. System.out.println(leaf.value + "\t" + leaf.frequency + "\t" + prefix);
  1356. } else if (tree instanceof HuffmanNode) {
  1357. HuffmanNode node = (HuffmanNode) tree;
  1358. // traverse left
  1359. prefix.append('0');
  1360. printCodes(node.left, prefix);
  1361. prefix.deleteCharAt(prefix.length() - 1);
  1362. // traverse right
  1363. prefix.append('1');
  1364. printCodes(node.right, prefix);
  1365. prefix.deleteCharAt(prefix.length() - 1);
  1366. }
  1367. }, public static void main(String[] args) {
  1368. String test = "this is an example for huffman encoding";
  1369. // we will assume that all our characters will have
  1370. // code less than 256, for simplicity
  1371. int[] charFreqs = new int[256];
  1372. // read each character and record the frequencies
  1373. for (char c : test.toCharArray()) charFreqs[c]++;
  1374. // build tree
  1375. HuffmanTree tree = buildTree(charFreqs);
  1376. // print out results
  1377. System.out.println("SYMBOL\tWEIGHT\tHUFFMAN CODE");
  1378. printCodes(tree, new StringBuffer());
  1379. }]
  1380. public static HuffmanTree buildTree(int[] charFreqs)
  1381. public static void printCodes(HuffmanTree tree, StringBuffer prefix)
  1382. public static void main(String[] args)
  1383.  
  1384. Checking class MorpionSolitairePanel
  1385. [enum State {
  1386.  
  1387. START, HUMAN, BOT, OVER
  1388. }, State gameState = State.START;, Grid grid;, String message = "Click to start a new game.";, int playerScore, botScore;, Font scoreFont;, public MorpionSolitairePanel() {
  1389. setPreferredSize(new Dimension(1000, 750));
  1390. setBackground(Color.white);
  1391. setFont(new Font("SansSerif", Font.BOLD, 16));
  1392. scoreFont = new Font("SansSerif", Font.BOLD, 12);
  1393. grid = new Grid(35, 9);
  1394. addMouseListener(new MouseAdapter() {
  1395.  
  1396. @Override
  1397. public void mousePressed(MouseEvent e) {
  1398. switch(gameState) {
  1399. case START:
  1400. gameState = State.HUMAN;
  1401. message = "Your turn";
  1402. playerScore = botScore = 0;
  1403. grid.newGame();
  1404. break;
  1405. case HUMAN:
  1406. if (SwingUtilities.isRightMouseButton(e))
  1407. grid.showHints();
  1408. else {
  1409. Grid.Result res = grid.playerMove(e.getX(), e.getY());
  1410. if (res == Grid.Result.GOOD) {
  1411. playerScore++;
  1412. if (grid.possibleMoves().isEmpty())
  1413. gameState = State.OVER;
  1414. else {
  1415. gameState = State.BOT;
  1416. message = "Computer plays...";
  1417. }
  1418. }
  1419. }
  1420. break;
  1421. }
  1422. repaint();
  1423. }
  1424. });
  1425. start();
  1426. }, public final void start() {
  1427. new Thread(new Runnable() {
  1428.  
  1429. @Override
  1430. public void run() {
  1431. Random rand = new Random();
  1432. while (true) {
  1433. try {
  1434. if (gameState == State.BOT) {
  1435. Thread.sleep(1500L);
  1436. List<Point> moves = grid.possibleMoves();
  1437. Point move = moves.get(rand.nextInt(moves.size()));
  1438. grid.computerMove(move.y, move.x);
  1439. botScore++;
  1440. if (grid.possibleMoves().isEmpty()) {
  1441. gameState = State.OVER;
  1442. } else {
  1443. gameState = State.HUMAN;
  1444. message = "Your turn";
  1445. }
  1446. repaint();
  1447. }
  1448. Thread.sleep(100L);
  1449. } catch (InterruptedException ignored) {
  1450. }
  1451. }
  1452. }
  1453. }).start();
  1454. }, @Override
  1455. public void paintComponent(Graphics gg) {
  1456. super.paintComponent(gg);
  1457. Graphics2D g = (Graphics2D) gg;
  1458. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  1459. grid.draw(g, getWidth(), getHeight());
  1460. if (gameState == State.OVER) {
  1461. message = "No more moves available. ";
  1462. if (playerScore > botScore)
  1463. message += "You win. ";
  1464. else if (botScore > playerScore)
  1465. message += "Computer wins. ";
  1466. else
  1467. message += "It's a tie. ";
  1468. message += "Click to start a new game.";
  1469. gameState = State.START;
  1470. }
  1471. g.setColor(Color.white);
  1472. g.fillRect(0, getHeight() - 50, getWidth(), getHeight() - 50);
  1473. g.setColor(Color.lightGray);
  1474. g.setStroke(new BasicStroke(1));
  1475. g.drawLine(0, getHeight() - 50, getWidth(), getHeight() - 50);
  1476. g.setColor(Color.darkGray);
  1477. g.setFont(getFont());
  1478. g.drawString(message, 20, getHeight() - 18);
  1479. g.setFont(scoreFont);
  1480. String s1 = "Player " + String.valueOf(playerScore);
  1481. g.drawString(s1, getWidth() - 180, getHeight() - 20);
  1482. String s2 = "Computer " + String.valueOf(botScore);
  1483. g.drawString(s2, getWidth() - 100, getHeight() - 20);
  1484. }]
  1485. public final void start()
  1486. public void paintComponent(Graphics gg)
  1487.  
  1488. Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement