willieshi232

Untitled

Apr 22nd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. 2.
  2. public class Main
  3. {
  4. int num = 5;
  5. writeNums(num);
  6. }
  7. public static void writeNums(int num)
  8. {
  9. int num2 = 1;
  10. writeNums(num, num2);
  11. }
  12. public static void writeNums(int num, int num2)
  13. {
  14. if(num == num2)
  15. {
  16. System.out.println(num);
  17. }
  18. else
  19. {
  20. System.out.print(num2 + ", ");
  21. writeNums(num2 + 1);
  22. }
  23. }
  24. 4.
  25. public static int mystery(int n) {
  26. if (n < 10) {
  27. return (10 * n) + n;
  28. } else {
  29. int a = mystery(n / 10);
  30. int b = mystery(n % 10);
  31. return (100 * a) + b;
  32. }
  33. }
  34. 6.
  35. import java.util.*;
  36. public class writeSquares
  37. {
  38. public static writeSquares(int n)
  39. {
  40. int t=n;
  41. if(n<=0)
  42. return;
  43. if(n%2==1)
  44. {
  45.  
  46. System.out.println(n*n);
  47. writeSquares(--n);
  48.  
  49. }
  50. else
  51. {
  52.  
  53. writeSquares(--n);
  54. System.out.println(t*t);
  55.  
  56. }
  57.  
  58. }
  59. }
  60. 8.
  61. import java.util.*;
  62. import java.io.*;
  63. public class exercises
  64. {
  65. public static void main(String [] args)
  66. {
  67. Scanner console = new Scanner(System.in);
  68. int n = console.nextInt();
  69. System.out.println(multiplyEvens(n));
  70.  
  71.  
  72. }
  73. private static int multiplyEvens(int n) {
  74. if (n < 1) throw new IllegalArgumentException("Value less than 1 not supported");
  75. else if (n == 1) return 2;
  76. else return multiplyEvens(n - 1) * (n * 2);
  77. }
  78. }
  79. 10.
  80. package suarez.com;
  81.  
  82. import java.util.*;
  83. public class digitMatch
  84. {
  85. public static void main(String [] args)
  86. {
  87. Scanner console = new Scanner(System.in);
  88. System.out.println("gimme an integer sunny boi(non-negative integer)");
  89. int x = console.nextInt();
  90. System.out.println("gimme another integer sunny boi(non-negative integer");
  91. int y = console.nextInt();
  92. int count = 0;
  93. int z = DigitMatch(x, y, count);
  94. System.out.println("You have" + z + "Digits Matching between your two inputs(");
  95. }
  96. public int DigitMatch(int x, int y)
  97. {
  98. return DigitMatch( x,y, 0);
  99. }
  100. private static int DigitMatch(int x, int y, int count)
  101. {
  102. if(x < 0 || y < 0)
  103. {
  104. throw new IllegalArgumentException("THATS A FREAKIN NEGATIVE NUMBER");
  105. }
  106. if(x == 0 || y == 0)
  107. {
  108. return count;
  109. }
  110. else
  111. {
  112. if(x % 10 == y % 10)
  113. {
  114. count++;
  115. }
  116. return DigitMatch(x/10, y/10, count);
  117. }
  118. }
  119. }
  120. 12.
  121. public static boolean isReverse(String word1, String word2) {
  122. if (word1 == null || word2 == null) {
  123. return false;
  124. }
  125. if (word1.length() == 1 && word2.length() == 1) {
  126. //Used equals just for fast compare
  127. return word1.equals(word2);
  128. } else if (word1.length() == word2.length()) {
  129. if (word1.charAt(0) == word2.charAt(word2.length() - 1)) {
  130. String firstWord = word1.substring(1, word1.length());
  131. String secondWord = word2.substring(0, word2.length() - 1);
  132. System.out.printf("Trimmed %s, %s to %s, %s\n", word1, word2, firstWord, secondWord);
  133. return isReverse(firstWord, secondWord);
  134. } else {
  135. //Characters didn't matched
  136. return false;
  137. }
  138. } else {
  139. //Lenght doesn't match
  140. return false;
  141. }
  142. }
  143. 14.
  144. public static int evenDigits(int n) {
  145. int rev = 0;
  146. int digitCount = 0;
  147.  
  148. // handle negative arguments
  149. if (n < 0) return -evenDigits(-n);
  150.  
  151. // Extract the even digits to variable rev
  152. while (n != 0) {
  153. if (n % 2 == 0) {
  154. rev = rev * 10 + n % 10;
  155. digitCount += 1;
  156. }
  157. n /= 10;
  158. }
  159.  
  160. // The digits were extracted in reverse order; reverse them again
  161. while (digitCount > 0) {
  162. n = n * 10 + rev % 10;
  163. rev /= 10;
  164. digitCount -= 1;
  165. }
  166.  
  167. // The result is in n
  168. return n;
  169. }
  170. 16.
  171. import java.awt.*;
  172. import java.awt.event.*;
  173. import javax.swing.*;
  174. public class SierpinskiCarpet extends JPanel implements ActionListener {
  175.  
  176.  
  177. public static void main(String[] args) {
  178. JFrame window = new JFrame("Sierpinski Carpet");
  179. window.setContentPane( new SierpinskiCarpet() );
  180. window.pack();
  181. window.setResizable(false);
  182. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  183. Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
  184. int x = Math.max(10, (screensize.width - window.getWidth()) / 2);
  185. int y = Math.max(10, (screensize.height - window.getHeight()) / 2);
  186. window.setLocation(x,y);
  187. window.setVisible(true);
  188. }
  189.  
  190.  
  191. private int level;
  192.  
  193. private JRadioButton[] levelButton;
  194.  
  195. private JCheckBox[][] regionCheckbox;
  196.  
  197. private Display display;
  198.  
  199.  
  200. private class Display extends JPanel {
  201. Display() {
  202. setBackground(Color.WHITE);
  203. setPreferredSize(new Dimension(729,729));
  204. }
  205. protected void paintComponent(Graphics g) {
  206. super.paintComponent(g);
  207. drawCarpet(g,level,0,0,729);
  208. }
  209. }
  210.  
  211.  
  212. public SierpinskiCarpet() {
  213. setLayout(new BorderLayout(3,3));
  214. setBackground(Color.GRAY);
  215. setBorder(BorderFactory.createLineBorder(Color.GRAY,3));
  216. display = new Display();
  217. add(display,BorderLayout.CENTER);
  218. Box controls = Box.createVerticalBox();
  219. controls.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  220. controls.setBackground(Color.WHITE);
  221. controls.setOpaque(true);
  222. add(controls, BorderLayout.EAST);
  223. controls.add(new JLabel("Controls"));
  224. controls.add(Box.createVerticalStrut(30));
  225. levelButton = new JRadioButton[7];
  226. ButtonGroup grp = new ButtonGroup();
  227. for (int i = 0; i < 7; i++) {
  228. levelButton[i] = new JRadioButton("Recursion Level " + i);
  229. grp.add(levelButton[i]);
  230. levelButton[i].addActionListener(this);
  231. controls.add(levelButton[i]);
  232. }
  233. level = 4;
  234. levelButton[4].setSelected(true);
  235. regionCheckbox = new JCheckBox[3][3];
  236. JPanel checks = new JPanel();
  237. checks.setLayout(new GridLayout(3,3,5,5));
  238. for (int r = 0; r < 3; r++) {
  239. for (int c = 0; c < 3; c++) {
  240. regionCheckbox[r][c] = new JCheckBox();
  241. checks.add(regionCheckbox[r][c]);
  242. regionCheckbox[r][c].addActionListener(this);
  243. regionCheckbox[r][c].setSelected(true);
  244. }
  245. }
  246. checks.setMaximumSize(new Dimension(90,90));
  247. checks.setAlignmentX(LEFT_ALIGNMENT);
  248. checks.setBackground(Color.WHITE);
  249. regionCheckbox[1][1].setSelected(false);
  250. controls.add(Box.createVerticalStrut(30));
  251. controls.add(new JLabel("Regions To Include:"));
  252. controls.add(Box.createVerticalStrut(15));
  253. controls.add(checks);
  254. }
  255.  
  256.  
  257.  
  258. private void drawCarpet(Graphics g, int level, int x, int y, int size) {
  259. g.fillRect(x,y,size,size);
  260. }
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment