Exercise 3-6 (storage - double string length)

Chapter_3     Exercises_3-4,5 Exercise_3-7







Exercise 3-6     TIJ, p. 61


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




Storage.java     TIJ, p. 49


public class Storage
{
static int storage (String s)
{
return s.length() * 2;
}

public static void main(String[] args)
{
String s = "Double the length of this text is: "; // 35 chars long
// access static field in a static context, main():
System.out.println(s + storage(s));
System.out.println(s + Storage.storage(s));
System.out.println(s + new Storage().storage(s));
}
}
/*
javac Storage.java
java Storage
Double the length of this text is: 70
Double the length of this text is: 70
Double the length of this text is: 70
*/




Note:  s.length() in storage() calls the method length() of class String.









Chapter_3     Exercises_3-4,5 BACK_TO_TOP Exercise_3-7



Comments

Popular posts from this blog

Contents