<p>Quadrialteral test program is not getting the correct answers</P>
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dianne_Calhoun
Posted On:   Monday, October 8, 2001 03:43 PM

My program all compiles, but the output is not correct. Could someone take a look and see what I'm doing wrong. Thanks Quadrilateral.java // This is an abstract class Quadrilateral program /** * Abstract class for quadrilateral shape object. * Corner numbers are from 1 to 4: * 2------3 * | | * 1------4 */ import java.lang.String.*; public abstract class Quadrilateral { // private attributes private double lowerLeftX[] = new double[4]; private double lowerLeftY[] = new double[4]; // public attributes double area; double perimeter; // public methods public double get   More>>

My program all compiles, but the output is not correct. Could someone take a look and see what I'm doing wrong. Thanks


Quadrilateral.java


			
// This is an abstract class Quadrilateral program
/**
* Abstract class for quadrilateral shape object.
* Corner numbers are from 1 to 4:
* 2------3
* | |
* 1------4
*/

import java.lang.String.*;

public abstract class Quadrilateral
{
// private attributes
private double lowerLeftX[] = new double[4];
private double lowerLeftY[] = new double[4];

// public attributes
double area;
double perimeter;

// public methods
public double getArea()
{
return area;
}

public double getlowerLeftX(int corner)
{
return lowerLeftX[corner -1];
}
public double getlowerLeftY(int corner)
{
return lowerLeftY[corner -1];
}

public double getPerimeter()
{
return perimeter;
}

protected void setlowerLeftX(int corner, double value)
{
lowerLeftX[corner -1] = value;
}

protected void setlowerLeftY(int corner, double value)
{
lowerLeftY[corner -1] = value;
}

public void setArea(double area)
{
this.area = area;
}

public void setPerimeter(double perimeter)
{
this.perimeter = perimeter;
}
}

Square.java


			
// This program is Square extends Quadrilateral

import java.lang.String.*;

public class Square extends Quadrilateral
{

private double side;
private double lowerLeftX;
private double lowerLeftY;

// Constructor
public Square()
{
side = 1;
lowerLeftX = 1;
lowerLeftY = 1;
}

public Square(double lowerLeftX, double lowerLeftY, double side)
{
this.side = side;
this.lowerLeftX = lowerLeftX;
this.lowerLeftY = lowerLeftY;
}

public double area()
{
double area;
area = (double)(side * side);
return area;
}

public double perimeter()
{
double perimeter;
perimeter = side * 4;
return perimeter;
}

}



Rectangle.java


			
// This program is Rectangle extends Quadrilateral

import java.lang.String.*;

public class Rectangle extends Quadrilateral
{
double width;
double height;
double lowerLeftX;
double lowerLeftY;

// Constructor
public Rectangle()
{
width = 1;
height = 1;
lowerLeftX = 1;
lowerLeftY = 1;
}

public Rectangle(double lowerLeftX, double lowerLeftY, double width, double height)
{
this.lowerLeftX = lowerLeftX;
this.lowerLeftY = lowerLeftY;
this.width = width;
this.height = height;
}

public double area()
{
double area;
area = width * height;
return area;
}

public double perimeter()
{
double perimeter;
perimeter = 2 * ( height + width);
return perimeter;
}

}

TestQuad.java


			
// This program tests the Quadrilateral program

import java.lang.String.*;

public class TestQuad
{
public static void main( String args[] )
{
// Quadrilateral array variable
Quadrilateral[] quadrilateralArray = {new Rectangle(0,0,6,4), new Square(0,0,12)};

for (int i = 0; i < quadrilateralArray.length; i++)
{
// Printing Out of Objects
System.out.println( "lowerLeftX is " + quadrilateralArray[i].getlowerLeftX(1));
System.out.println( "lowerLeftY is " + quadrilateralArray[i].getlowerLeftY(4));
System.out.println( "area is " + quadrilateralArray[i].getArea());
System.out.println( "perimeter is " + quadrilateralArray[i].getPerimeter());
}
}
}

			
Wrong Answer:
C:myjavaassignment3problem1>javac TestQuad.java

C:myjavaassignment3problem1>java TestQuad
lowerLeftX is 0.0
lowerLeftY is 0.0
area is 0.0
perimeter is 0.0
lowerLeftX is 0.0
lowerLeftY is 0.0
area is 0.0
perimeter is 0.0

C:myjavaassignment3problem1>

Thanks for your help, if anyway possible, I have to have this for a class in an hour(5:30), I just couldn't get it to work.

   <<Less
About | Sitemap | Contact