APCSA Chapter 2.8-2.9

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Static methods

(also called class methods) are called using the class name and the dot operator (.) followed by the method name. You do not need to create an object of the class to use them. You can use ClassName.methodName() or just methodName() if they are called from within the same class.

the range is the

(max number - min number + 1)

The Java compiler applies unboxing when a wrapper class object is

-Passed as a parameter to a method that expects a value of the corresponding primitive type. -Assigned to a variable of the corresponding primitive type.

The java compiler applies autoboxing when a primitive value is

-Passed as a parameter to a method that expects an object of the corresponding wrapper class. -Assigned to a variable of the corresponding wrapper class.

int rnd2 = (int)(Math.random()*10) + 1;

// rnd2 is in the range 1-10 (including 10). The parentheses are necessary!

int rnd3 = (int)(Math.random()*6) + 5;

// rnd3 is in the range 5-10 (including 10). The range is 10-5+1 = 6.

int rnd4 = (int)(Math.random()*20) - 10;

// rnd4 is in the range -10 up to 9 (including 9). The range is doubled (9 - -10 + 1 = 20) and the minimum is -10.

Autoboxing

Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double. The Java compiler applies autoboxing when a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class or assigned to a variable of the corresponding wrapper class. Here's an example of autoboxing.

2-9-5: Which of the following would return a random number from 0 to 10 inclusive?

B. ((int) (Math.random() * 11))

2-9-4: Which of the following would return a random number from 1 to 5 inclusive?

C. ((int) (Math.random() * 5) + 1)Check MeCompare me

Double(double value)

Constructs a new Double object that represents the specified double value.

2-9-7: Which of the following statements assigns a random integer between 25 and 60, inclusive, to rn?

D. int rn = (int) (Math.random() * 36) + 25;

wrapper class for double is called

Double

Wrapper class one word definition

Integer

wrapper class for int is called

Integer

Overflow ex/formula

Integer.MAX_VALUE + 1

underflow definiton/formula

Integer.MIN_VALUE - 1

Underflow

Java will actually return the maximum integer value if you try to subtract one from the minimum value. This is called underflow.

Overflow

Java will return the minimum integer value if you try to add one to the maximum. This is called overflow.

Math.pow(number, exponent)

Math.pow(number,exponent). For example, if you had 2 dials on your combination lock where each dial can be set to a digit from 0-9 (10 digits), there are 102 possible permutations. In Java, this would be written as Math.pow(10,2) which means 10 to the power of 2. If you start listing all the permutations possible, you can tell that there are 102 or 100 possible permutations for a 2 dial lock from 0-9.

double rnd = Math.random()

Math.random() returns a random number between 0.0-0.99

double.random()

Returns a double value greater than or equal to 0.0 and less than 1.0 (not including 1.0!).

double abs(double)

Returns the absolute value of a double value.

int abs(int)

Returns the absolute value of an int value (which is the value of a number without its sign, for example Math.abs(-4) = 4).

double sqrt(double)

Returns the positive square root of a double value.

double pow(double, double)

Returns the value of the first parameter raised to the power of the second parameter.

double doubleValue()

Returns the value of this Double as a double.

Whater are the Integer class and Double class considered

The Integer class and Double class are wrapper classes that create objects from primitive types.

static methods

These methods are in the Math class defined in the java.lang package. These are static methods which means you can call them by just using ClassName.methodName() without creating an object. This is why we can just say Math.random() instead of having to define an object of the class Math.

When would you ever use Integer.MIN_VALUE or Integer.MAX_VALUE?

They are handy if you want to initialize a variable to the smallest possible value and then search a sequence of values for a larger value.

Deprecated

To wrap a value, call the constructor for the wrapper class in earlier versions of Java. In Java 9 on, this is deprecated which means it's not the best way to do this anymore, and you should instead just set it equal to a value. The AP CS A Exam covers Java 7 which does allow using the constructor.

Unboxing

Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double. The Java compiler applies unboxing when a wrapper class object is passed as a parameter to a method that expects a value of the corresponding primitive type or assigned to a variable of the corresponding primitive type. Here's an example of unboxing:

how to wrap a value

call the constructor for the wrapper class in earier versions of Java

primitive type

int

primitive type definition/formula

int

Autoboxing definition in summary

is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double.

Unboxing definition in summary

is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double.

the Math.random() is a

method that returns a number greater than or equal to 0.0, ad less than 1.0

Math.random()

method to generate a random number.

(int)(Math.random()*range) + min example/definition in summary

moves the random number into a range starting from a minimum number. The range is the (max number - min number + 1). For example, to get a number in the range of 5 to 10, use the range 10-5+1 = 6 and the min number 5: (int)(Math.random()*6) + 5).

(int)(Math.random()*range) + min

moves the random number into a range starting from a minumum number

For every primitive type in Java, there is a built in

object type called a wrapper class

Math.random() returns a

random number between 0.0-0.99

int intValue()

returns the vale of this integer as an int

int rnd1 = (int)(Math.random()*10);

rnd1 is an integer in the range (0-9) including 9

Math.abs(int)

which calculates the absolute value of an int argument (which is the value of a number without its sign, for example Math.abs(-4) = 4).

Sometimes you may need to create a wrapped object for a primitive type so that __

you can give it a method that is expecting an object


Set pelajaran terkait

Supply Chain Design & Management Exams

View Set

Chapter 52: Care of the Patient with a Sensory Disorder

View Set

Week 1 Fundamentals Sherpa CH 1-Theoretical Base of Nursing & Nursing Practice

View Set

NURmental health- Substance-Related and Addictive Disorders.

View Set