Display 5.1

public class RoundStuff {
public static final double PI = 3.14159;
public static double area (double radius) {return PI*radius*radius) }
public static double volume (double radius) {return ((4.0/3.0)*PI*radius*radius*radius); }}

1. Is the following legal? The class RoundStuff is defined in Display 5.1

RoundStuff roundObject = new RoundStuff();
System.out.println("A circle of radius 5.5 has area " + roundObject.area(5.5));

2. In Display 5.1, we did not define any constructors for the class RoundStuff. Is this poor programming style?

3. Can a class contain both static and nonstatic (that is, regular) methods?

4. Can you invoke a nonstatic method within a static method?

5. Can you invoke a static method within a nonstatic method?

6. Can you reference an instance variable within a static method? Why or why not?

7. What is the difference between a static variable and an instance variable?

8. Can you use an instance variable (without an object name and dot) in the definition of a static method of the same class? Can you use an instance variable (without an object name and dot) in the definition of a nonstatic (ordinary) method of the same class?

 

9. Can you use a static variable in the definition of a static method of the same class? Can you use a static variable in the definition of a nonstatic method of the same class?

 

10. Can you use the this parameter in the definition of a static method?

 

 

11. Following the style guidelines given in the text, when should a static variable be marked private?

12. What do static methods and static variables have in common?

13. What values are returned by each of the following?

Math.round (3.2)
Math.round(3.6)
Math.floor(3.2)
Math.floor(3.6)
Math.ceil(3.2)
Math.ceil(3.6)

14. Suppose answer is a variable of type double. Write an assignment statement to assign Math.round(answer) to the int variable roundedAnswer.

 

15. Suppose n is of type int and m is of type long. What is the type of the value returned by Math.min(n,m)? Is it int or long?

 

Revised: November 3, 2008