Thinking in Java Fourth Edition Bruce Eckel Chapter 1 Exercise 4

Exercise 4: Turn the DataOnly code fragments into a program that compiles and runs.

Solution:

public class DataOnlyTest
{  
    public static void main(String[] args)
    {
        class DataOnly
        {
            int i;
            double d;
            boolean b;
            void show()
            {
                System.out.println(i);
                System.out.println(d);
                System.out.println(b); 
            }
        }  
        DataOnly data = new DataOnly();
        data.i = 3;
        data.d = 2.71828;
        data.b = false;    
        data.show();
    }
}

Output:

 

Leave a Reply

Your email address will not be published. Required fields are marked *