# help me solve excercise from book

• 03-12-2014, 05:30 PM
andreya
help me solve excercise from book
hi
I'm in my journey to learning java
I do self study with book
this is the book that i'm study
The Java Programming Language, 4th Edition: Ken Arnold, James Gosling, David Holmes: 9780321349804: Amazon.com: Books

and i have trouble in my excercises
here is the excercises
Quote:

Originally Posted by EXCERCISES 1.10
Modify the ImprovedFibonacci application to store its sequence in an array. Do this by
creating a new class to hold both the value and a boolean value that says whether the value is even, and
then having an array of object references to objects of that class.

I'm quite confuses with the language (sorry my first language is not english)
and almost didn't know what i have to do to answer this questions
did i just need to declaring the variable in other class (for boolean value and the value itself) or else ?

here is the code for ImprovedFibonacci.java
Code:

```class ImprovedFibonacci {         static final int MAX_INDEX = 9;         /**         * Print out the first few Fibonacci numbers,         * marking evens with a '*'         */         public static void main(String[] args) {                 int lo = 1;                 int hi = 1;                 String mark;                 System.out.println("1: " + lo);                 for (int i = 2; i <= MAX_INDEX; i++) {                         if (hi % 2 == 0)                                 mark = " *";                         else                                 mark = "";                         System.out.println(i + ": " + hi + mark);                         hi = lo + hi;                         lo = hi - lo;                 }         } }```
thanks before
and thanks for helping
if my english is wrong and bad
feel free to tell me which is wrong and tell me how to fix it :)
• 03-12-2014, 05:38 PM
kneitzel
Re: help me solve excercise from book
Hi,

my understanding is:
A new class (e.g. "FibonacciNumber) with 2 fields: the value itself and the information if it is even or not.
And the array will hold objects of that class.

So inside your main function you could
- define the array of FibonacciNumber
- Inside the for loop, you can store the data into that array.

Did this help?

With kind regards,

• 03-12-2014, 05:59 PM
andreya
Re: help me solve excercise from book
Quote:

Originally Posted by kneitzel
Hi,

my understanding is:
A new class (e.g. "FibonacciNumber) with 2 fields: the value itself and the information if it is even or not.
And the array will hold objects of that class.

So inside your main function you could
- define the array of FibonacciNumber
- Inside the for loop, you can store the data into that array.

Did this help?

With kind regards,

yeah i'm just thinking about that
so i have a class that declaring the variable and status
is this right?
considering my main class is FiboClass{} and Value{} is my sub-class
Code:

```class Value{         public int value; //the fibonacci number that have to store to array         public boolean type; //the status if the fibonacci number even or odd }```
so that in my main class i must do initialize Value objects? as much as i need?
Value array1 = new Value();
value array2 = new Value();
and so on
• 03-13-2014, 05:20 AM
andreya
Re: help me solve excercise from book

this is my code
for main class
Code:

```class ImprovedFibonacci {         static final int MAX_INDEX = 9;         /**         * Print out the first few Fibonacci numbers,         * marking evens with a '*'         */         public static void main(String[] args) {         //deklarasi bahan bahan                        int[] kotak;                 kotak = new int [10];                 Value nilai = new Value(); // instanisasi kelas                 int lo = 1;                 int hi = 1;                 nilai.nilai = lo; //starting print                 nilai.cetak();                 for (int i = 2; i <= MAX_INDEX; i++) {                         nilai.nilai = hi;                         nilai.cetak();                         hi = lo + hi;                         lo = hi - lo;                 }         } }```
code for subclass
Code:

```class Value {         public int nilai = 0 ;         public String mark;         public void clear() {                 nilai = 0;         }         //print the value and clear it         public void cetak() {                 if (nilai % 2 == 0)                         mark = " *";                 else                         mark = "";                 System.out.println(nilai + mark);                 clear();         } }```
its no need to initialize for each array
just use same initialize and use clear()
now i just need to store it to array then my excercise is completed
thanks guys
• 03-13-2014, 02:20 PM
kneitzel
Re: help me solve excercise from book
Hi,

I am a little wondering about your code. You defined an array of integers (kotak) but you do not really use it. And inside the loop you are using the same instance of your Value class.

And then the naming seems a little strange. "Value" can be everything. Most things are values. Your name is also a value. So I would try to find better names.

And when you switch numbers around, the operation used is hard to read:
hi = lo + hi;
lo = hi - lo;
I would prefer something like
int temp = hi+lo;
lo = hi;
hi = temp;

But if you keep all numbers of the series, then you could remove these variables completly. I think it is quite hard to understand, so I simply show you some code that I wrote now:

Code:

```public class FibonacciNumber {     private boolean even;     private int number;     public FibonacciNumber(int number) {         this.number = number;         even = number % 2 == 0; // We calculate the even field in here.     }     public boolean getEven() {         return even;     }     public void setEven(boolean even) {         this.even = even;     }     public int getNumber() {         return number;     }     public void setNumber(int number) {         this.number = number;     }     @Override     public String toString() {         if (even)             return "" + number + " *";         else             return "" + number + " ";     } }```
Code:

```public class FibonacciApp {     static final int FIBONACCI_NUMBER_ARRAY_SIZE = 10;     public static void main (String[] args) {         FibonacciNumber[] numbers = new FibonacciNumber[FIBONACCI_NUMBER_ARRAY_SIZE];         numbers[0] = new FibonacciNumber(1);         numbers[1] = new FibonacciNumber(1);         for (int i = 2; i < FIBONACCI_NUMBER_ARRAY_SIZE; i++)             numbers[i] = new FibonacciNumber(numbers[i-1].getNumber() + numbers[i-2].getNumber());         // Now we got all fibonacci Numbers. We could Display the series now:         for (int i = 0; i < FIBONACCI_NUMBER_ARRAY_SIZE; i++) {             System.out.println(numbers[i]);         }     } }```
I hope teh code shows clearly what I meant with my comments above.

With kind regards,