chapter 3 -revisited Flashcards
what is a class?
blueprint of an object
defines the variables and methods an object contains.
what are the parts of the following line of code (class, etc): Scanner scan = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
__________________________________
Scanner – is the class and the blue print for objects.
scan – is an object being created/
new – is a java operator that is the creator of a new object.
Scanner – this is a constructor and part of the Scanner class.
( )— used after the Scanner as part of its constructor to set the parameters that must be used.
(System.in) — parameters used represents input from the keyboard.
if i wanted to create a second scanner object with the same functionality as the first then how would i and what is this called?
Scanner scan2;
^ is a deceleration in itself.
scan2 = scan;
^^ both are the same in functionality.
Aliases of one another: 2 ways to get to the same object.
“when multiple reference variables refer to the same object”.
if i wanted to create a second scanner object but have it scan from a different input such as a file then how would i do this?
scan2 = new Scanner(new File ("myData.txt")); here a new object called scan2 is created and it uses the constructor with parameters that scans in data from a specific file.
what is a constructor?
constructor:
a method that sets up a newly created object used to give an initial value to a variable.
how to create a random object for generation of random numbers? (random class)
Random generating = new Random(); generating is the object name that we set for random import java.util.random;
int num1 = generating.nextInt(300);
what will this do?
will return a random number in the range between 0 and 299.
what code is written to create a random number between 0 and 6?
int num2 = generating.nextINT(5) + 1;
what range will the following code give me?
int num3 = generating.nextInt(20) - 10;
int num3 = generating.nextInt(20) - 10;
will produce a range of: -10 and 9
**the (20) produces a range of 0 through 19 then we add the -10 for the final range.
are math class methods static or do we use objects to invoke methods of the math class?
math class methods are static. so what this means: int num4 = Math.cos(30); ^^ this object was not created by me b/c it is static with the math class and already made.
what is the math function to raise a number to a power?
int squared = Math.pow(a, 2);
^ object name ^ a represents the number while 2 represents the exponent (power being raised to)
The math function uses what type of input? integer, double, float, String?
Math function always uses a double so do not assign anything else with it otherwise you will have errors.