CPSC 210

B4: Data Flow

Learning Goals

  • You need to be able to determine the value of a variable…
    • by reading code, and using the debugger
    • by tracing the value passed into a method
    • by tracing the value returned by a method
    • by determining if there are duplicate references to the same object
    • by understanding the scope of variables

Lecture Ticket Review

public class Person {
    private int age;

    public Person() {
        this.age = 1;
    }

    public int getAge() {
        return age;
    }

    public void getOlderByYears(int years) {
        this.age = this.age + years;
        years = years + 1;
        // POINT A
    }
}

public class Main {
    public static void main(String[] args) {
        Person jean = new Person();
        int ageAmount = 3;
        //POINT B

        jean.getOlderByYears(ageAmount);
        //POINT C
    }
}
public class Person {
    private int age;

    public Person() {
        this.age = 1;
    }

    public int getAge() {
        return age;
    }

    public void getOlderByYears(int years) {
        this.age = this.age + years;
        years = years + 1;
        // POINT A
    }
}

public class Main {
    public static void main(String[] args) {
        Person jean = new Person();
        int ageAmount = 3;
        //POINT B

        jean.getOlderByYears(ageAmount);
        //POINT C
    }
}

Q1

public class Person {
    private int age;

    public Person() {
        this.age = 1;
    }

    public int getAge() {
        return age;
    }

    public void getOlderByYears(int years) {
        this.age = this.age + years;
        years = years + 1;
        // POINT A
    }
}

public class Main {
    public static void main(String[] args) {
        Person jean = new Person();
        int ageAmount = 3;
        //POINT B

        jean.getOlderByYears(ageAmount);
        //POINT C
    }
}

Q2

public class Person {
    private int age;

    public Person() {
        this.age = 1;
    }

    public int getAge() {
        return age;
    }

    public void getOlderByYears(int years) {
        this.age = this.age + years;
        years = years + 1;
        // POINT A
    }
}

public class Main {
    public static void main(String[] args) {
        Person jean = new Person();
        int ageAmount = 3;
        //POINT B

        jean.getOlderByYears(ageAmount);
        //POINT C
    }
}

Q3

public class Person {
    private int age;

    public Person() {
        this.age = 1;
    }

    public int getAge() {
        return age;
    }

    public void getOlderByYears(int years) {
        this.age = this.age + years;
        years = years + 1;
    }
    
    public void copyInto(Person other) {
    	other = this;
    }
}

public class Main {
    public static void main(String[] args) {
        Person jean = new Person();
        Person manon = new Person();
        jean.getOlderByYears(10);
        jean.copyInto(manon);
        System.out.println(manon.getAge());
    }
}

BONUS: Q4

What does this print?

We asked what happens to the argument when we reassign a parameter's value. Which is the answer?

  • Nothing, if it's of a primitive type rather than a reference to an object.
  • Nothing, because the parameter's value is in its own space in memory.

Lecture Lab

Please select an option (plus or quit):
plus
you selected: plus
Please enter the first number to plus
6
Please enter the second number to plus
2
that equals: 0
Please select an option (plus or quit):
plus
you selected: plus
Please enter the first number to plus
8
Please enter the second number to plus
5
that equals: 0
Please select an option (plus or quit):
plus
you selected: plus
Please enter the first number to plus
12
Please enter the second number to plus
32
that equals: 0
Please select an option (plus or quit):
quit
you selected: quit
Operation log: [
plus [6, 2, 8, 5, 12, 32] equals 44, 
plus [6, 2, 8, 5, 12, 32] equals 44, 
plus [6, 2, 8, 5, 12, 32] equals 44]
plus[6,2] equals 8, plus [8,5] equals 13...

Expected:

Extra: Git Branching

Branch: main

Branch: myfavoriteanimal

Create
Branch

Merge
Branch

Commits

B4: Data Flow

The End - Thank You!

Made with Slides.com