Week 2 Flashcards
How do one make an if statment?
if (cond 1) {
—-body 1
} else if (cond 2){
—-body 2
} else {
—-body 3
}
How to make a while-loop?
while (condition) {
—-body
}
How to make do-while-loop?
do {
—-body
} while (condition);
How to make for-loop?
for (int; Test; Update) {
—-body
}
What are statements?
An executable instruction that must end with ;
int x = 5;
System.out.println(x+” is the number”);
What are blocks?
Code which is surrounded by { and }
What is a modeling principal in java?
Problems are represented as sets of interacting entities and the interactions between entities are represented by “messages” between them.
For example Univesity is based faculties, schools, staff, students and courses.
What is a practical way to program entities?
Create one compound type of the entity. Aka a template for the entitie which can be used when creating mutiples of the entity.
For example when creating students one should create a compound type which makes the process of creating many students efficient. I also uses abstraction where the underlying code of student is not known when creating a new student.
What defines a type?
A type is defined by the value it can hold and the operations it can do.
What is “instance of a type”?
“Instance of a type” is an object with a specific type.
What is encapsulation?
Encapsulation is a way to bind data to a class, meaning only the class where the data is declared can use the data. By declaring a variable “private” it can not be used outside the class.
What is a way to extract encapsulated variables?
To extract variables which are private one can use getter method.
public class Car {
—-private String model;
// constructor Car
—-Car( String model) {
——–this.model = model;
—-}
// here is the getter method
//can be used by other classes where car objects
// have been created
—-public String getModel() {
——–return model;
—-}
}
What is a way to change encapsulated variables?
To change private variables one can use setter method.
public class Car {
—-private String model;
// constructor Car
—-Car( String model) {
——–this.setModel(model);
—-}
// here is the setter method
//can be used by other classes where car
//objects have been created
// void is used since setter does not return a value
—-public void setModel(String model) {
——–this.model = model;
—-}
}
What is a constructor?
A constructer is a method called upon when an object of the class is created. Constructors must have the same name as a class.
class Main{
—-String language;
—-// creates the constructor
—-Main( String lang){
——–language = lang;
——–System.out.println( language + “programming language”);
—-}
—-public static void main (String[] args){
——– //create a new Main object which calls uppon the Main constructor*
——–Main obj1 = new Main(“Java”);
—-}
}
OUTPUT: Java Programming Language
What are invariants?
Invariants are conditions which are always true, like date of birth.
What is the main purpose of information hiding?
The main purpose of information hiding is for making sure that member variables are not changed in wrong ways.
What is abstraction?
Abstraction is using methods without being able to see the internals of the method. Used for making sure internals are kept intakt.
What is a static method?
A static method is a method which can be used without creating an object of the class first.
What is a static member variable?
A static member variable belong to the whole class not a specific object.
How can numbers be compared?
Integers can be compared by use of ==.
This will not work for floats. It is better to check if the absolute value of the difference between to floats are less than a certain treshold.
How does one compare objects?
objects can be compared by use of the equals method.
obj1.equals(obj2);
What are mutable objects?
Objects which can be changed after creation.
What are immutable objects?
Objects that can not be changed after creation.
What is inheritance?
Inheritance is that subclasses gain access to method of superclass.