03 Prelim CP2 Flashcards
- It is a special method that is used to CREATE and INITIALIZE an object.
- Using the “new” keyword calls a ______.
- It must have the same name with the class to which it belongs to.
- It does not have a return type.
CONSTRUCTOR
- These are shared by all the objects of its class.
- ______ _____ that are not constants should be private.
- These should be accessed or changed only via accessor and mutator methods
Sample:
private static double numOfAccounts;
STATIC VARIABLES
- can be invoked without using any object
-invoked by using the class name instead of the object name
Sample:
public static int getNumofAccounts() {
return numOfAccounts;
}
STATIC METHODS
How to invoke a static method:
variableName = ClassName.staticMethod(arg);
Example:
inches = UnitConverter.convertFeetToInches(2.6);
feet = UnitConverter.convertInchesToFeet(53.7);
When you declare a variable or a method as _____, it belongs to the class rather than to a specific instance.
This means that only one instance of a static member exists, even if you create multiple objects of the class or if you do not create any. It will be shared by all objects.
STATIC
- Provides a number of standard mathematical
methods. - Does not require an import statement.
- All the methods in this class are static.
- it returns the absolute value of its parameter.
-You do not need to create an object of the Math class to use it. To access it, just type in Math. and the corresponding method.
Sample:
int higherNum = Math.max(7, 9);
MATH CLASS
What method in Math class returns the absolute value of its parameter
Math.abs()
Sample:
int a = Math.abs(-10);
// output “10”
What method in Math class returns the smallest parameter.
Math.min()
Sample:
int m = Math.min(10, 20);
// output “10”
What method in Math class takes two (2) parameters and returns the first parameter raised to the power of the second parameter.
Math.pow()
Sample:
double p = Math.pow(2, 3);
// output “8.0” (2^3 = 8)
What method in Math class returns a random number in the range >0 and <1
Math.random();
What method in Math class returns the largest parameter.
Math.max()
Sample:
int m = Math.max(10, 20);
// output “20”
What method in Math class returns the round number of the given parameter:
Math.round();
Sample:
Math.round(6.2); // 6
Math.round(6.8); // 7
What method in Math class returns the highest closest number of the given parameter:
Math.ceil();
Sample:
Math.ceil(3.2); // 4.0
Math.ceil(3.8); // 4.0
What method in Math class returns the lowest closest number of the given parameter:
Math.floor();
Sample:
Math.floor(3.2); // 3.0
Math.floor(3.8); // 3.0
What method in Math class returns the square root of the given parameter:
Math.sqrt();
Sample:
Math.sqrt(4.0); // 2.0
- _______the method name is when you give two (2) or more methods the same name within the same class.
- Methods
with the same name must have a different number of
parameters or have corresponding parameters with differing
types.
OVERLOADING
METHODS with the SAME NAME must have a DIFFERENT number of
PARAMETERS or have corresponding parameters with DIFFERING
types.
-True
-False
TRUE