• Post Reply Bookmark Topic Watch Topic
  • New Topic
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

A Quadrilateral extend program

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
*/
public abstract class Quadrilateral {
// 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
Rectangle extends Quadrilateral.
Constructor:
public Rectangle( double lowerLeftX, double lowerLeftY,
double width, double height);
Class: Square
Square extends Quadrilateral.
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
___________________________________-
Quadrilateral.java

Rectangle.java

Square.java

TestQuad.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:
C:\myjava\assignment3\problem1>javac Quadrilateral.java
Quadrilateral.java:25: 'class' or 'interface' expected
public double getX(int corner)
^
Quadrilateral.java:38: 'class' or 'interface' expected
}
^
Quadrilateral.java:47: 'class' or 'interface' expected
}
^
Quadrilateral.java:22: cannot resolve symbol
symbol : variable width
location: class Quadrilateral
return width*length;
^
Quadrilateral.java:22: cannot resolve symbol
symbol : variable length
location: class Quadrilateral
return width*length;
^
5 errors

------------------
Thanks, Dianne
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Quadrialteral.java

Rectangle.java

Square.java

TestQuad.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
C:\myjava\assignment3\problem1>javac TestQuad.java
TestQuad.java:13: cannot resolve symbol
symbol : constructor Rectangle ()
location: class Rectangle
Quadrilateral[] quadrilateralArray = {new Rectangle(), new Square()}
;
^
TestQuad.java:13: cannot resolve symbol
symbol : constructor Square ()
location: class Square
Quadrilateral[] quadrilateralArray = {new Rectangle(), new Square()}
;
^
TestQuad.java:18: cannot resolve symbol
symbol : method getlowerLeftX ()
location: class Quadrilateral
System.out.println( "lowerLeftX is " + quadrilateralArra
y[i].getlowerLeftX());
^
TestQuad.java:19: cannot resolve symbol
symbol : method getlowerLeftY ()
location: class Quadrilateral
System.out.println( "lowerLeftY is " + quadrilateralArra
y[i].getlowerLeftY());
^
TestQuad.java:20: cannot resolve symbol
symbol : method getarea ()
location: class Quadrilateral
System.out.println( "area is " + quadrilateralArray[i].g
etarea());
^
TestQuad.java:21: cannot resolve symbol
symbol : method getperimeter ()
location: class Quadrilateral
System.out.println( "perimeter is " + quadrilateralArray
[i].getperimeter());
^
6 errors
C:\myjava\assignment3\problem1>
End of errors.
Thanks for your help.
------------------
Thanks, Dianne
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Quadrialteral.java

Square.java

Rectangle.java

TestQuad.java

It prints out all zeros for everything.
------------------
Thanks, Dianne
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic