This special method in a class has the same name as the class and initializes objects.
Constructor
You make your instance variables with this access ____ and MOST of your methods with this access _____
What does a constructor do?
Initializes objects using either the this keyword or setting the instance variable equal to a parameter with matching data types or even setting specific values, so when the object is created, a beginning state is created.
Explain what a getter method does and what a setter method does
A getter method allows you to access instance variables values outside of the class, and a setter method lets you change/manipulate instance variable values outside of a class.
What is the main ingredient in hummus?
Chickpeas (garbanzo beans)
This keyword is used to create a new object from a class.
new
Making variables private and controlling access through methods demonstrates this OOP principle.
Encapsulation
Having multiple constructors with different parameter lists is called this.
What is constructor overloading?
What is the output?
2
5
What is acrophobia a fear of?
Heights
Why is using multiple classes in a large program beneficial?
It improves modularity, organization and makes maintenance/debugging easier. Answers may vary somewhat.
The Device class stores a device’s brand and model. Provide the code for two instance variables and constructor + initialize the instance variables in the constructor and display proper encapsulation principles.
private String brand;
private String model;
public Device(String b, String m)
{
brand = b;
model = m;
}
Provide an example of an object being created based on this constructor and class.
public class Animal
{
private String type;
private int age;
public Animal(String type, int age)
{
this.type = type;
this.age = age;
}
}
Animal a = new Animal("dog", 5);
Why won’t this work correctly?
private double total;
private void calc()
{
double total = 5;
}
public double get()
{
calc();
return total;
}
A. total must be public
B. calc must return double
C. Local variable hides instance variable
D. Method must be static
C. Local variable hides instance variable. Local variable with same name takes precedence over instance variables, which are global.
What do you call a word that reads the same way backward and forward?
Palindrome
public class Test {
private int val;
public Test(int v) {
val = v;
}
public Test() {
val = 10;
}
public int getVal() {
return val;
}
}
Test t = new Test(3);
System.out.println(t.getVal());
What does this print?
What is 3?
What does it mean if an instance variable is static? What kind of variable does it become?
It means it's a "Class variable" and is shared by all objects of a class. The primary difference is that static variables belong to the class and are shared by all instances, while instance variables belong to a specific object instance, with each object having its own unique copy.
Name the default values for the following data types:
int, double, boolean
int - 0
double - 0.0
boolean - false
What prints?
int x = 10;
public static void change(int a)
{
a = 50;
}
change(x);
System.out.println(x);
10
Java is pass-by-value.
When change(x) runs:
a gets copy of 10
Changing a does NOT affect x
So x remains 10.
In which country would you find Mount Kilimanjaro?
Tanzania
public class Book {
private int pages;
public Book(int p) {
pages = p;
}
public void addPages(int p) {
pages += p;
}
public int getPages() {
return pages;
}
}
public class Main {
public static void main(String[] args) {
Book a = new Book(100);
Book b = a;
b.addPages(50);
a.addPages(25);
System.out.println(a.getPages() + " " + b.getPages());
}
}
175 175
What is printed?
public class Count
{
private static int total = 0;
public Count()
{
total++;
}
public static void show()
{
System.out.println(total);
}
}
Count a = new Count();
Count b = new Count();
Count c = new Count();
Count.show();
3, with each object creation, the shared static class variable called "total" increases by 1 each time an object is created.
Why does this print null?
public class Phone
{
private String brand;
public Phone(String brand)
{
brand = brand;
}
public String getBrand()
{
return brand;
}
}
brand = brand is the problem with the code. It should be re-written as this.brand = brand. The parameter brand shadows the instance variable.
It assigns the parameter to itself instead of instance variables.
Without this, the instance variable stays null and prints null.
public class Car
{
private String model;
public Car(String m) {
model = m;
}
public String getModel() {
return model;
}
}
Car c1 = new Car("Tesla");
Car c2 = new Car("Tesla");
PRINT(c1.getModel().equals(c2.getModel))));
What is printed?(couldn't fit System.out.println, but assume it is there for the word PRINT)
True
What do the olympic rings stand for?
Unity, each represents one of the 5 continents.