Week 2 Flashcards

1
Q

How do one make an if statment?

A

if (cond 1) {
—-body 1
} else if (cond 2){
—-body 2
} else {
—-body 3
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to make a while-loop?

A

while (condition) {
—-body
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to make do-while-loop?

A

do {
—-body
} while (condition);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to make for-loop?

A

for (int; Test; Update) {
—-body
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are statements?

A

An executable instruction that must end with ;
int x = 5;
System.out.println(x+” is the number”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are blocks?

A

Code which is surrounded by { and }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a modeling principal in java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a practical way to program entities?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What defines a type?

A

A type is defined by the value it can hold and the operations it can do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is “instance of a type”?

A

“Instance of a type” is an object with a specific type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is encapsulation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a way to extract encapsulated variables?

A

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;
—-}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a way to change encapsulated variables?

A

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;
—-}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a constructor?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are invariants?

A

Invariants are conditions which are always true, like date of birth.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the main purpose of information hiding?

A

The main purpose of information hiding is for making sure that member variables are not changed in wrong ways.

17
Q

What is abstraction?

A

Abstraction is using methods without being able to see the internals of the method. Used for making sure internals are kept intakt.

18
Q

What is a static method?

A

A static method is a method which can be used without creating an object of the class first.

19
Q

What is a static member variable?

A

A static member variable belong to the whole class not a specific object.

20
Q

How can numbers be compared?

A

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.

21
Q

How does one compare objects?

A

objects can be compared by use of the equals method.
obj1.equals(obj2);

22
Q

What are mutable objects?

A

Objects which can be changed after creation.

23
Q

What are immutable objects?

A

Objects that can not be changed after creation.

24
Q

What is inheritance?

A

Inheritance is that subclasses gain access to method of superclass.