What is the method name for returning the length of an Array?
arrayName.length;
This loop is known for its use of the i incrementor
For Loop
What type of parameter passing does Java use?
Pass by value
Which access modifer makes a attribute/method only visible/useable inside a given class?
Private
How do you create a Random object AND a Scanner object? Include both import and the object creation.
import Java.util.Scanner;
import Java.util.Random;
Scanner scan = new Scanner(System.in);
Random rand = new Random();
What index value do arrays start at in Java?
0
This statement can be used to end a loop immediately
Break
If an object is made up of first name, last name, age, position, and salary, what datatypes would these variables be?
String, String, int, String, int
What keyword do you use when accessing a variable inside of a class, especially when figuring out if you're using the class variable name or the parameter name?
this
This scanner method is used to geta full String-based input.
scanner.nextLine();
Give an example of an int array populated with three values on the whiteboard using only one line.
int[] numbers = {1, 2, 3}
This loop will always run once regardless if the condition is true or false.
Do While Loop
What datatype does this method return??
final int MULTI = 2;
public ___ multiple(int num){
___multiple = num * MULTI;
return multiple;
}
integar
This is what is called when an object is first created
The Constructor
What number range does random.nexInt(2, 27) return?
2 - 26
What is the name of the error when trying to access an array element that does not exist?
ArrayIndexOutOfBoundsException
Write a while loop that constantly asks the user for String input non-stop until the user enters CEASE AND DESIST IMMEDIATELY! Include your class name and public static void main
import java.util.Scanner;
public class WhileLoopDemo {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Declare a String variable to store the user input
String input = "";
// Loop until the user enters CEASE AND DESIST IMMEDIATELY!
while (!input.equals("CEASE AND DESIST IMMEDIATELY!")) {
// Prompt the user to enter a String
System.out.print("Enter a String: ");
// Read the user input and store it in the variable
input = scanner.nextLine();
// Print the user input
System.out.println("You entered: " + input);
}
// Close the Scanner object
scanner.close();
}
}
What is the output?
public class Main{
public static void main(String[] args){
int num = 5;
int[] numberArray = {2, 4, 6, 8, 10};
changeValues(num, numberArray);
System.out.println(num);
for(int i = 0; i < numberArray.length; i++)
System.out.println(numberArray[i]);
}
public static void changeValues(int num, int[] numArray){
num = 15;
numArray[2] = 1000;
}
}
5
2
4
1000
8
10
These are the methods for returning or modifying existing attributes.
Getters and Setters
What number range does random.nextDouble() * 39 give?
0.0 - 38.99...
Write a static method that returns the average of ANY given double array.
public static double average(Double[] numArray){
double sum = 0.0;
for(int i = 0; i < numArray.length; i++)
sum += numArray[i];
return sum / numArray.length;
}
Write a for loop that will print out the multiplications table, from 0 - 9, remember to use System.out.print() and System.out.println()! Include your class name and public static void main.
public class Main {
public static void main(String[] args) {
for(int i = 0; i < 10; i++){
System.out.print(i + ": ");
for(int j = 0; j < 10; j++){
System.out.print(j * i + " ");
}
System.out.println();
}
}
Write a method that is called calculateWage() that takes in salary and hours, and returns a double datatype called wage.
public double calculateWage(double salary, double hours){
double wage = salary * hours;
return wage;
}
(int or salary or hours will be accepted)
Write a public class method that will return true if two Wealth objects have the same value; assume this class has a public double attribute called money and a public int attribute called age.
public isEquals(Wealth other){
return(this.money == other.money && this.age == other.age);
What type of error is produced when a String is entered with scan.nextInt()?
InputMismatchException