2017-11-09 1 views
-2

Ich habe versucht, Getter und Setter zusammen mit toString zu verwenden, und ich habe Probleme zu sehen, was das Problem mit meinem Code ist. Ich bin mir nicht sicher, wo genau das Problem liegt. Also, soll ich ein Haupt haben? Ich bin mir nicht sicher, ob das in dieser Situation nötig ist oder nicht.ToString in Java verwenden kann Symbolfehler nicht finden?

Gibt es auch eine Möglichkeit, das "Rectangle (x, x)" besser zu formatieren? Es sieht irgendwie komisch aus, wie es im Moment ist.

public class Rectangle { 
// DO NOT MODIFY THE INSTANCE VARIABLES 
// begin instance variables 
private int width; 
private int height; 
// end instance variables 

// TODO - write your code below this comment. 
// You need to do the following: 
// 
// 1.) Define a constructor which takes two ints 
//  representing the width and height, respectively. 
//  The constructor should set its instance variables 
//  equal to these values. 
// 
// 2.) Define a "getter" named getWidth, which returns 
//  the width of the rectangle. 
// 
// 3.) Define a "getter" named getHeight, which returns 
//  the height of the rectangle. 
// 
// 4.) Define a "setter" named setWidth, which takes 
//  the new width of the rectangle and sets the 
//  rectangle's width to that value. 
// 
// 5.) Define a "setter" named setHeight, which 
//  takes the new height of the rectangle and sets 
//  the rectangle's height to that value 
// 
// 6.) Define a toString method, which returns 
//  a String representation of the rectangle. 
//  As an example, if the width of the rectangle is 
//  3 and the height of the rectangle is 4, this should 
//  return the String: 
// 
//  "Rectangle(3, 4)" 
// 
public Rectangle(int rectWidth, int rectHeight) { 
    rectWidth = width; 
    rectHeight = height; 
} 
public int getWidth() { 
    return width; 
} 
public int getHeight() { 
    return height; 
} 
public void setWidth(int rectWidth) { 
    width = rectWidth; 
} 
public void setheight(int rectHeight) { 
    height = rectHeight; 
} 
public String toString() { 
    return s; 
} 
    public static void main(String[] args) { 
    Rectangle s = new Rectangle("Rectangle"+"("+rectWidth+", 
"+rectHeight+")"); 
    System.out.println(s); 
    } 
} 
+0

Aber bitte machen Sie es zu einem * minimal * Beispiel. Die Kommentare usw. sind nicht relevant. Nun scheinen Sie zu versuchen, einen 'Rectangle (String)' -Konstruktor aufzurufen, der nicht existiert und die Variablen 'rectWidth' und' rectHeight' verwendet, die nicht im Gültigkeitsbereich sind. Es ist nicht klar, wie Sie das erwarten. –

+1

Ich vermute, die erste Zeile Ihrer 'main' Methode sollte' Rectangle s = new Rectangle (3, 4); 'zum Beispiel sein. –

+0

Dann versucht Ihre 'toString()' Methode, 's' zurückzugeben, was kein Feld in der Klasse ist ... –

Antwort

1

Ein paar Dinge: Ihr Erbauer war falsch:

Sie müssen die Instanzvariablen für „dieses“ Objekt setzen, die Sie erstellen, und Sie müssen den Konstruktor mit den richtigen Argumenten aufrufen. ToString sollte verwendet werden, um eine Zeichenfolgendarstellung dieser bestimmten Instanz von Rectangle zu erhalten. Versuchen Sie folgendes:

public class Rectangle { 
    // DO NOT MODIFY THE INSTANCE VARIABLES 
    // begin instance variables 
    private int width; 
    private int height; 
    // end instance variables 

    // TODO - write your code below this comment. 
    // You need to do the following: 
    // 
    // 1.) Define a constructor which takes two ints 
    //  representing the width and height, respectively. 
    //  The constructor should set its instance variables 
    //  equal to these values. 
    // 
    // 2.) Define a "getter" named getWidth, which returns 
    //  the width of the rectangle. 
    // 
    // 3.) Define a "getter" named getHeight, which returns 
    //  the height of the rectangle. 
    // 
    // 4.) Define a "setter" named setWidth, which takes 
    //  the new width of the rectangle and sets the 
    //  rectangle's width to that value. 
    // 
    // 5.) Define a "setter" named setHeight, which 
    //  takes the new height of the rectangle and sets 
    //  the rectangle's height to that value 
    // 
    // 6.) Define a toString method, which returns 
    //  a String representation of the rectangle. 
    //  As an example, if the width of the rectangle is 
    //  3 and the height of the rectangle is 4, this should 
    //  return the String: 
    // 
    //  "Rectangle(3, 4)" 
    // 
    public Rectangle(int rectWidth, int rectHeight) { 
     // "this" refers to the instance of Rectangle you are creating 
     // so this objects width and height are set to the values passed into the constructor... 
     this.width = rectWidth; 
     this.height = rectHeight; 
    } 
    public int getWidth() { 
     return width; 
    } 
    public int getHeight() { 
     return height; 
    } 
    public void setWidth(int rectWidth) { 
     width = rectWidth; 
    } 
    public void setheight(int rectHeight) { 
     height = rectHeight; 
    } 
    public String toString() { 
     // the toString returns a string representation for "this" object 
     return "Rectangle(" + this.width + "," + this.height + ")"; 
    } 
    public static void main(String[] args) { 
     Rectangle s = new Rectangle(5,4); 
       System.out.println(s); 
    } 
} 
Verwandte Themen