Thinking in Java Fourth Edition Bruce Eckel Chapter 1 Exercise 6

Exercise 6: Write a program that includes and calls the storage( ) method defined as a code fragment in this chapter.

Solution:

public class StorageTest
{  
    public static void main(String[] args)
    {
        class StoreStuff
        {
            int storage(String s)
            {
                return s.length() * 2;
            }  
        }
        StoreStuff x = new StoreStuff();
        System.out.println(x.storage("hi"));       
    }
}

Output:


 

Leave a Reply

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