OOP Flashcards

1
Q

What is OOP

A

OOP is a programming paradigm centered around the concept of objects.

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

What are the 4 Concepts of OOP?

A

Encapsulation
Inheritance
Polymorphism
Abstraction

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

What is Encapsulation

A

Encapsulation is a way to restrict the direct access to some components of an object, so users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object.

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

What is Inheritance

A

Inheritance is a mechanism that allows a class to inherit properties and behaviors from another class.

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

What is Polymorphism

A

Polymorphism in programming gives a program the ability to redefine methods for derived classes.

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

What is Abstraction

A

Through the process of abstraction, a programmer only puts relevant data about an object in order to reduce complexity and increase efficiency.

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

What are the 3 Java access modifiers?

A

Protected

\+ Public
- Private
# Protected
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does static and final do?

A

The static keyword is used to represent the class member. It is basically used with methods and variables to indicate that it is a part of the class, not the object.
static = global
final = constant

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

What is the class syntax?

A

public/private(access modifier)
class (keyword)
className{ codeblock }

public class Main {
  int x = 5;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the variable/attribute syntax?

A

public/private(access modifier)
datatype (keyword)
value

 private int x = 5;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the parameters syntax?

A

( datatype datavalue )

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

What is the method syntax?

A

public/private(access modifier)
return type (void or any other datatype)
methodName(parameters)
{codeblock}

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

What are the Java data types?

A

String
int
float
double
char
boolean

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

What are the 2 types of constructors?

A

Default Constructor

public class Person {
    private String name;
    private int age;
    
    // Normal constructor
    public Person() {
        this.name = "Unknown";
        this.age = 0;
    }
    
    // Getter method for name attribute
    public String getName() {
        return name;
    }
    
    // Getter method for age attribute
    public int getAge() {
        return age;
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object of Person using the default constructor
        Person person = new Person();
        
        // Getting the name and age attributes using getter methods
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}

Parameterized Constructor

`public class Person {
private String name;

// Parameterized constructor
public Person(String name) {
    this.name = name;
}

// Getter method for name attribute
public String getName() {
    return name;
} }

public class Main {
public static void main(String[] args) {
// Creating an object of Person with a parameterized constructor
Person person = new Person(“John”);

    // Getting the name attribute using getter
    System.out.println("Name: " + person.getName());
} } `
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the constructor syntax?

A

public/private(access modifier)
constructorName (Which would be the class name)
classAttributes = initial value;

 public Person( ) {
        this.name = "Unknown";
				this.age = 0;
    }

or

 public Person(String name) {
        this.name = name;
				this.age = age
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the getter syntax?

A

public/private(access modifier)
getterName()
{return attribute}

17
Q

What is the setter syntax?

A

public/private(access modifier)
void (return type)
setterName()
(parameters)
{this.attributename = parametervalue }

public void setName(String newName) {
this.name = newName;
}

18
Q

How to get user input?

A

import java.util.Scanner;
// Import the Scanner class

class Main {
public static void main(String[] args) {

Scanner myObj = new Scanner(System.in);  // Create a Scanner object
System.out.println("Enter username");

String userName = myObj.nextLine();  // Read user input

System.out.println("Username is: " + userName);   // Output user input   } }
19
Q

What is the Array syntax?

A

datatype [ ] arrayName = { value1, value2, value3 }

20
Q

How to loop through an array?

A

String[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}

OR

String[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};
for (String i : cars) {
System.out.println(i);
}

21
Q

What is the 2D array syntax?

A
datatype [ ] arrayName = 
{
    {value1, value2, value3}  //array 1   
    {value4, value5, value6}  //array 2

}
22
Q

How to loop through a 2D array?

A
public class Main {
  public static void main(String[ ] args) {
	
    int[ ][ ] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
		
    for (int i = 0; i < myNumbers.length; ++i) {
		
      for(int j = 0; j < myNumbers[i].length; ++j) {
        
				System.out.println(myNumbers[i][j]);
      }
    }
  }
}

Remember this

23
Q

What is the class diagram structure?

Three parts

A

Class Name
Attributes
Methods

24
Q

What are the 3 types of access modifiers and their symbols?

Class Diagrams

A

Protected

\+ Public
- Private
25
How do you identify static attributes? | Class Diagrams
They are underlined
26
Give me an example of a Class Diagram | What does it need (5)
Triangle //Class name - side1: double // private attribute - side2: double // private attribute - side3: double // private attribute + Triangle() // public constructor + setDimensions // public setter ( s1: double, s2: double, s3: double): void // return type void + calculateArea(): double // public method return type double + calculatePerimeter(): double //public method return type double | Name Attributes Constructor Setter Methods
27
Give me the java code for that class diagram
``` package triangle; import java.lang.Math; import java.util.Scanner; /** * * @author Deshawn */ public class Triangle { private double side1; private double side2; private double side3; //Constructor public Triangle() { side1 = 0.0; side2 = 0.0; side3 = 0.0; } // Setter public void setDimensions(double s1, double s2, double s3) { if (s1 > 0.0 && s2 > 0.0 && s3 > 0.0) { this.side1 = s1; this.side2 = s2; this.side3 = s3; } else { System.out.println("All dimensions must be greater than 0.0"); } } //Method to calculate area public double calculateArea() { double s = (side1 + side2 + side3) / 2; double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); return area; } //Method to calculate perimeter public double calculatePerimeter() { double perimeter = side1 + side2 + side3; return perimeter; } public static void main(String[] args) { //Triangle object Triangle triangle = new Triangle(); Scanner input = new Scanner(System.in); System.out.print("Enter the dimensions of the triangle"); double side1 = input.nextDouble(); double side2 = input.nextDouble(); double side3 = input.nextDouble(); triangle.setDimensions(side1, side2, side3); double area = triangle.calculateArea(); double perimeter = triangle.calculatePerimeter(); System.out.println("Area of the triangle: " + area); System.out.println("Perimeter of the triangle: " + perimeter); } } ```