Help write a program(with given class and methods) *thank you*

C

caixingmei520

Write methods
public static double angle(Point2D.Double p, Point2D.Double q)
public static double slope(Point2D.Double p, Point2D.Double q)
public static double perimeter(Ellipse2D.Double e)
public static double area(Ellipse2D.Double e)

that computes the angle between the x-axis and the line joining two
points (measured in degrees), the slope of that line, and the
perimeter and area of ellipse e. Add these methods to a class
Geometry. Supply suitable preconditions.

OPTIONAL

Write methods
public static boolean isInside(Point2D.Double p, Ellipse2D.Double e)
public static boolean isOnBoundary(Point2D.Double p, Ellipse2D.Double
e)

that test whether a point is inside or on the boundary of an ellipse.
You can’t use the method contains available from the Ellipse class.
Add the methods to the class Geometry.



sorry i'm really a newbie and i'm failing computer science class
can anyone please help me?
thanks very much
we didn't learn to use applet and panel yet
so please don't use those
thank you !!!!
 
J

John B. Matthews

Write methods [...]
public static double angle(Point2D.Double p, Point2D.Double q)
public static double slope(Point2D.Double p, Point2D.Double q)
public static double perimeter(Ellipse2D.Double e)
public static double area(Ellipse2D.Double e)

The formulas are well known and usually taught in secondary school.
The area formula is exact, subject to the limits of double arithmetic.
For the perimeter, I doubt you're required to evaluate the relevant
elliptic integral, but the preconditions for the approximation
practically write themselves:

<http://en.wikipedia.org/wiki/Slope>
<http://www.efunda.com/math/areas/EllipseGen.cfm>
<http://en.wikipedia.org/wiki/Ellipse#Area_and_circumference>

[...]
Write methods
public static boolean isInside(Point2D.Double p, Ellipse2D.Double e)
public static boolean isOnBoundary(Point2D.Double p, Ellipse2D.Double e)

Ellipse2D implements the Shape interface, which has helpful methods for
implementing these tests.

<http://java.sun.com/javase/6/docs/api/java/awt/geom/Ellipse2D.Double.html>

[...]
orry 'm really a newbie and 'm failing computer science
class. [C]an anyone please help me?


Your teachers? Seriously, you need to review grammar and geometry.
Consider the possibility that you aren't ready to study computer
science yet.

[...]
 
C

caixingmei520

well, tell you the truth
i'm a high school student and people in my class either getting A's or
D&F's. The people getting As they already know computer science. And
everyone knows that the teacher just doesn't make any sense at all.
Well of course i know how to find the slope and area in geometry, I
learned them in 4th grade. I just have a hard time getting these in
the programs. This is an AP class and everyone was required to have
A's in Pre-Cal in order to get in this class And sorry that i have bad
grammars cuz i've only learned English for 3 years.

But thanks anyway!
hopefully I could really get some help for the programming part.

Can anyone PLZ help me with it?
thank you very much
 
E

Eric Sosman

well, tell you the truth
i'm a high school student and people in my class either getting A's or
D&F's. The people getting As they already know computer science. And
everyone knows that the teacher just doesn't make any sense at all.
Well of course i know how to find the slope and area in geometry, I
learned them in 4th grade. I just have a hard time getting these in
the programs. This is an AP class and everyone was required to have
A's in Pre-Cal in order to get in this class And sorry that i have bad
grammars cuz i've only learned English for 3 years.

But thanks anyway!
hopefully I could really get some help for the programming part.

Can anyone PLZ help me with it?

import java.awt.geom.Point2D.Double;
import java.awt.geom.Ellipse2D.Double;
class Geometry {
public static double angle(Point2D.Double p,
Point2D.Double q)
{
// TODO
}
public static double slope(Point2D.Double p,
Point2D.Double q)
{
// TODO
}
public static double perimeter(Ellipse2D.Double e) {
// TODO
}
public static double area(Ellipse2D.Double e) {
// TODO
}
public static boolean isInside(Point2D.Double p,
Ellipse2D.Double e)
{
// TODO
}
public static boolean isOnBoundary(Point2D.Double p,
Ellipse2D.Double e
{
// TODO
}
}

Replace the "// TODO" lines with what you learned in the
fourth grade, and you're all set.
> thank you very much

You're welcome. Free advice is worth every penny you
pay for it.
 
C

caixingmei520

This is what I have right now
but I think i missed something because it doesn't compile

The tester

import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;

public class GeometryTester
{
public static void main(String[] args)
{
Point2D.Double a = new Point2D.Double(1, 1);
Point2D.Double b = new Point2D.Double(3,3);
System.out.println("Angle: " + Geometry.angle(a, b)); //
expected answer: 90 degrees

a = new Point2D.Double(-1, 2);
b = new Point2D.Double(-2, 5);
System.out.println("Angle: " + Geometry.angle(a, b)); //expected
answer: 71.56 degrees

a = new Point2D.Double(3, 4);
b = new Point2D.Double(3, -6);
System.out.println("Slope: " + Geometry.slope(a, b)); //expected
answer: "undefined"

Ellipse2D.Double e = new Ellipse2D.Double(30, 40, 20, 20);
System.out.println("Area: " + Geometry.area(e)); //expected
answer: 314.16 square units

e = new Ellipse2D.Double(30, 40, 40, 30);
System.out.println("Perimeter: " + Geometry.perimeter(e)); //
expected answer: 110.52 units
}
}


the program

public class Geometry {
public static double angle(Point2D.Double p, Point2D.Double q)
{
return Math.atan2(q.y-p.y, q.x-p.x);
}
public static double slope(Point2D.Double p, Point2D.Double q)
{
return Math.tan(angle(p,q));
}
public static double perimeter(Ellipse2D.Double e)
{
return 1/2*Math.PI*(e.getWidth()+e.getHeight());

}
public static double area(Ellipse2D.Double e)
{
return 1/4*Math.PI*e.getHeight()*e.getWidth();
}
public static void main(String[] a)
{
System.out.println(slope(new Point2D.Double(2,5),new Point2D.Double
(8,3)));
}

}
 
C

caixingmei520

lol the one u showed me correct my problem but the compiler kept on
saying
java.awt.geom.Ellipse2D.Double is already defined in a single type
import
i guess is there anything to do with the tester?
again, thank you so much for helping ^__^
Cecilia
 
L

Lew

This is what I have right now
but I think i missed something because it doesn't compile

You need to copy and paste the error message into the post in future so we can
better help you.
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;

public class GeometryTester

This class is defined in GeometryTester.java, correct?
{
public static void main(String[] args)

It's best for Usenet not to use more than four spaces per indent level.
{
Point2D.Double a = new Point2D.Double(1, 1);

For this constructor use double values, i.e, (1.0, 1.0). It documents what
you're really doing that way.
Point2D.Double b = new Point2D.Double(3,3);

(3.0, 3.0)

You need to make your indentation consistent.
System.out.println("Angle: " + Geometry.angle(a, b)); //
expected answer: 90 degrees

Because Geometry.angle() uses Math.atan2(), the answer will be in radians, not
degrees.
a = new Point2D.Double(-1, 2); // (-1.0, 2.0)
b = new Point2D.Double(-2, 5);
System.out.println("Angle: " + Geometry.angle(a, b)); //expected
answer: 71.56 degrees

What is that in radians?
a = new Point2D.Double(3, 4);
b = new Point2D.Double(3, -6);
System.out.println("Slope: " + Geometry.slope(a, b)); //expected
answer: "undefined"

That's not going to happen. You might get back Double.NaN, though.
Ellipse2D.Double e = new Ellipse2D.Double(30, 40, 20, 20);

(30.0, 40.0, 20.0, 20.0)
System.out.println("Area: " + Geometry.area(e)); //expected
answer: 314.16 square units

e = new Ellipse2D.Double(30, 40, 40, 30);
System.out.println("Perimeter: " + Geometry.perimeter(e)); //
expected answer: 110.52 units
}
}


the program

public class Geometry {

This is defined in Geometry.java in the same directory as the tester class, right?
public static double angle(Point2D.Double p, Point2D.Double q)
{
return Math.atan2(q.y-p.y, q.x-p.x);

You should indent.
}
public static double slope(Point2D.Double p, Point2D.Double q)
{
return Math.tan(angle(p,q));
}
public static double perimeter(Ellipse2D.Double e)
{
return 1/2*Math.PI*(e.getWidth()+e.getHeight());

1/2 in integer arithmetic yields 0.
}
public static double area(Ellipse2D.Double e)
{
return 1/4*Math.PI*e.getHeight()*e.getWidth();

1/4 in integer arithmetic yields 0.
}
public static void main(String[] a)
{
System.out.println(slope(new Point2D.Double(2,5),new Point2D.Double
(8,3)));
}

}

So, what was the error message?
 
L

Lew

lol the one u

"you", not "u".
showed me correct my problem but the compiler kept on saying
java.awt.geom.Ellipse2D.Double is already defined in a single type
import
i guess is there anything to do with the tester?
again, thank you so much for helping ^__^

None of the code you showed us imports Ellipse2D.Double.

Perhaps you haven't shown us your actual source code?
 
C

caixingmei520

So basically this is what I have and the tester is given by the
teacher. So I don't think we can do anything with the tester

still, the compiler says java.awt.geom.Ellipse2D.Double is already
defined in a single type import


import java.awt.geom.Point2D.Double;
import java.awt.geom.Ellipse2D.Double;
public class Geometry
{
public static double angle(Point2D.Double p, Point2D.Double q)
{
return Math.atan2(q.y-p.y, q.x-p.x);
}
public static double slope(Point2D.Double p, Point2D.Double q)
{
return Math.tan(angle(p,q));
}
public static double perimeter(Ellipse2D.Double e)
{
return 1/2*Math.PI*(e.getWidth()+e.getHeight());

}
public static double area(Ellipse2D.Double e)
{
return 1/4*Math.PI*e.getHeight()*e.getWidth();
}

public static void main(String[] args)
{
System.out.println(slope(new Point2D.Double(2,5),new Point2D.Double
(8,3)));
}

}
 
L

Lew

So basically this is what I have and the tester is given by the
teacher. So I don't think we can do anything with the tester

still, the compiler says java.awt.geom.Ellipse2D.Double is already
defined in a single type import


import java.awt.geom.Point2D.Double;
import java.awt.geom.Ellipse2D.Double;

Here you import both Double classes. This is an error.

I suggest you only import Point2D and Ellipse2D, not the nested classes.
public class Geometry
{
public static double angle(Point2D.Double p, Point2D.Double q)

Here you refer to the imported class without a fully-qualifed name (FQN).

What if you only import Point2D and Ellipse2D, and keep the method
declarations the same? That way the compiler will know you have Point2

And what happened to your indentation, hmm?

Pick an indentation level *greater than zero* spaces per level, and less than
or equal to four, for Usenet. (Don't use TABs for Usenet indentation, just
spaces.)
 
J

John B. Matthews

(e-mail address removed) wrote:

[...]
still, the compiler says java.awt.geom.Ellipse2D.Double is already
defined in a single type import

import java.awt.geom.Point2D.Double;
import java.awt.geom.Ellipse2D.Double;

Look at the GeometryTester import list; use the same approach.

[...]
public static double perimeter(Ellipse2D.Double e) {
return 1/2*Math.PI*(e.getWidth()+e.getHeight());
}

As already pointed out to you, this returns zero; the expected answer
requires Ramanujan's approximation:

public static double area(Ellipse2D.Double e) {
return 1/4*Math.PI*e.getHeight()*e.getWidth();
}

This also returns zero; the correct formula uses the semimajor axes, not
the width and height.

public class GeometryTester {
...
Point2D.Double a = new Point2D.Double(1, 1);
Point2D.Double b = new Point2D.Double(3, 3);
... // expected answer: 90 degrees

As these two points define the line y = x, I would expect the slope to
be pi/4 radians or 45 degrees.
a = new Point2D.Double(-1, 2);
b = new Point2D.Double(-2, 5);
... //expected answer: 71.56 degrees

These two points define the line y = -3x-1; I get 108.4+ degrees. The
points (1, 2) and (2, 5) define the line 3x-1, which has the expected
angle.
orry that have bad grammar [because] 've only learned
English for 3 years. ... Can anyone [please] help me with it?


For some years I had the responsibility of editing the manuscripts of
talented researchers whose native language was not English. Despite
their varied cultures and backgrounds, they all shared a common desire:
that their writing should reflect their considerable education.
Typographical errors are inevitable, but careless grammar (in any
language) can become habitual and reflect poorly on the writer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top