programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
I've gotten a ways on this program, but am stuck as what I'm doing wrong. Here are my instructions:
/**
* Abstract class for quadrilater shape object.
* Corner numbers are from 1 to 4:
* 2 ---- 3
* | |
* 1 ---- 4
*/
// private attributes
private double x[] = new double[4];
private double y[] = new double[4];
// public methods
public double getArea() {
// fill in here
}
public double getX(int corner) {
return x[corner - 1];
}
public double getY(int corner) {
return y[corner - 1];
}
public double getPerimeter() {
// fill in here.
}
protected void setX(int corner, double value) {
x[corner - 1] = value;
}
protected void setY(int corner, double value) {
y[corner - 1] = value;
}
}
Create these class:
Class: Rectangle
Constructor:
public Rectangle( double lowerLeftX, double lowerLeftY,
double width, double height);
Class: Square
Constructor:
public Square( double lowerLeftX, double lowerLeftY,
double side);
Create a TestQuad.java driver program that creates a
rectangle, square object and puts them
in a Quadrilateral[] array variable. Loop through the array
and print out the area, perimeter, and the corner positions
of shape.
Note that constructor for each concreate class is different.
End of Instructions
___________________________________-

Rectangle.java

Square.java

Here are the errors I'm getting, they are less than I was getting, but I'm not sure what I need to do now. Any suggestions would be great. Thanks
Errors:
public double getX(int corner)
^
}
^
}
^
symbol : variable width
return width*length;
^
symbol : variable length
return width*length;
^
5 errors

------------------
Thanks, Dianne

Ranch Hand
Posts: 34
• Number of slices to send:
Optional 'thank-you' note:
Dianne,
You have one bracket too many in the getArea() method of Quadrilateral. This is in effect ending the class at that line!
That's why you are getting the "class or interface expected" at the next line.
It's really worthwhile using an editor that has bracket-matching capability to avoid errors like this. Editors can spark religious wars but two that I like are TextPad (Windows only) or jEdit which is written entirely in Java and runs on just about anything.
Martin

Dianne Calhoun
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
I have gotten a lot further and am stuck again. I have all of the code compiling except the TestQuad.java program. Could you please take a look and see what I'm doing wrong. The instructions are still the same as above, but here is my new code.

Rectangle.java

Square.java

<br /> Here are my errors:<br /> Microsoft Windows 2000 [Version 5.00.2195]<br /> (C) Copyright 1985-2000 Microsoft Corp.<br /> C:\myjava>cd assignment3
C:\myjava\assignment3>cd problem1
symbol : constructor Rectangle ()
location: class Rectangle
;
^
symbol : constructor Square ()
location: class Square
;
^
symbol : method getlowerLeftX ()
System.out.println( "lowerLeftX is " + quadrilateralArra
y[i].getlowerLeftX());
^
symbol : method getlowerLeftY ()
System.out.println( "lowerLeftY is " + quadrilateralArra
y[i].getlowerLeftY());
^
symbol : method getarea ()
System.out.println( "area is " + quadrilateralArray[i].g
etarea());
^
symbol : method getperimeter ()
System.out.println( "perimeter is " + quadrilateralArray
[i].getperimeter());
^
6 errors
C:\myjava\assignment3\problem1>
End of errors.
------------------
Thanks, Dianne

"The Hood"
Posts: 8521
• Number of slices to send:
Optional 'thank-you' note:
Dianne,
1 - Your Rectangle class does not have a constructor that takes no parameters. Same for your Square class. Remember that as soon as you supply even ONE constructor the compiler will not provide a default constructor for you. You tried to create a Rectangle using "new Rectangle()" in your array initialization.
2 - you used "quadrilateralArray[i].getlowerLeftX()" but there is no getlowerLeftX() method in class Quadrilateral, there is only a getX() and getY().
3 - you do not have a getArea() method in class Quadrilateral.
4 - you do not have a getperimeter() method in class Quadrilateral. You DO have a getPerimeter() which is spelled differently (capitalization counts).

Dianne Calhoun
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Well, I get everything to compile, but it's not the right answer. So, I must not be doing what you told me too, but I'm not sure where else it is that you said I did something wrong. could you please take another look and see if you can help me fiugure it out. I've tried lots of things, and nothing is working.

Square.java

Rectangle.java

It prints out all zeros for everything.
------------------
Thanks, Dianne

Cindy Glass
"The Hood"
Posts: 8521
• Number of slices to send:
Optional 'thank-you' note:
Your Quadrilateral has 2 variables called lowerLeftX and lowerLeftY which are arrays with 4 elements (?? You have 4 lowerLeftX's??).
Your Square and Rectangle have 2 variables which are not arrays called lowerLeftX and LowerLeftY. You set these values in your Square and Rectangle methods, but then print out the array values from above, which are amazingly enough, still zero, because you never put anything in those array elements.
I would expect making the variables in the super class "not arrays" and not re-declaring them in the sub-classes would be a better approach. Then each object would know it's own lowerLeftX and lowerLeftY.

 Consider Paul's rocket mass heater.