Void
When this keyword is placed in a method's signature, it can only use variables and other methods that use this modifier.
What is "Static"?
If a method is static, it can ONLY use other static methods or static variables in its code.
Determine output. If an error, explain why:
String thing = "apple";
int a = 1;
int b = 4;
System.out.print( thing.substring(a, b) );
Output is "ppl"
A variable that defined in a class, for which each object of the class has a separate copy.
What is an instance variable?
What famous cartoon character (specifically) entered public domain in 2024?
Steamboat Willie (Mickey Mouse)
Given the Pizza class, write a setter method for topping.
public class Pizza{
private String topping;
//constructor not shown
//insert setter here
}
public void setTopping( String newTopping) {
topping = newTopping;
}
OR
public void setTopping( String topping) {
this.topping = topping;
}
This modifier allows an instance variable to be changed directly outside the class without the need for a Mutator / Setter method.
What is "public"?
Determine output. If an error, explain why:
String thing = "apple";
int a = 2;
int b = 2;
System.out.print( thing.substring(a, b) );
No output.
A keyword that makes a variable or method only visible and accessible within its own class.
What is "private"?
One breaks but never falls. It's opposite falls but never breaks. What are we?
Day and Night
Given the Pizza class, write an accessor method for topping.
public class Pizza{
private String topping;
//constructor not shown
//insert accessor here
}
public String getTopping(){
return topping; //or this.topping
}
Which of these is a valid way to create a new object of the "Sport" class that has one written constructor that takes in a single string?
Sport s1 = Sport("Baseball");
Sport s4;
s4 = new Sport();
Sport s4;
s4 = new Sport();
Determine output. If an error, explain why:
String thing = "apple";
int a = 5;
int b = a+1;
System.out.print( thing.substring(a, b) );
An error is thrown; numbers taken in from substring must be between 0 and the length of the word.
A modifier that designates that a method or variable can be accessed without creating an object of a class.
What is "static"?
Someone throws a ball as hard as they can. It comes back to her, even though nothing and nobody touches it. How?
They threw the ball straight up
What happens if two constructors exist with the same signature (number, type, and order of variables)?
The code will not compile
The code will compile, but will cause a run time error if an object of that type is created
The code will compile and will use the first constructor that matches the object
The code will compile and use the default constructor rather than either written constructors.
1. The code will not compile
What is the value of numDucks after running Duck.addNephews();?
public class Duck{
private static int numDucks = 1;
public Duck(){
numDucks = numDucks + 1;
}
public static void addNephews() {
Duck Huey = new Duck();
Duck Dewey = new Duck();
Duck Louie = new Duck();
}
}
numDucks = 4
Determine output. If an error, explain why:
String thing = "apple";
int a = 1;
int b = 10;
System.out.print( thing.substring(a, b) );
An error is thrown; numbers taken in from substring must be between 0 and the length of the word.
Something that must be true before your method will work
What is a precondition?
What won the oscar for best picture in 2023?
Everything Everywhere All at Once
Identify the 4 different mistakes below (some errors, some conventions that we have learned):
private class dragon
{
public String name
public int power
//constructor and methods not shown
}
1. Class should be public
2. Class name should be capitalized (Dragon)
3. Instance variables should be private
4. Semicolons missing after instance variables
What is wrong with this class definition?
public class Superhero {
private String name;
private int strength;
public Superhero (String name, int strength){
name = name;
strength = strength;
}
}
Must use "this" in the constructor when the parameters have the same name as instance variables.
Determine output. If an error, explain why:
String thing = "apple";
int a = 4;
int b = 1;
System.out.print( thing.substring(a, b) );
An error is thrown; substring will only work if the first number is lower than the second number.
A way of referring to the current object in a method or constructor, often used to eliminate confusion between class variables and parameters with the same name.
What is the "this" keyword?
Which of the following is one of the 7 ACM Code of Ethics and Professional Conduct's General Ethical Principles?
Be fair
Credit original creator when using other's work
Be fair and take action not to discriminate
Be fair and take action not to discriminate