Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ThirdAssignment;
- import java.awt.*;
- import java.applet.Applet;
- public class ThirdAssignment extends Applet {
- int list [][] = new int [12][15];
- int sort [][] = new int [12][15];
- int choice, greeting;
- int searchNum, drawcircle1;
- int occurrences = 0, checker;
- Button fill, decending, ascending, search, occurrence;
- TextField input;
- public void init (){
- resize (800, 305);
- fill = new Button ("Fill");
- add (fill);
- ascending = new Button ("Ascending");
- add (ascending);
- decending = new Button ("Decending");
- add (decending);
- search = new Button ("Search");
- add (search);
- occurrence = new Button ("Occurences");
- add (occurrence);
- input = new TextField ("0",5);
- add (input);
- }
- public boolean action (Event e, Object args){
- if (e.target == fill){
- for (int i = 0; i < 12; i++){
- for (int j = 0; j < 15; j++){
- list [i][j] =(int) (Math.round(Math.random()*(100-(-10))/2)*2 -10);
- sort [i][j] = list[i][j];
- }
- }
- }
- if (e.target == decending){
- int row,col,temp;
- for (int a = 0 ; a < sort.length; a++ ){
- for ( int b = 0 ; b < sort[a].length ; b++){
- col = a;
- row = b;
- while (col != -1 && row!= -1){
- if (row != 0){
- if (sort[col][row] >= sort[col][row - 1]){
- temp =sort[col][row-1] ;
- sort[col][row-1]= sort[col][row];
- sort[col][row] = temp;
- }
- }
- else if ( row==0 && col!= 0){
- if (sort[col][row] >= sort[col-1][14]){
- temp =sort[col-1][14] ;
- sort[col-1][14]= sort[col][row];
- sort[col][row] = temp;
- }
- }
- if (row!=0){
- row--;
- }
- else if (row==0){
- col--;
- row = 14;
- }
- }
- }
- }
- greeting =1;
- choice = 1;
- }
- if (e.target == ascending){
- int row,col,temp;
- for (int a = 0 ; a < sort.length; a++ ){
- for ( int b = 0 ; b < sort[a].length ; b++){
- col = a;
- row = b;
- while (col != -1 && row!= -1){
- if (row != 0){
- if (sort[col][row] >= sort[col][row - 1]){
- temp =sort[col][row-1] ;
- sort[col][row-1]= sort[col][row];
- sort[col][row] = temp;
- }
- }
- else if ( row==0 && col!= 0){
- if (sort[col][row] >= sort[col-1][14]){
- temp =sort[col-1][14] ;
- sort[col-1][14]= sort[col][row];
- sort[col][row] = temp;
- }
- }
- if (row!=0){
- row--;
- }
- else if (row==0){
- col--;
- row = 14;
- }
- }
- }
- }
- greeting =2;
- choice = 2;
- }
- if (e.target == search){
- while(true) {
- try {
- searchNum = Integer.parseInt(input.getText());
- break;
- }
- catch (NumberFormatException e1) {
- checker = 1;
- }
- }
- for (int i = 0; i < 12; i++){
- for (int j = 0; j < 15; j++){
- if (searchNum == list[i][j] || searchNum == sort[i][j]){
- drawcircle1 = 1;
- }
- }
- }
- }
- if (e.target == occurrence){
- occurrences = 0;
- while(true) {
- try {
- searchNum = Integer.parseInt(input.getText());
- break;
- }
- catch (NumberFormatException e1) {
- checker = 1;
- }
- }
- for (int i = 0; i < 12; i++){
- for (int j = 0; j < 15; j++){
- if (searchNum == list[i][j]){
- occurrences = occurrences + 1;
- }
- }
- }
- }
- repaint();
- return true;
- }
- public void paint (Graphics g) {
- g.drawString("This problem will create a 12 x 15 2D array filled with even random numbers between -10 to 100.",25,50);
- g.drawString("Random Numbers Unsorted",25,75);
- if (checker == 1){
- g.drawString("Non-numeric input, please try again", 600,300);
- }
- if (greeting == 1){
- g.drawString("Random Numbers Sorted Decending",415,75);
- }
- else if (greeting == 2){
- g.drawString("Random Numbers Sorted Ascending",415,75);
- }
- int yPosition = 90, xPosition = 10;
- for (int i = 0; i < 12; i++){
- yPosition = yPosition + 15;
- for (int j = 0; j < 15; j++){
- xPosition = xPosition + 25;
- if (drawcircle1 == 1 && searchNum == list[i][j] && searchNum >= 0 && searchNum <=8) {
- g.setColor(Color.red);
- g.drawOval(xPosition-2,yPosition-13,14,14);
- }
- else if (drawcircle1 == 1 && searchNum == list[i][j] && (searchNum == -10 || searchNum == 100)) {
- g.setColor(Color.red);
- g.drawOval(xPosition-2,yPosition-13,26,18);
- }
- else if (drawcircle1 == 1 && searchNum == list[i][j] && (searchNum >= -8 && searchNum <= -2 || searchNum >= 10 && searchNum <= 98 )) {
- g.setColor(Color.red);
- g.drawOval(xPosition-2,yPosition-13,22,18);
- }
- g.setColor(Color.black);
- g.drawString(String.valueOf(list[i][j]), xPosition, yPosition);
- }
- xPosition = 10;
- }
- int yPosition2 = 90, xPosition2 = 400;
- if (choice == 1){
- for (int i = 0; i < 12; i++){
- yPosition2 = yPosition2 + 15;
- for (int j = 0; j < 15; j++){
- xPosition2 = xPosition2 + 25;
- if (drawcircle1 == 1 && searchNum == sort[i][j] && searchNum >= 0 && searchNum <=8) {
- g.setColor(Color.red);
- g.drawOval(xPosition2-2,yPosition2-13,14,14);
- }
- else if (drawcircle1 == 1 && searchNum == sort[i][j] && (searchNum == -10 || searchNum == 100)) {
- g.setColor(Color.red);
- g.drawOval(xPosition2-2,yPosition2-13,26,18);
- }
- else if (drawcircle1 == 1 && searchNum == sort[i][j] && (searchNum >= -8 && searchNum <= -2 || searchNum >= 10 && searchNum <= 98 )) {
- g.setColor(Color.red);
- g.drawOval(xPosition2-2,yPosition2-13,22,18);
- }
- g.setColor(Color.black);
- g.drawString(String.valueOf(sort[i][j]), xPosition2, yPosition2);
- }
- xPosition2 = 400;
- }
- }
- if (choice == 2){
- for (int i = 11; i >= 0; i--){
- yPosition2 = yPosition2 + 15;
- for (int j = 14; j >= 0; j--){
- xPosition2 = xPosition2 + 25;
- if (drawcircle1 == 1 && searchNum == sort[i][j] && searchNum >= 0 && searchNum <=8) {
- g.setColor(Color.red);
- g.drawOval(xPosition2-2,yPosition2-13,14,14);
- }
- else if (drawcircle1 == 1 && searchNum == sort[i][j] && (searchNum == -10 || searchNum == 100)) {
- g.setColor(Color.red);
- g.drawOval(xPosition2-2,yPosition2-13,26,18);
- }
- else if (drawcircle1 == 1 && searchNum == sort[i][j] && (searchNum >= -8 && searchNum <= -2 || searchNum >= 10 && searchNum <= 98 )) {
- g.setColor(Color.red);
- g.drawOval(xPosition2-2,yPosition2-13,22,18);
- }
- g.setColor(Color.black);
- g.drawString(String.valueOf(sort[i][j]), xPosition2, yPosition2);
- }
- xPosition2 = 400;
- }
- }
- if (occurrences >= 0){
- g.drawString("The number of occurences for the number ",25,300);
- g.drawString(String.valueOf(searchNum), 260, 300);
- g.drawString(" is ", 275, 300);
- g.drawString(String.valueOf(occurrences), 295, 300);
- g.drawString(" for one 12x15 grid", 315, 300);
- }
- }
- }
Add Comment
Please, Sign In to add comment