Results 1 to 6 of 6
  1. #1
    andreya is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default 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
    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 :)

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default 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,

    Konrad

  3. #3
    andreya is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Re: help me solve excercise from book

    Quote Originally Posted by kneitzel View Post
    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,

    Konrad
    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
    Java 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

  4. #4
    andreya is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Re: help me solve excercise from book

    finally i made it

    this is my code
    for main class
    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) {
    	//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
    Java 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

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default 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:

    Java 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 + " ";
        }
    }
    Java 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,

    Konrad

  6. #6
    andreya is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Re: help me solve excercise from book

    I'm so happy u help me learn and do this excercise, really appreciate it
    Thank you

Similar Threads

  1. Help with changing a Book Enum to a Book Class
    By Dragrain in forum New To Java
    Replies: 1
    Last Post: 12-06-2012, 07:18 PM
  2. Need A SMART person for this excercise (beginner)
    By khavanian in forum New To Java
    Replies: 11
    Last Post: 10-12-2012, 10:14 AM
  3. Do not know how to solve this
    By jaylimix in forum New To Java
    Replies: 13
    Last Post: 05-31-2011, 11:42 AM
  4. Replies: 17
    Last Post: 08-22-2010, 04:34 AM
  5. Please help me solve them
    By quangtrung89 in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 10:08 PM

Posting Permissions

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