-3

I have a class Rectangle laid out like this:

package Inheritance;

/**
 *
 * @author Jacob
 */
public class Rectangle {
final private int length;
final private int width;

public Rectangle (int l, int w)
{
    length = l;
    width = w;
}

public int getLength ()
{
    return length;
}

public int getWidth ()
{
    return width;
}

@Override
public String toString ()
{
    return String.format ("Rectangle (%dX%d)", length, width);
}

}

I then need to create class square in the following way:

ad Square: Square extends Rectangle / No fields are declared in class Square / It has a parameterized constructor with (only) one parameter / The parameter is used to initialize both fields of Rectangle / It has a method called getSide to expose the side-length of the square / Override the toString method so that it will return a String of the following form: / Square(side) e.g. Square(4)

The values for the sides are going to be hard coded. Rectangle is going to have a width of 4. In order to get the side of the square to be 4 do I create an instance of rectangle and call the method getWidth and set that as the side length. Thats how I would think to do it but in that case I would only be using one of the fields so, My question is how do I initialize both fields? Can I call Rectangle and make length and width equal or is there some other way I should do it?

Here is the code for my Square class:

public class Square {

    public Square (int side)
    {
        super(side, side);
    }

    public int getSide ()
    {
        return side;
    }

    @Override
    public String toString ()
    {
        return String.format ("Square (%d)", side);
    }
}

For the line super(side, side) I get the error constructor Object in class Object cannot be applied to given types. Required no arguments, found int, int. For my return statements I get that it cannot find the variable side.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3440443
  • 13
  • 2
  • 8
  • smell like a home work. is it? – Ruchira Gayan Ranaweera Sep 26 '14 at 04:09
  • 1
    In `Square`, you should be taking a single parameter which is the size, you would then simply need to call `super(size, size)` within the `Square`s constructor... – MadProgrammer Sep 26 '14 at 04:11
  • You should probably start by following a book or a tutorial. Like the Java Tutorial's section on [inheritance](http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html) – vanza Sep 26 '14 at 04:15
  • "The values for the sides are going to be hard coded." I assume that you mean these values are hardcoded in `main()` or wherever you create `Rectangle` and `Square` objects. They absolutely should **not** be hardcoded any where else. – Code-Apprentice Sep 26 '14 at 04:17
  • Yes, The values will be hard coded in the main method where the objects will be created. – user3440443 Sep 26 '14 at 04:27

1 Answers1

0

The values for the sides are going to be hard coded.

I assume that you mean that you will hardcode the values for the width and length when you create a Rectangle and Square object (for example in main()). These values should absolutely not be hardcoded any where in the Rectangle and Square classes.

Rectangle is going to have a width of 4. In order to get the side of the square to be 4 do I create an instance of rectangle and call the method getWidth and set that as the side length.

Not at all. Rather, Square should have its own constructor which calls the Rectangle constructor with the same value for both the width and length:

public Square(int side) {
    super(side, side); // Set the width and length to the same value.
}
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I attempted to use super but I put length and width as the parameters. I corrected with what you had suggested but it says that super required no arguments. Also when writing the getSide method am I returning the width from the getWidth method? – user3440443 Sep 26 '14 at 04:28
  • @user3440443 Please show the code for your Square class in order for us to help you. – Code-Apprentice Sep 26 '14 at 20:38
  • I added the Square class. – user3440443 Sep 28 '14 at 05:08
  • I managed to figure it out after some more research on my own. I didn't understand which class of my five classes was the superclass or to tell if one was. I found out that I had to define my Square class as the subclass in my class declaration. I then called super.getWidth () to get my value for my side as they will be the same. The whole time I wasn't able to figure that out because I was working on understanding the logic for my problem and then the syntax for the super command. After I had tried incorporate your suggestion for the syntax I had researched the error with no luck. – user3440443 Sep 28 '14 at 05:35