Results 1 to 9 of 9
  1. #1
    fleg is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default the variable thing

    Hi I would like to ask for a favor... I have fully functional code here but I would like to ask you if you can improve this code... Class GeneralShape implements interface Shape and there is a method toString is there any possibility that I will not have in Classes Rectangle and Circle the toString method bud the toString method in GeneralShape will have something like a variable which will be able to decide what shape it is it looks like this... Maybe it sounds really stupid... but i will try to explain it in code..

    public interface Shape
    {

    /**
    * It returns the color of the shape.
    *
    * @return Name of the color
    */
    String getColor();

    /**
    * It returns maximal height of the shape.
    *
    * @return maximal height in cm
    */
    double getHeight();

    /**
    * It returns maximal width of the shape.
    *
    * @return maximal width in cm
    */
    double getWidth();

    /**
    * It returns a string representation of the shape.
    * The returned value has pattern '<color> <type of shape>',
    * e.g. "red shape", "blue circle", etc.
    *
    * @return string representation of the shape
    */
    String toString();

    }

    public class GeneralShape implements Shape
    {
    private double width;
    private double height;
    private String color;

    public GeneralShape (String color,double width,double height){
    this.color= color;
    this.width= width;
    this.height= height;


    }

    public GeneralShape(String color,double width){
    this.width=width;
    }

    public String getColor(){
    return color;
    }

    public double getHeight(){
    return height;
    }

    public double getWidth(){
    return width;
    }

    public String toString(){
    return this.color+ "shape" ;
    // so instead of "shape" there will be some variable which will be able to decide if the object is circle rectangle or general shape..
    }
    }

    public Rectangle(String color,double width,double height){
    super(color,width,height);
    }

    public boolean isSquare(){
    return getHeight()==getWidth();
    }

    public String toStrin(){
    if (isSquare()) {return getColor() + "square";
    }
    else{
    return getColor() + "rectangle";
    }
    }

    }

    public Circle(String color,double width){
    super(color,width);

    }

    public String toString(){
    return getColor() + "circle" ;
    }

    thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You could make GeneralShape abstract, with an abstract protected method:

    Java Code:
        /** Returns a simple description of the shape. */
    protected abstract String getShapeDescription();
    }

    Then implement the GeneralShape's toString() method as:

    Java Code:
    public String toString() {
        return color + getShapeDescription();
    }

    I'm not sure I would do this for toString() - but there might be other things that you want the base class to have control of.

  3. #3
    fleg is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    but in that getShapeDescription what should i write there to the method?
    Should i make something like this, that i add a new variable

    private String typeOfShape;
    public GeneralShape(String color,doule width,double height){
    this.color= color;
    this.width= width;
    this.height= height;
    typeOfShape= "shape";
    }

    public Rectangle(String color,double width,double height){
    super(color,width,height);
    typeOfShape= "rectangle"
    }

    /** Returns a simple description of the shape. */
    protected abstract String getShapeDescription();
    return typeOfShape;
    }

    or is it possible to do such thing as typeOfShape or the getShapeDescription will be able to return the name of Class? Because i dont see any other option than this... but tthx for helping me

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,963
    Rep Power
    19

    Default

    You make the GeneralShape class abstract, so there is no need for it to say what getShapeDescription() does. That is up to the subclasses. So Rectangle will define it as:
    Java Code:
    protected String getShapeDescription() {
        return "rectangle";
    }
    And GeneralShape does not then need a typeOfShape attribute.

  5. #5
    fleg is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    thanks but when i use abstract method i will not be able to define it for GeneralShape class if oyu know what i mean, that i will create a GeneralShape general = new GeneralShape ("red",10,20); and i want that toString will write
    => red shape so how to define it for the abstract class too? thanks..

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,963
    Rep Power
    19

    Default

    Oh, just give each one its own toString().
    This is getting more complicated than it needs to be.
    Didn't realise you wanted to be able to instantiate a GeneralShape as well.

  7. #7
    fleg is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    yeah its quite complicated... in fact i wanted to ask if it is possible to do it somehow because, firstly they had their own toStrings with description of shape... ňokey nevermind thanks for your help... i should have written that i want to do it for GeneralShape too...

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,963
    Rep Power
    19

    Default

    It's not complicated, it's just not worth the effort.
    You're not gaining anything, saving any code, simplifying the existing code.

  9. #9
    fleg is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Yeah the point was to make the code more simplier and easier to read.. because they want it at school...i would like to give you a +rep for help but i dont see where :D... so i think this thread can be close...

Similar Threads

  1. Trying to understand the whole 2D thing.
    By xael in forum Java 2D
    Replies: 4
    Last Post: 10-06-2010, 01:41 AM
  2. please i need help with a simple thing
    By zohdi in forum New To Java
    Replies: 28
    Last Post: 05-30-2010, 01:41 AM
  3. A new thing arrives...
    By ewomack in forum Introductions
    Replies: 3
    Last Post: 10-09-2009, 09:16 PM
  4. What did i do wrong on thing method?
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 08:37 AM
  5. PLz i really need help on this final thing
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •