Guest User

Untitled

a guest
Jul 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. package ThirdAssignment;
  2. import java.awt.*;
  3. import java.applet.Applet;
  4. public class ThirdAssignment extends Applet {
  5. int list [][] = new int [12][15];
  6. int sort [][] = new int [12][15];
  7. int choice, greeting;
  8. int searchNum, drawcircle1;
  9. int occurrences = 0, checker;
  10. Button fill, decending, ascending, search, occurrence;
  11. TextField input;
  12. public void init (){
  13. resize (800, 305);
  14. fill = new Button ("Fill");
  15. add (fill);
  16. ascending = new Button ("Ascending");
  17. add (ascending);
  18. decending = new Button ("Decending");
  19. add (decending);
  20. search = new Button ("Search");
  21. add (search);
  22. occurrence = new Button ("Occurences");
  23. add (occurrence);
  24. input = new TextField ("0",5);
  25. add (input);
  26. }
  27. public boolean action (Event e, Object args){
  28. if (e.target == fill){
  29. for (int i = 0; i < 12; i++){
  30. for (int j = 0; j < 15; j++){
  31. list [i][j] =(int) (Math.round(Math.random()*(100-(-10))/2)*2 -10);
  32. sort [i][j] = list[i][j];
  33. }
  34. }
  35. }
  36. if (e.target == decending){
  37. int row,col,temp;
  38. for (int a = 0 ; a < sort.length; a++ ){
  39. for ( int b = 0 ; b < sort[a].length ; b++){
  40. col = a;
  41. row = b;
  42. while (col != -1 && row!= -1){
  43. if (row != 0){
  44. if (sort[col][row] >= sort[col][row - 1]){
  45. temp =sort[col][row-1] ;
  46. sort[col][row-1]= sort[col][row];
  47. sort[col][row] = temp;
  48. }
  49. }
  50. else if ( row==0 && col!= 0){
  51. if (sort[col][row] >= sort[col-1][14]){
  52. temp =sort[col-1][14] ;
  53. sort[col-1][14]= sort[col][row];
  54. sort[col][row] = temp;
  55. }
  56. }
  57. if (row!=0){
  58. row--;
  59. }
  60. else if (row==0){
  61. col--;
  62. row = 14;
  63. }
  64. }
  65. }
  66. }
  67. greeting =1;
  68. choice = 1;
  69. }
  70. if (e.target == ascending){
  71. int row,col,temp;
  72. for (int a = 0 ; a < sort.length; a++ ){
  73. for ( int b = 0 ; b < sort[a].length ; b++){
  74. col = a;
  75. row = b;
  76. while (col != -1 && row!= -1){
  77. if (row != 0){
  78. if (sort[col][row] >= sort[col][row - 1]){
  79. temp =sort[col][row-1] ;
  80. sort[col][row-1]= sort[col][row];
  81. sort[col][row] = temp;
  82. }
  83. }
  84. else if ( row==0 && col!= 0){
  85. if (sort[col][row] >= sort[col-1][14]){
  86. temp =sort[col-1][14] ;
  87. sort[col-1][14]= sort[col][row];
  88. sort[col][row] = temp;
  89. }
  90. }
  91. if (row!=0){
  92. row--;
  93. }
  94. else if (row==0){
  95. col--;
  96. row = 14;
  97. }
  98. }
  99. }
  100. }
  101. greeting =2;
  102. choice = 2;
  103. }
  104. if (e.target == search){
  105. while(true) {
  106. try {
  107. searchNum = Integer.parseInt(input.getText());
  108. break;
  109. }
  110. catch (NumberFormatException e1) {
  111. checker = 1;
  112. }
  113. }
  114. for (int i = 0; i < 12; i++){
  115. for (int j = 0; j < 15; j++){
  116. if (searchNum == list[i][j] || searchNum == sort[i][j]){
  117. drawcircle1 = 1;
  118. }
  119. }
  120. }
  121. }
  122. if (e.target == occurrence){
  123. occurrences = 0;
  124. while(true) {
  125. try {
  126. searchNum = Integer.parseInt(input.getText());
  127. break;
  128. }
  129. catch (NumberFormatException e1) {
  130. checker = 1;
  131. }
  132. }
  133. for (int i = 0; i < 12; i++){
  134. for (int j = 0; j < 15; j++){
  135. if (searchNum == list[i][j]){
  136. occurrences = occurrences + 1;
  137. }
  138. }
  139. }
  140. }
  141. repaint();
  142. return true;
  143. }
  144.  
  145. public void paint (Graphics g) {
  146. g.drawString("This problem will create a 12 x 15 2D array filled with even random numbers between -10 to 100.",25,50);
  147. g.drawString("Random Numbers Unsorted",25,75);
  148. if (checker == 1){
  149. g.drawString("Non-numeric input, please try again", 600,300);
  150. }
  151. if (greeting == 1){
  152. g.drawString("Random Numbers Sorted Decending",415,75);
  153. }
  154. else if (greeting == 2){
  155. g.drawString("Random Numbers Sorted Ascending",415,75);
  156. }
  157. int yPosition = 90, xPosition = 10;
  158. for (int i = 0; i < 12; i++){
  159. yPosition = yPosition + 15;
  160. for (int j = 0; j < 15; j++){
  161. xPosition = xPosition + 25;
  162. if (drawcircle1 == 1 && searchNum == list[i][j] && searchNum >= 0 && searchNum <=8) {
  163. g.setColor(Color.red);
  164. g.drawOval(xPosition-2,yPosition-13,14,14);
  165. }
  166. else if (drawcircle1 == 1 && searchNum == list[i][j] && (searchNum == -10 || searchNum == 100)) {
  167. g.setColor(Color.red);
  168. g.drawOval(xPosition-2,yPosition-13,26,18);
  169. }
  170. else if (drawcircle1 == 1 && searchNum == list[i][j] && (searchNum >= -8 && searchNum <= -2 || searchNum >= 10 && searchNum <= 98 )) {
  171. g.setColor(Color.red);
  172. g.drawOval(xPosition-2,yPosition-13,22,18);
  173. }
  174. g.setColor(Color.black);
  175. g.drawString(String.valueOf(list[i][j]), xPosition, yPosition);
  176. }
  177. xPosition = 10;
  178. }
  179. int yPosition2 = 90, xPosition2 = 400;
  180. if (choice == 1){
  181. for (int i = 0; i < 12; i++){
  182. yPosition2 = yPosition2 + 15;
  183. for (int j = 0; j < 15; j++){
  184. xPosition2 = xPosition2 + 25;
  185. if (drawcircle1 == 1 && searchNum == sort[i][j] && searchNum >= 0 && searchNum <=8) {
  186. g.setColor(Color.red);
  187. g.drawOval(xPosition2-2,yPosition2-13,14,14);
  188. }
  189. else if (drawcircle1 == 1 && searchNum == sort[i][j] && (searchNum == -10 || searchNum == 100)) {
  190. g.setColor(Color.red);
  191. g.drawOval(xPosition2-2,yPosition2-13,26,18);
  192. }
  193. else if (drawcircle1 == 1 && searchNum == sort[i][j] && (searchNum >= -8 && searchNum <= -2 || searchNum >= 10 && searchNum <= 98 )) {
  194. g.setColor(Color.red);
  195. g.drawOval(xPosition2-2,yPosition2-13,22,18);
  196. }
  197. g.setColor(Color.black);
  198. g.drawString(String.valueOf(sort[i][j]), xPosition2, yPosition2);
  199. }
  200. xPosition2 = 400;
  201. }
  202. }
  203. if (choice == 2){
  204. for (int i = 11; i >= 0; i--){
  205. yPosition2 = yPosition2 + 15;
  206. for (int j = 14; j >= 0; j--){
  207. xPosition2 = xPosition2 + 25;
  208. if (drawcircle1 == 1 && searchNum == sort[i][j] && searchNum >= 0 && searchNum <=8) {
  209. g.setColor(Color.red);
  210. g.drawOval(xPosition2-2,yPosition2-13,14,14);
  211. }
  212. else if (drawcircle1 == 1 && searchNum == sort[i][j] && (searchNum == -10 || searchNum == 100)) {
  213. g.setColor(Color.red);
  214. g.drawOval(xPosition2-2,yPosition2-13,26,18);
  215. }
  216. else if (drawcircle1 == 1 && searchNum == sort[i][j] && (searchNum >= -8 && searchNum <= -2 || searchNum >= 10 && searchNum <= 98 )) {
  217. g.setColor(Color.red);
  218. g.drawOval(xPosition2-2,yPosition2-13,22,18);
  219. }
  220. g.setColor(Color.black);
  221. g.drawString(String.valueOf(sort[i][j]), xPosition2, yPosition2);
  222. }
  223. xPosition2 = 400;
  224. }
  225. }
  226. if (occurrences >= 0){
  227. g.drawString("The number of occurences for the number ",25,300);
  228. g.drawString(String.valueOf(searchNum), 260, 300);
  229. g.drawString(" is ", 275, 300);
  230. g.drawString(String.valueOf(occurrences), 295, 300);
  231. g.drawString(" for one 12x15 grid", 315, 300);
  232. }
  233. }
  234. }
Add Comment
Please, Sign In to add comment