What are the methods that allow another class to view and change instance methods of a class
Accessor and Mutator Methods
How do you get the size of an array?
Use the .length function
What are the 3 loops that we have talked about so far?
While, for, enhanced for
Initialize a Painter object named bob that has an x position of 3, a y position of 5, is facing East and has 6 units of paint.
Painter bob = new Painter(3,5, "East", 6)
What is a result of the print statement?
int x = 10;
double y = 15.0;
System.out.println(x+y);
25.0
What is the role of a constructor?
When creating the objects, a constructor will set the instance variables for that object
Create an array of type string that holds the values
Have, a, Merry, Christmas
String[] something = {"Have", "a", "Merry","Christmas"}
What are the 3 parts to a for loop? Write and label them.
}
Initialization, condition, change
Write the toString method for a class called Dog that uses the instance variables name and breed
ex. "Dog name: Air Bud, Breed: Lab"
public String toString() {
return "Dog name: " + name + ", Breed: " + breed;
}
what are the values of arr after the following code is completed?
int[] arr = {1, 5, 8, 4};
arr[0] *= 2;
arr[1] = arr[0] + arr[3]
[2, 6, 8, 4]
What method is used to print an object? What is the act of adding this method to your class called?
toString Method
Overriding
How do you access an element at specified position? What is it called when you try to access an element at a position that doesn't exist in a list?
Using the index, array[index]
Out of bounds exception error
What are two reasons for using an enhanced for loop instead of a for loop or while loops when working with an array?
Readability and ease
Don't want to modify the array
Traversing through the entirety of an array
Write a parameterized constructor for the class Mystery that has instance variables x and y that are of type int.
this.x = x;
this.y = y;
}
int x = 2; int y = 0
while (x != 0) {
x*= 4; y++;
x%3 -1;
}
System.out.print(y);
2
What is a keyword used to set the visibility of classes, variables, constructors, and methods?
What is the name of the concept of hiding our instance variables from other classes using it?
Encapsulation
Explain what to do if you want to change the size of an array
False. If you wanted change the size of an array, then you would need to create a new one and populate it with the values you want.
What is the error in the following code?
int[] array = {1, 3, 5, 8};
for (item : array) {
array[item]++;
}
This will hit an out of bounds exception
Create two objects of the Painter class and save them in an array of Painter objects
Painter[] stuff = {new Painter(), new Painter()};
Painter bob = new Painter();
Painter lisa = new Painter();
Painter [] stuff = {bob, lisa};
What is printed out?
String[] arr = {"Hey", "Hello", "Goodbye"}
for(int i = 0; i < arr.length; i++) {
for(int j = i+1; j<arr.length; j++) {
System.out.println(arr[j - i]);
}}
Hello
Goodbye
Goodbye
public class Food {}
public class ItalianMenu extends Food {}
public class Pasta extends ItalianMenu
Use the idea of polymorphism to create an object of one class and store it in a different class
Food item = new ItalianMenu();
ItalianMenu item = new Pasta();
Food item = new Pasta();
What is the default value for Strings, ints, doubles and boolean when an an array is created but not given a value?
Null, 0, 0.0, false
Turn the while loop into a for loop
int count = 0;
while (count < 5) {
System.out.println("Hello");
count++;
}
for (int count = 0; count < 5; count++) {
System.out.println("Hello);
}
Write a method in a Class called minimum that takes in an array of ints and returns the lowest value
public int minimum(int[] arr) {
int temp = arr[0];
for(num : arr) {
if(num < temp) {
temp = num;
}
)
)
What is printed out?
int[] nums = {2, 5, 7}
for (num : nums) {
System.out.println(num / 2 + " " + num%2);
}
1 0
2 1
3 1