Thinking in Java Fourth Edition Bruce Eckel Chapter 1 Exercise 5

Exercise 5: Modify the previous exercise so that the values of the data in DataOnly are assigned to and printed in main( ).

Solution:

public class DOTest2
{      
    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 = 234;
        data.d = 2.1234545;
        data.b = true;     
        data.show();
    }
}

Output:

Leave a Reply

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