2017-01-31 3 views
1

Ich habe eine Frage zu meinem Java-Projekt. In School begannen wir mit dem Schreiben einer bubblesort-Methode, die eine Liste von Ganzzahlen sortieren und dann grafisch sortieren soll. Mein Problem ist jedoch, dass wir es zu Hause beenden mussten, und ich scheine mit einer kleinen Sache hängen zu bleiben.Java BlueJ Bubblesort

Dies ist der gesamte Code meiner Bubblesort Klasse:

import java.util.Random; 
/** 
* Write a description of class Bubblesort here. 
* 
* @author Aaron Zwickenpflug 
* @version 30.01.2017 
*/ 
public class Bubblesort { 
    private Random random; 
    private int maxSquare; 
    private int maxRandom; 
    private int[] x; 
    private Square[] square; 

    public Bubblesort() { 
     this.random = new Random(); 

     this.maxSquare = 100; 
     this.maxRandom = 200; 

     this.x = new int[this.maxSquare]; 
     this.square = new Square[this.maxSquare]; 

     for (int i = 0; i < this.maxSquare; i++) { 
      this.square[i] = new Square(i, x[i]); 
      this.x[i] = random.nextInt(maxRandom); 
      this.square[i].changeY(x[i]); 
      // System.out.println(this.x[i]); 
     } 
    } 

    public void redraw() { 
     for (int i = 0; i < this.maxSquare - 1; i++) { 
      this.square[i + 1].changeY(this.x[i]); 
     } 
    } 

    public void sort_bubble() { 
     int m; 
     for (int i = 1; i < 100; i++) { 
      for (int y = 0; y < this.maxSquare - i; y++) { 
       if (this.x[y] > this.x[y + 1]) { 
        m = this.x[y]; 
        this.x[y] = this.x[y + 1]; 
        this.x[y + 1] = m; 
        this.square[y + 1].changeY(this.x[i + 1]); 
       } 
      } 
     } 
    } 

} 

Hier ist die Square Klasse:

import java.awt.*; 

/** 
* A square that can be manipulated and that draws itself on a canvas. 
* 
* @author Michael Kölling and David J. Barnes 
* @version 2016.02.29 
*/ 

public class Square { 
    private int xSize; 
    private int ySize; 
    private int xPosition; 
    private int yPosition; 
    private String color; 
    private boolean isVisible; 

    /** 
    * Create a new square at default position with default color. 
    */ 
    public Square(int xPos, int height) { 
     xSize = 9; 
     ySize = height; 
     xPosition = xPos * 10; 
     yPosition = 1; 
     color = "green"; 
     isVisible = false; 
    } 

    public void changeY(int x) { 
     this.ySize = x; 
     makeVisible(); 
    } 

    /** 
    * Make this square visible. If it was already visible, do nothing. 
    */ 
    public void makeVisible() { 
     isVisible = true; 
     draw(); 
    } 

    /** 
    * Make this square invisible. If it was already invisible, do nothing. 
    */ 
    public void makeInvisible() { 
     erase(); 
     isVisible = false; 
    } 

    /** 
    * Move the square a few pixels to the right. 
    */ 
    public void moveRight() { 
     moveHorizontal(20); 
    } 

    /** 
    * Move the square a few pixels to the left. 
    */ 
    public void moveLeft() { 
     moveHorizontal(-20); 
    } 

    /** 
    * Move the square a few pixels up. 
    */ 
    public void moveUp() { 
     moveVertical(-20); 
    } 

    /** 
    * Move the square a few pixels down. 
    */ 
    public void moveDown() { 
     moveVertical(20); 
    } 

    /** 
    * Move the square horizontally by 'distance' pixels. 
    */ 
    public void moveHorizontal(int distance) { 
     erase(); 
     xPosition += distance; 
     draw(); 
    } 

    /** 
    * Move the square vertically by 'distance' pixels. 
    */ 
    public void moveVertical(int distance) { 
     erase(); 
     yPosition += distance; 
     draw(); 
    } 

    /** 
    * Slowly move the square horizontally by 'distance' pixels. 
    */ 
    public void slowMoveHorizontal(int distance) { 
     int delta; 

     if(distance < 0) { 
      delta = -1; 
      distance = -distance; 
     } else { 
      delta = 1; 
     } 

     for(int i = 0; i < distance; i++) { 
      xPosition += delta; 
      draw(); 
     } 
    } 

    /** 
    * Slowly move the square vertically by 'distance' pixels. 
    */ 
    public void slowMoveVertical(int distance) { 
     int delta; 

     if(distance < 0) { 
      delta = -1; 
      distance = -distance; 
     } else { 
      delta = 1; 
     } 

     for(int i = 0; i < distance; i++) { 
      yPosition += delta; 
      draw(); 
     } 
    } 

    /** 
    * Change the size to the new size (in pixels). Size must be >= 0. 
    */ 
    public void changeSize(int newSize) { 
     erase(); 
     xSize = newSize; 
     ySize = newSize; 
     draw(); 
    } 

    /** 
    * Change the color. Valid colors are "red", "yellow", "blue", "green", 
    * "magenta" and "black". 
    */ 
    public void changeColor(String newColor) { 
     color = newColor; 
     draw(); 
    } 

    /** 
    * Draw the square with current specifications on screen. 
    */ 
    private void draw() { 
     if(isVisible) { 
      Canvas canvas = Canvas.getCanvas(); 
      canvas.draw(this, color, 
         new Rectangle(xPosition, yPosition, xSize, ySize)); 
      canvas.wait(10); 
     } 
    } 

    /** 
    * Erase the square on screen. 
    */ 
    private void erase() { 
     if(isVisible) { 
      Canvas canvas = Canvas.getCanvas(); 
      canvas.erase(this); 
     } 
    } 
} 

Jetzt liegt das Problem in der letzten Zeile:

this.square[y + 1].changeY(this.x[i + 1]); 

Es sollte normalerweise (wenn ich das Programm ausführen) Ändern Sie die y-Koordinate zwischen 2 Quadrat es, und "sortiere" es. Obwohl es die Grafiken nur vermasselt, und die Quadrate anfangen, zu irgendwelchen seltsamen Werten zu ändern. Ich habe die Liste überprüft, die sortiert werden muss, und das funktioniert ziemlich gut.

Dies ist, wie die Grafik kann wie folgt aussehen: Bubblesort graphics

Ich hoffe jemand mir mit meinem Problem helfen kann. Vielen Dank im Voraus, A

+0

Bitte bearbeiten Sie Ihre Frage mit einem [mcve] (fügen Sie im Prinzip den Code der 'Square'-Klasse hinzu) und fügen Sie auch einen Link zu einem Bild Ihrer Grafik hinzu. –

+1

in Ihrem redraw for loop Warum iterieren Sie auf maxSquare - 1? Sie sollten immer basierend auf der Größe der Sammlung, über die Sie iterieren, iterieren. i

+0

Sie sollten 'x [i]' vor der Verwendung von 'x [i]' in 'new Square 'setzen (i, x [i]) '(es sei denn, dies ist beabsichtigt). Ich glaube, dass 'int' Arrays jeden Index auf' 0' initialisieren, also machst du grundsätzlich 'new Square (i, 0)'. –

Antwort

0

ich einige Zeit auf den Code zu schauen nahm zu, ich sollte das Problem früher erkannt haben, aber die Canvas Klasse Sie verwenden warf mich ab. So schrieb ich Ihr Programm mit Hilfe von Java Swing-Komponenten statt nur AWT-Komponenten, und nachdem ich eine Zeile korrigiert und fügte hinzu, eine Zeile Code in Ihrer sort_bubble() Methode, habe ich folgendes Ergebnis:

Image of a sorted graph.

Das Problem belogen im Austausch von Elementen in Ihrem sort_bubble() Methode oder tauschen vielmehr die Elemente in Ihrer zugrunde liegenden int Array aber dann nicht die Aktualisierung der Square Array richtig:

m = this.x[y]; 
this.x[y] = this.x[y + 1]; 
this.x[y + 1] = m; 
this.square[y + 1].changeY(this.x[i + 1]); 

Above Sie tauschen x[y] mit x[y + 1] aber aus irgendeinem Grund aktualisieren Sie square[y + 1] mit dem Wert x[i + 1]. Ihre Updates für das Array Square sollten Ihre Swaps auf dem zugrunde liegenden Array int widerspiegeln. Daher Swap-und Update-Code in sort_bubble() sollte ähneln:

m = this.x[y]; 
this.x[y] = this.x[y + 1]; 
this.x[y + 1] = m; 

// reflect the above swaps when updating 'this.square' 
this.square[y].changeY(this.x[y]); 
this.square[y + 1].changeY(this.x[y + 1]); 


Hoffnung diese Antwort hilft und leider dauerte es eine Weile zu reagieren. Lassen Sie mich wissen, wenn Sie weitere Fragen zu dem haben, was ich in dieser Antwort erwähnt habe.