1. Member
Join Date
Jan 2008
Posts
36
Rep Power
0

hey guys long time no see, can someone help me I'm having trouble understanding how to work out two of these questions.

/**
* A class for numeric progressions.
*/
public class Progression {

/** First value of the progression. */
protected long first;

/** Current value of the progression. */
protected long cur;

/** Default constructor. */
Progression() {
cur = first = 0;
}

/** Resets the progression to the first value.
*
* @return first value
*/
protected long firstValue() {
cur = first;
return cur;
}

/** Advances the progression to the next value.
*
* @return next value of the progression
*/
protected long nextValue() {
return ++cur; // default next value
}

/** Prints the first n values of the progression.
*
* @param n number of values to print
*/
public void printProgression(int n) {
System.out.print(firstValue());
for (int i = 2; i <= n; i++)
System.out.print(" " + nextValue());
System.out.println(); // ends the line
}

}

1. Implement a Java class (C2_2Progression) that extends the Progress class so that each value in the progression is the absolute value of the difference between the previous two values. You should include a default constructor that starts with 2 and 200 as the first two values and a parameteric constructor that starts with a specified pair of numbers as the first two values. You must name your class C2_2Progression .

2. Write a Java class (C2_3Progression.java) that extends the Progression class so that each value in the progression is the floor of the square root of the previous value. You are not allowed to use the Math.sqrt() method from the Java Math library. You should include a default constructor that has 65,536 as the first vlue and a parametric constructor that starts with a specified non-negative integer as the first value. You must name your class C2_3Progression .

2. please use code tags as it is difficult to read for long codes.
lets start w/ the first one. what don't you understand about it? and what have you done so far?

3. Member
Join Date
Jan 2008
Posts
36
Rep Power
0
so far i have this and i think this is what i need but where im having trouble is having it so that each value in the progression is the absolute value of the difference between the previous two values. You should include a default constructor that starts with 2 and 200 as the first two values and a parameteric constructor that starts with a specified pair of numbers as the first two values. You must name your class C2_2Progression
Java Code:
```/**
* Fibonacci progression.
*/
class FibonacciProgression extends Progression {
/** Previous value. */
long prev;
// Inherits variables first and cur.

/** Default constructor setting 0 and 1 as the first two values. */
FibonacciProgression() {
this(0, 1);
}
/** Parametric constructor providing the first and second values.
*
* @param value1 first value.
* @param value2 second value.
*/
FibonacciProgression(long value1, long value2) {
first = value1;
prev = value2 - value1; // fictitious value preceding the first
}

/** Advances the progression by adding the previous value to the current value.
*
* @return next value of the progression
*/
protected long nextValue() {
long temp = prev;
prev = cur;
cur += temp;
return cur;
}
// Inherits methods firstValue() and printProgression(int).
}```

4. 1) class name should be C2_2Progression, save that for later.
2) this(0, 1); should be this(2, 200);
3) absolute value: take a look at the Math.abs() for more ideas.

5. Member
Join Date
Jan 2008
Posts
36
Rep Power
0
it compiles without errors, but when i run it it says errror in thread main...how do i fix that
Java Code:
```/**
* Fibonacci progression.
*/
class C2_2Progression extends Progression {
/** Previous value. */
long prev;
// Inherits variables first and cur.

/** Default constructor setting 0 and 1 as the first two values. */
C2_2Progression() {
this(2, 200);
}
/** Parametric constructor providing the first and second values.
*
* @param value1 first value.
* @param value2 second value.
*/
C2_2Progression(long value1, long value2) {
first = value1;
prev = value2 - value1; // fictitious value preceding the first
}

/** Advances the progression by adding the previous value to the current value.
*
* @return next value of the progression
*/
protected long nextValue() {
long temp = prev;
prev = cur;
cur += temp;
return cur;
}
// Inherits methods firstValue() and printProgression(int).
}```

6. can't help if you don't include main.
also you need to override nextValue() to produce:
"the absolute value of the difference between the previous two values."

7. Member
Join Date
Jan 2008
Posts
36
Rep Power
0
by main do you mean the progression.java...its in the first post....im not so good at java so i dont really know what to do at all im completely stuck

8. oh i see, you forgot to write a main method. you can't run it w/o a main().

the output you want is: 2,200,198,2,196,194,2,192..... i think.
worked that out on paper. no idea how to do that in the codes. you'll just have to mess around with it.

#### Posting Permissions

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