DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

Thread: class hierarchy with loops problem! reward!

1. Registered User
Join Date
Nov 2004
Posts
5

class hierarchy with loops problem! reward!

I need to design class hierarchy which has a super-class Pythagoras and two sub-classes (Vieta and Wallis), which can be used to produce estimates of PI, using either the method proposed by Vieta or that of Wallis. The only input by the user is a "number of terms"

Vieta's method:

2/Pi = SQRT(2)/2 *sqrt(2+sqrt(2))/2 *sqrt(2+sqrt(2+sqrt(2)))/2 etc etc

Wallis's method:

PI/2 = 2/1 * 2/3 * 4/3 * 4/5 etc

i know this requires a sort of loop construct but am having difficulty writing the methods for the two directions of the loop....here's my code can anyone help?

/****************************************************/
/* */
/* PYTHAGORAS CLASS */
/* */
/****************************************************/

class PythagorasClass {

//----------------------------FIELDS-----------------------------

private int numberOfTerms;
private int firstTerm;

//----------------------------CONSTRUCTORS-----------------------

/* Constructor */

public PythagorasClass (int numN, int fTerm) {
numberOfTerms = numN;
firstTerm = fTerm;
}

/****************************************************/
/* */
/* VIETA CLASS */
/* */
/****************************************************/

class VietaClass extends PythagorasClass {

//----------------------------FIELDS------------------------------

private static double sqrt;
private static double (sqrt + 2);
private double variable1 = 0;
private double variable2 = 0;

//---------------------------CONSTRUCTORS-------------------------

public VietaClass (int numN, int fTerm) {
super(numN, fTerm);

}

//--------------------------METHODS-------------------------------

/* Output terms method */

public void outputTerms() {
int loopParameter;
final int START_CONDITION = 0;

for(loopParameter = START_CONDITION;
loopParameter < numberOfTerms;loopParameter++)
System.out.print(WHAT TO PUT HERE?); <<<<<<<<<<
System.out.print("\n\n");
}

/****************************************************/
/* */
/* WALLIS CLASS */
/* */
/****************************************************/

class WallisClass extends PythagorasClass {

//---------------------------FIELDS-------------------------------

private static double 1;
private static double 2;
private static double 3;
private static double 4;
private static double 5;
private double variable1 = 0;
private double variable2 = 0;

//--------------------------CONSTRUCTORS---------------------------

public WallisClass (int numN) {
super(numN);

//--------------------------METHODS--------------------------------

}
}

PS - if someone can solve this email me @ fatboyfat63@hotmail.com and I shall reward you with paypal!

2. Senior Member
Join Date
Mar 2004
Posts
635
that's your code? All you've done is declare variables. Have you tried the problem yet? Nobody is going to do your homework for you.
For Wallis's method, you're going to need a function that returns prime numbers.

3. I agree w. Phaelax here

In your code I see this:

private static double 1;
private static double 2;
private static double 3;
private static double 4;
private static double 5;

Are you serious ?

4. I think I'll get a bit metaforical here:

We are dealing w. eggs that grows new layers for as long as you wish. If you want to keep control over how much yolk there is left (the value) in an egg that, each time it gets a new layer, converts the inner layer to yolk, you must find the rule/process that the egg uses to accomplish this. When you have found that rule you code it.

We are here dealing with two sorts of layergrowing eggs, and the only thing we know that is common in their way of inner “mutation” is that they do it in steps, and that each step yields an increased yolk (value). We know that both are started off by being told how many layers they shall grow, and that they must be able to tell what their value is (smart eggs) when they have finished “mutating”. We are then able to define an abstract superclass:
Code:
```public abstract class PIEstimator {
protected double value=1.0d;
protected int nOfSteps=0;
public PIEstimator () {}
public PIEstimator (int nOfSteps) {
this.nOfSteps=nOfSteps;
}
public void estimate() {
for (int stepNo=0; stepNo<nOfSteps; stepNo++) {
value*=doNextProcessStep(stepNo);
}
}
public abstract double  getResult();
public abstract void  tellResult();
public abstract double doNextProcessStep(int stepNo);
public static void main(String [] args) {
VietaEstimator ve=new VietaEstimator(100);
ve.estimate();
ve.tellResult();
}
}```
Then youll have to code the two subclasses, one for each estimation method.

5. Senior Member
Join Date
Mar 2004
Posts
635
i think its "metaphorical".
But anyway, you have to look at the sequence and find the pattern. Find the pattern, then it's easy.

6. You mean something along these lines ?

Code:
```class VietaEstimator extends PIEstimator {
public VietaEstimator(int nOfSteps) {
super(nOfSteps);
}
public void tellResult() {
System.out.println("The result according to Vieta is: "+getResult());
}
public double getResult() {
return 2.0d/value;
}
public double doNextProcessStep (int stepNo) {
double d=Math.sqrt(2.0d);
for (int i=0; i<stepNo; i++) {
d=Math.sqrt(2.0d+d);
}
return d/2.0d;
}```
Then use it like this:
Code:
```public static void main(String [] args) {
VietaEstimator ve=new VietaEstimator(100);
ve.estimate();
ve.tellResult();
}```
As I read this, an important part of the solution was to implement a class hierachy.

Metaforical, well, I think my Norwegian got the best of me there

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise