Skillstorm Intro to Coding - OOP & Java Fundamentals Flashcards
What are classes in Java?
They are blueprints used to define objects
What are a few examples of classes?
- Real-world objects
- Application components
What are the members of a Class in Java?
=> Variables (State)
=> Methods (Behavior)
=> Constructors (Initialization)
Given the following code, identify the class body, constructor, method body, and variable.
public class HelloVehicle { int speed; void accelerate() {}
HelloVehicle() {
speed = 0;
}
}
public class HelloVehicle { // class body int speed; // variable void accelerate() {} //method body
HelloVehicle() {
speed = 0;
} // constructor
}
Given a basic HelloWorld class, what’s the best way to write the signature of the main method in Java?
public class HelloWorld {
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World”);
}
}
Which is the correct signature of the main method?
- public static void main(String[] args)
- static public void main(String[] args)
Both 1. and 2. are correct!!!
Label the separate code blocks correctly with following labels: Class, Constructor, Method, and Variable
- ____________
public class HelloVehicle{
2._____________
int speed;
3._____________
void accelerate() {}
4._____________
HelloVehicle(){
speed = 0;
}
}
- Class
public class HelloVehicle {
- Variable
int speed;
- Method
void accelerate() {}
- Constructor
HelloVehicle(){
speed = 0;
}
}
Java is strongly-typed, in other words, you have to state what data type each variable is going to be? True or False
True
What are the four data types in Java and their sub-types?
- Numeric (whole)
- byte: -128 to 127
- short: -32,768 to 32,767
- int: -2^31 to 2^31
- long: -2^64 to 2^64
- Numeric (decimal)
- float: single precision
- double: double precision
- Text
- char: single character
- True/False
- boolean
What are non-primitive data types with an example?
Non-Primitive Data Types
- Any class you want
- Create your own types
For Example,
Vehicle Class as a variable to be used with an Engine Class
String
- Multiple characters together
You could string characters together to get a word such as HELLO.
In Java arrays, you store multiple values in a _____ _____ variable
single reference
Are Java arrays continuous?
Yes, they are a sequential block of memory
How do you access the elements of a Java array?
By their index
Though values can be different in each index of a Java array, can they also be of a different data type?
No. They have to be the same data type but can be different values
For Example:
If the data type is char, index 0 can have raj, index 1 can have hudek and so on, but index 3 could not have 42 because that is a numeric data type which is different than the original char data type we started out with in index 0 and index 1.
What do constructors initialize?
An object’s state
Calling a constructor by its new keyword does what?
It creates an object at runtime
Does Java provide a default constructor?
Yes, BUT only if you haven’t provided your own
Can you define many constructors and if so, what would that be called?
Yes, and it is known as constructor overloading
When a class contains a constructor, and you have more than one keyword, what happens when you call the second keyword?
The constructor is invoked to create a brand new object with its own state.
If you call the second reference variable (keyword) in a constructor, will the method from the class that acted on the first reference variable work here as well?
No
Why can’t you write two constructors that have the same number and type of arguments for the same class?
Because the compiler would not be able to tell them apart!
What happens if you don’t provide any constructors for your class?
The compiler automatically provides a no-argument, default constructor for any class without constructors.
How do you have variables that are common to all objects?
By using a static modifier
What does it mean that a static modifier is called a static field or class variable?
It means they are associated with the class, rather than any with any object.
If any object can change the value of a class variable, can class variables also be manipulated without creating an instance of the class?
Yes
How are class variables referenced?
By referencing the class name itself.
For example:
public class Bicycle {
private int cadence;
private int gear;
private int speed;
// add an instance variable for the object ID
private int id;
// add a class variable for the number of Bicycle objects instantiated
private static int numberOfBicycles = 0;
…
}
What can you use to access static fields?
static methods.
For example, we could add a static method to the Bicycle class to access the numberOfBicycles static field:
public static int getNumberOfBicycles() {
return numberOfBicycles;
}
What can instance methods access directly?
- instance variables
- instance methods
- class variables
- class methods
What can class methods access directly?
What can class methods not access directly and what must they use to access these things indirectly?
BONUS: Why can’t class methods use the “this” keyword?
Can access directly => Class variables and class variables.
Cannot access directly => Instance variables or instance methods
What must they use => Object reference
BONUS => Because there is no instance for “this” to refer to.
What are the only required elements of a method declaration?
- Return type
- Name
- A pair of parentheses ()
- A body between braces {}
More generally, what are the six components of method declarations in order?
- Modifiers—such as public, private, and others you will learn about later.
- The return type—the data type of the value returned by the method, or void if the method does not return a value.
- The method name—the rules for field names apply to method names as well, but the convention is a little different.
- The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
- An exception list—to be discussed later.
- The method body, enclosed between braces—the method’s code, including the declaration of local variables, goes here.
What two components of a method declaration comprise the method signature?
- The method’s name
- The parameter types
What is proper naming convention for methods?
By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc.
In multi-word names, the first letter of each of the second and following words should be capitalized.
For example:
run
runFast
getFinalData
Though it’s typical for a method to have a unique name within its class, when could they have the same name as other methods (with an example)?
Method overloading or methods with different method signatures. In other words methods within a class can have the same name if they have different parameter lists.
For example:
public class DataArtist {
…
public void draw(String s) {
…
}
public void draw(int i) {
…
}
public void draw(double f) {
…
} public void draw(int i, double f) {
…
}
}
Give examples of static methods that are created without instances of a class (without an object) using the Math class and the static modifier.
- Math.random();
- Math.pow(2, 31);
- Math.tan(90);
Where Math is the class and random, pow, and tan are the static modifiers.
Match the value to the correct variable declaration for Class Vehicle {}:
Values => 1. false, 2. ‘D’, 3. “Toyota”, 4. 123456789123456789L, 5. 100.1234567890123f, 6. 20000000000
int speed =
char fuel =
boolean running =
long serial =
float fuelRemaining =
String make =
int speed = 2000000000
char fuel = ‘D’
boolean running = false
long serial = 123456789123456789L
float fuelRemaining = 100.1234567890123f
String make = “Toyota”
Complete the code to call the go method and store the returned value:
public class Methods {
public static void main(String[] args) {
Methods example = new Methods();
int returned = ____________;
}
public ___ go () {
return 510;
}
}
public class Methods {
public static void main(String[] args) {
Methods example = new Methods();
int returned = example.go();
}
public int go () {
return 510;
}
}
Create an object of the Pizza class
Pizza {
pizza = new Pizza();
}