method Flashcards
java predefined method for finding the absolute value
Math.abs( any number )
java predefined method for finding the bigger (max) value
Math.max(big num, small num)
java predefined method for finding a num to the power of another num
Math.pow
java predefined method for finding the smaller (min) max
Math.min
java predefined method for rounding a num
long x = Math.round
java predefined method for finding the square root
Math.sqrt
in Math.ceil, you round to the
right
ex:
double num = Math.ceil (2.4); ///3.0
double num = Math.ceil(-2.4); //-2.0
double num = Math.ceil(2.0); //2.0
in Math.floor, you round to the
left
ex:
double num = Math.ceil (2.4); ///2.0
double num = Math.ceil(-2.4); //-3.0
double num = Math.ceil(2.0); //2.0
java predefined method for giving you random numbers within a certain range
Math.random();
ex:
print number between 1000 and 1113 inclusive
System.out.println(int) ( Math.random() * 114 + 1000 );
[we take the difference between the two numbers and add 1 to it then multiply it to Math.random(), the add the smaller value between the two]
what can you write at the beginning of a code that uses a lot of Math. predefined methods?
import static java.lang.Math.*;
T/F: in a public class, there can exist public static void main only.
F, we can also have a method public static void.
T/F: in a public class, there can exist public static void main only.
F, we can also have a method public static void.
T/F: You can call out a method more than once in main.
T,
public class testM2
3 {
4 public static void main(String args[])
5 {
6 System.out.println(“welcom”) ;
7
8 mona() ;
9
10 System.out.println(“end my program”) ;
11 mona() ;
12 }
13 public static void mona()
14 {
15 System.out.println(“inside mona “) ;
16 }
T/F: You can name a method anything even if it defies the naming laws.
F, you can name it anything so long as it abides by the naming laws
the variables in a method header are called _________
formal parameter
the variables that are called out in main not metho are the ________
actual parameter
T/F: the variable intialized in main must be the same in the method.
F, in main the variable could be called out as (a , b) and in method they would be (x, y)
is the following correct:
public static void sum (int x, y)
incorrect, we have to identify the variables each time since this area isn’t an intializing space…
public static void sum (int x, int y)
T/F: the method must be intialized inside of the main to be called out.
true
a reserved word in method that destroys everything before it and returns only the thing you next to it to main.
return
ex:
public static int sum (int x, int y){
int s = x + y
return s ;
}
correct the following:
public static void sum (int x, int y){
int s = x + y
return s ;
}
it must be int not void since the value you are going to return is int.
public static int sum (int x, int y){
int s = x + y
return s ;
}
correct the following:
import java.util.*;
public class testMethod{
scanner console = new Scanner(System.in);
public static void main (String[] args){
if the scanner is written before/outside the main, we must add “static” to it.
import java.util.*;
public class testMethod{
static scanner console = new Scanner(System.in);
public static void main (String[] args){
correct the following:
public static void main (String[] args){
System.out.println( squar(10) );
System.out.println( cube(10) );
}
public static int square(int x){
return x*x;
}
public static void cube(int x){
System.out.println( Math.pow(x, 3));
}
incorrection: calling out the method in print.
public static void main (String[] args){
System.out.println( squar(10) );
cube(10);
}
public static int square(int x){
return x*x;
}
public static void cube(int x){
System.out.println( Math.pow(x, 3));
}
correct the following:
public static int methodsx(int x){
System.out.println(“**”);
return x + 5;
System.out.println(“end.”);
}
incorrection: adding anything after return is useless and a “unreachable statement” message would appear for you as an error.
public static int methodsx(int x){
System.out.println(“**”);
return x + 5;
}