quick java constructor question

Danimal1209

Senior member
Nov 9, 2011
355
0
0
I'm on my final project for Java this quarter. I have finished it but there is extra credit I want to do. Here is my problem.

I am making a square class that is a child class of rectangle.

Rectangle has these fields in its constructor:
shapeId, redIn, greenIn, blueIn, lengthIn, widthIn

And square needs the same MINUS length, since all sides are the same.

I cannot use only width, since I'm inheriting from rectangle I need the length right? Because I will be inputing only 1 value for width. But, part of the extra credit is to only have the 5 constructor parameters. I'm not sure how to eliminate length.

any help or suggestions are appreciated.
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
what is redIn, greenIn, blueIn? If that's color take a look at java.awt.Color

How come you have In appended to length and width? Is that suppose to be inches? If so spell it out to be inline with java naming conventions.

A square would have the same length and width. Set the constructor to accept a value for side which will be used for your length and width.

Did you make an abstract class that these objects implement?
 
Last edited:

Monster_Munch

Senior member
Oct 19, 2010
873
1
0
I guess they want you to do something like this

Constructor for Rectangle class

Code:
public Rectangle (int shapeId, int redIn, int greenIn, int blueIn, int lengthIn, int widthIn) {
    // some shit happens
}

Constuctor for Square class

Code:
public Square (int shapeId, int redIn, int greenIn, int blueIn, int widthIn) {
   super(shapeId, redIn, greenIn, blueIn, widthIn, widthIn);
}

This way your square class has only 5 params and the width is passed to the rectangle constructor for both length and width.
 
Last edited:

Danimal1209

Senior member
Nov 9, 2011
355
0
0
RedIn etc are the parameters for the constructor. I MUST use them along with widthIn(In just means its the value going IN to the constructor, its how my teacher said to do it.)

I can't use a value called side, it must be width.

My problem is that the parent has 6 parameters in the constructor, while my child class has only 5.
 

Danimal1209

Senior member
Nov 9, 2011
355
0
0
I guess they want you to do something like this

Constructor for Rectangle class

Code:
public Rectangle (int shapeId, int redIn, int greenIn, int blueIn, int lengthIn, int widthIn) {
    // some shit happens
}
Constuctor for Square class

Code:
public Square (int shapeId, int redIn, int greenIn, int blueIn, int widthIn) {
   super(shapeId, redIn, greenIn, blueIn, widthIn, widthIn);
}

this would normally work. but only 1 value is coming in for width, not 2.
 

Danimal1209

Senior member
Nov 9, 2011
355
0
0
This is what I have right now:

public class Square extends Finals.Rectangle{

private Square (String idIn, int redIn, int greenIn, int blueIn, double widthIn){
super(idIn, redIn, greenIn, blueIn, widthIn);
}

}

The error netbeans is giving me says:

"actual and formal arguments lists differ in length"

So, I assume it has something to do with me trying to take only 5 of the 6 parameters from the parent class.
 

Monster_Munch

Senior member
Oct 19, 2010
873
1
0
super() comes from the Rectangle class so it needs both width and length. That's why I said you should pass in the widthIn for both.
 

bobross419

Golden Member
Oct 25, 2007
1,981
1
0
Gotta love all the "Help me please" threads that get solved by the OP without ever saying what they did to solve it. Forums don't exist just for asking questions, but also for finding answers. Why not take the 10 seconds to post up how you got it working?
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
RedIn etc are the parameters for the constructor. I MUST use them along with widthIn(In just means its the value going IN to the constructor, its how my teacher said to do it.)

I can't use a value called side, it must be width.

My problem is that the parent has 6 parameters in the constructor, while my child class has only 5.

lol, dang. Why didn't he do shapeIdIn too.

that would be weird if he didn't let you use your own variable name for your square object. If that's the case then you'll just do what I was saying at the end (replace the word side with widthIn if that's what he wants.)

looks like your good to go though.
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
Originally Posted by Monster_Munch
I guess they want you to do something like this

Constructor for Rectangle class

Code:

public Rectangle (int shapeId, int redIn, int greenIn, int blueIn, int lengthIn, int widthIn) {
// some shit happens
}

Constuctor for Square class

Code:

public Square (int shapeId, int redIn, int greenIn, int blueIn, int widthIn) {
super(shapeId, redIn, greenIn, blueIn, widthIn, widthIn);
}

this would normally work. but only 1 value is coming in for width, not 2.

Monster_Munch is right and this is what I was saying before

oh think i got it.

High five!
 
Last edited:

Gooberlx2

Lifer
May 4, 2001
15,381
6
91
Gotta love all the "Help me please" threads that get solved by the OP without ever saying what they did to solve it. Forums don't exist just for asking questions, but also for finding answers. Why not take the 10 seconds to post up how you got it working?

I think he meant he understood what they were saying...
Without the other constructor parameters, here's how I wrote it:

Code:
public class Main {
    public static void main(String[] args){
        Rectangle r = new Rectangle(10,20);
        Square s = new Square(6);
    }
}


public class Rectangle {
    protected int length;
    protected int width;
    public Rectangle(int length, int width){
        this.length = length;
        this.width = width;
        System.out.println("I am a rectangle. My length is: "+this.length+". My width is: "+this.width);
    }    
}

public class Square extends Rectangle{
    public Square(int side){
        super(side, side);
        System.out.println("...which actually just makes me a square with side length: "+this.length);
    }    
}

Which would print the following:
Code:
I am a rectangle. My length is: 10. My width is: 20
I am a rectangle. My length is: 6. My width is: 6
...which actually just makes me a square with side length: 6
 
Last edited: