JAVA
6. Member variables can act as "global state" for an entire class, which can often lead to very confusing and hard to maintain code (often called "spaghetti code" since the pieces are so intertwined with each other). One easy way to avoid using global state in your programs is to use static member variables primarily to store values that don't change. These are often referred to as constants and should be declared with the final keyword to indicate to the compiler that the value cannot be changed after it is initialized. Write the declaration and initialization you might use for the following constant values. These should all be private, static members that are final. Pick the appropriate data type and good name for storing each constant. Example: The mathematical constant 'e', also known as Euler's number, is approximated by 2.7182818284 Answer: private static final double E = 2.7182818284 o The number of days in a week o The number of centimeters in one inch • A tax rate of 23% (There's no data type for a "percent" but you can store it as a decimal) • The mathematical constant 'phi, also known as the "golden ratio", equal to 1+√5. Recall that in Java Math.sqrt is the "square root" function and that any expression can be used in place of a simple value in Java.
JAVA
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am